-
Notifications
You must be signed in to change notification settings - Fork 77
/
list.cpp
254 lines (224 loc) · 5.6 KB
/
list.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#ifndef list_H
#define list_H
#include <iostream>
#include <stdexcept>
template <typename T>
class list {
public:
// Rule of three
list();
list(list<T> const &);
list(list<T> &&);
list <T> & operator = (list<T> );
list <T> & operator = (list<T> &&);
~list();
/* Modifiers */
void push_back(T&& data);
void push_back(T const& data);
void push_front(T&& data);
void push_front(T const& data);
void pop_back();
void pop_front();
void swap(list &x);
void clear();
/* Capacity */
size_t size() const;
bool empty() const;
/* Element Access */
T& front();
T const& front() const;
T& back();
T const& back() const;
/* Iterators */
/*
pointers doesn't suffice as iterators since the steps from an object to it's adjacent objects is not as easy as an increase / decrease of the adress. You have to access the underlying nodes next and prev pointers. Hence the iterator type must an own class.
*/
// typedef T* iterator;
// typedef T const* const_iterator;
// typedef T* reverse_iterator;
// typedef T const* const_reverse_iterator;
// const_iterator begin() const; // cbegin
// iterator begin();
//
// const_iterator end() const; //cend()
// iterator end();
//
// const_reverse_iterator rbegin() const;
// reverse_iterator rbegin();
//
// const_reverse_iterator rend() const;
// reverse_iterator rend();
private:
struct node {
int data;
node *next, *prev;
node(T const& data, node* next, node* prev)
: data(data)
, next(next)
, prev(prev) {
}
node(T&& data, node* next, node* prev)
: data(std::move(data))
, next(next)
, prev(prev) {
}
};
int elements;
node *head, *tail;
};
// empty constructor
template <typename T>
list<T>::list() {
elements = 0;
head = tail = nullptr;
}
// destructor
template <typename T>
list <T>::~list() {
node* tmp;
while(head) {
tmp = head;
head = head->next;
delete tmp;
}
}
template <typename T>
void list<T>::swap(list &that) {
std::swap(head, that.head);
std::swap(tail, that.tail);
std::swap(elements, that.elements);
}
/* Copy assignment operator */
template <typename T>
list<T>& list<T>::operator = (list<T> rhs) {
// copy and swap idiom
this->swap(rhs);
}
/* Move assignment operator */
template <typename T>
list<T>& list<T>::operator = (list<T> &&rhs) {
this->swap(rhs);
}
/* Copy contructor */
template <typename T>
list<T>::list(list<T> const& rhs)
: elements(rhs.size()) {
if(!rhs.empty()) {
node* rhsIt = rhs.head;
node* It = new node(rhsIt->data, nullptr, nullptr);
head = It;
while((rhsIt = rhsIt->next) != rhs.tail) {
try {
node* Next = new node(rhsIt->data, It, nullptr);
It = It->next = Next;
} catch(std::bad_alloc& Exception) {
/* if bad allocation occur, then delete all the already allocated node */
for(node* Last; head != nullptr; delete Last) {
Last = head;
head = head->next;
}
throw;
}
}
tail = It;
tail->next = nullptr;
}
}
template <typename T>
T& list<T>:: front() {
if(head == nullptr)
throw std::runtime_error("Invalid Action!");
return head->data;
}
template <typename T>
T& list<T>:: back() {
if(tail == nullptr)
throw std::runtime_error("Invalid Action!");
return tail->data;
}
template <typename T>
void list<T>::push_back(T const& data) {
node* newNode = new node(data, nullptr, tail);
if(head == nullptr)
head = newNode;
if(tail != nullptr)
tail->next = newNode;
tail = newNode;
++elements;
}
template <typename T>
void list<T>::push_back(T&& data) {
node* newNode = new node(std::move(data), nullptr, tail);
if(head == nullptr)
head = newNode;
if(tail != nullptr)
tail->next = newNode;
tail = newNode;
++elements;
}
template <typename T>
void list<T>::push_front(T const& data) {
node* newNode = new node(data, head, nullptr);
if(tail == nullptr)
tail = newNode;
if(head != nullptr)
head->prev = newNode;
head = newNode;
++elements;
}
template <typename T>
void list<T>::push_front(T&& data) {
node* newNode = new node(std::move(data), head, nullptr);
if(tail == nullptr)
tail = newNode;
if(head != nullptr)
head->prev = newNode;
head = newNode;
++elements;
}
template <typename T>
void list<T>::pop_front() {
if(head == nullptr)
throw std::runtime_error("Invalid Action");
node *tmp = head;
head = head->next;
if(head != nullptr)
head->prev = nullptr;
--elements;
if(elements == 0)
tail = nullptr;
delete tmp;
}
template <typename T>
void list<T>::pop_back() {
if(tail == nullptr)
throw std::runtime_error("Invalid Action");
node *tmp = tail;
tail = tail->prev;
if(tail != nullptr)
tail->next = nullptr;
--elements;
if(elements == 0)
head = nullptr;
delete tmp;
}
template <typename T>
bool list<T>::empty() const {
return head == nullptr;
}
template <typename T>
size_t list<T>::size() const {
return elements;
}
template <typename T>
void list<T>::clear() {
node* curr = head;
while(head) {
curr = head;
head = head->next;
delete curr;
}
head = tail = nullptr;
elements = 0;
}
#endif // list_H