-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathulliststr.cpp
More file actions
205 lines (175 loc) · 3.26 KB
/
Copy pathulliststr.cpp
File metadata and controls
205 lines (175 loc) · 3.26 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#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_;
}
////////////////////////////////////////////////////
std::string* ULListStr::getValAtLoc(size_t loc) const{
if(empty()){
return nullptr;
}
if(loc >= size_){
return nullptr;
}
Item* curr = head_;
while(curr != nullptr){
size_t blockSize = curr->last - curr->first;
if(loc < blockSize){
return &curr->val[curr->first + loc];
}
else{
loc -= blockSize;
curr = curr->next;
}
}
return nullptr;
}
std::string const & ULListStr::back() const
{
if(empty()){
throw std::invalid_argument("List is empty");
}
return tail_->val[tail_->last-1];
}
// WRITE YOUR CODE HERE
std::string const & ULListStr::front() const
{
if(empty()){
throw std::invalid_argument("List is empty");
}
return head_->val[head_->first];
}
void ULListStr::push_back(const std::string& val)
{
if(empty()){
head_= new Item();
tail_ = head_;
head_->first = 0;
head_->last =0;
head_->prev = nullptr;
head_->next = nullptr;
}
if(tail_->last == ARRSIZE){
Item* node = new Item();
node->first = 0;
node->last = 0;
node->prev = tail_;
node->next = nullptr;
tail_->next = node;
tail_ = node;
}
tail_->val[tail_->last] = val;
tail_->last += 1;
size_ +=1;
}
void ULListStr::push_front(const std::string& val)
{
if(empty()){
head_= new Item();
tail_ = head_;
head_->first = ARRSIZE;
head_->last =ARRSIZE;
head_->prev = nullptr;
head_->next = nullptr;
}
if(head_->first == 0){
Item* node = new Item();
node->first = ARRSIZE;
node->last = ARRSIZE;
node->prev = nullptr;
node->next = head_;
head_->prev = node;
head_ = node;
}
head_->first -= 1;
head_->val[head_->first] = val;
size_ +=1;
}
void ULListStr::pop_back()
{
if(empty()){
return;
}
tail_->last -= 1;
size_ -= 1;
if(tail_->first == tail_ -> last){
Item* old = tail_;
tail_ = tail_->prev;
if(tail_){
tail_->next = nullptr;
}
else{
head_ = nullptr;
}
delete old;
}
}
void ULListStr::pop_front()
{
if(empty()){
return;
}
head_->first += 1;
size_ -= 1;
if(head_->first == head_ -> last){
Item* old = head_;
head_ = head_->next;
if(head_){
head_->prev = nullptr;
}
else{
tail_ = nullptr;
}
delete old;
}
}
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;
}