-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathulliststr.cpp
More file actions
162 lines (141 loc) · 2.72 KB
/
ulliststr.cpp
File metadata and controls
162 lines (141 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <cstddef>
#include <stdexcept>
#include "ulliststr.h"
ULListStr::ULListStr()
{
head_ = NULL;
tail_ = NULL;
size_ = 0;
}
ULListStr::~ULListStr()
{
clear();
}
bool ULListStr::empty() const
{
return size_ == 0;
}
size_t ULListStr::size() const
{
return size_;
}
// WRITE YOUR CODE HERE
void ULListStr::push_back(const std::string& val){
if(empty()){
head_=tail_=new Item();
head_->first=0;
head_->last=0;
}
if(tail_->last==ARRSIZE){
Item* newItem = new Item();
newItem->first = 0;
newItem->last=0;
tail_->next = newItem;
newItem->prev = tail_;
tail_= newItem;
}
tail_->val[tail_->last]=val;
tail_->last++;
size_++;
}
void ULListStr::pop_back(){
if(empty()) return;
tail_->last--;
size_--;
if(tail_->first == tail_->last){
Item* temp = tail_;
tail_ = tail_->prev;
if(tail_!= NULL){
tail_->next = NULL;
}else{
head_=NULL;
}
delete temp;
}
}
void ULListStr::push_front(const std::string& val){
if(empty()){
head_=tail_=new Item();
head_->first=ARRSIZE;
head_->last=ARRSIZE;
}
if(head_->first==0){
Item* newItem = new Item();
newItem->first = ARRSIZE;
newItem->last=ARRSIZE;
head_->prev = newItem;
newItem->next = head_;
head_= newItem;
}
head_->first--;
head_->val[head_->first]=val;
size_++;
}
void ULListStr::pop_front(){
if(empty()) return;
head_->first++;
size_--;
if(head_->first == head_->last){
Item* temp = head_;
head_ = head_->next;
if(head_!= NULL){
head_->prev = NULL;
}else{
tail_=NULL;
}
delete temp;
}
}
std::string const & ULListStr::back() const{
return tail_->val[tail_->last-1];
}
std::string const & ULListStr::front() const{
return head_->val[head_->first];
}
std::string* ULListStr::getValAtLoc(size_t loc) const{
Item* curr= head_;
size_t idx=0;
while(curr!=NULL){
size_t bs=curr->last-curr->first;
if(loc<idx+bs){
return &curr->val[curr->first+(loc-idx)];
}
idx+=bs;
curr=curr->next;
}
return NULL;
}
void ULListStr::set(size_t loc, const std::string& val)
{
std::string* ptr = getValAtLoc(loc);
if(ptr == NULL){
throw std::invalid_argument("Bad location");
}
*ptr = val;
}
std::string& ULListStr::get(size_t loc)
{
std::string* ptr = getValAtLoc(loc);
if(ptr == NULL){
throw std::invalid_argument("Bad location");
}
return *ptr;
}
std::string const & ULListStr::get(size_t loc) const
{
std::string* ptr = getValAtLoc(loc);
if(ptr == NULL){
throw std::invalid_argument("Bad location");
}
return *ptr;
}
void ULListStr::clear()
{
while(head_ != NULL){
Item *temp = head_->next;
delete head_;
head_ = temp;
}
tail_ = NULL;
size_ = 0;
}