-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.h
226 lines (180 loc) · 5.33 KB
/
List.h
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
#ifndef LIST_H
#define LIST_H
template<typename T>
class List {
public:
List() {};
virtual ~List();
List(const List& rhs);
List& operator=(const List& rhs);
void insertAt(int pos, const T& element) noexcept(false);
T getAt(int pos) const noexcept(false);
void removeAt(int pos) noexcept(false);
bool removeElement(const T& element);
bool findElement(const T& toFind) const;
int length() const;
void getAll(T arr[], int cap) noexcept(false);
private:
/* Inner class for nodes. */
class Node {
public:
Node() {};
Node(const T& element) : elem(element) {};
~Node() {};
T elem;
Node *link = nullptr;
};
/* Outer class privates. */
Node *head = nullptr; /* First node. */
int len = 0;
void deepCopy(const List& other);
void free();
};
#endif
/* Deep copy. */
template<typename T>
void List<T>::deepCopy(const List& other) {
Node* otherNode = other.head;
for (int i = 0; i < other.len; i++) {
insertAt(i, otherNode->elem);
otherNode = otherNode->link;
}
this->len = other.len;
}
template<typename T>
void List<T>::free() {
Node* prevNode = this->head;
Node *nextNode = nullptr;
for (int i = 0; i < this->len; i++ && prevNode) {
nextNode = prevNode->link;
delete prevNode;
prevNode = nullptr;
prevNode = nextNode;
}
}
/* List destructor. */
template<typename T>
List<T>::~List() {
free();
}
/* Ctor. */
template<typename T>
List<T>::List(const List& rhs) {
deepCopy(rhs);
}
/* Overloading assignment operator. */
template<typename T>
List<T>& List<T>::operator=(const List& rhs) {
if (this != &rhs) {
free();
deepCopy(rhs);
}
return *this;
}
template<typename T>
void List<T>::insertAt(int pos, const T& element) noexcept(false) {
if (pos > this->len) throw "Error: Position is larger than length of list.";
if (pos < 0) throw "Error: Position cannot be negative.";
Node *node = new Node(element); /* Pointer to new node. */
node->elem = element;
Node *nextNode = this->head;
Node *prevNode = nullptr;
if (pos == 0) { /* Insert at head. */
node->link = (this->head) ? this->head : nullptr;
this->head = node;
} else { /* Insert at pos > 0. */
for (int i = 0; i < pos; i++) {
prevNode = nextNode;
nextNode = nextNode->link;
}
prevNode->link = node;
node->link = nextNode;
}
this->len++;
}
/* Return element at pos N. */
template<typename T>
T List<T>::getAt(int pos) const noexcept(false) {
if (pos > this->len) throw "Error: Position is larger than length of list.";
if (pos < 0) throw "Error: Position cannot be negative.";
Node *node = this->head;
/* Traverse list until iterator is at pos N. */
for (int i = 0; i < pos; i++) {
node = node->link;
}
return node->elem;
}
/* Remove element at pos N */
template<typename T>
void List<T>::removeAt(int pos) noexcept(false) {
if (pos > this->len) throw "Error: Position is larger than length of list.";
if (pos < 0) throw "Error: Position cannot be negative.";
Node *node = this->head;
Node *prevNode = nullptr;
if (pos == 0) {
Node *nextNode = this->head->link;
delete this->head;
this->head = nextNode;
} else {
for (int i = 0; i < pos; i++) {
prevNode = node; /* Node preceding the one we're removing. */
node = node->link; /* Node to remove. */
if (!node->link) { /* At tail. */
delete node;
prevNode->link = nullptr;
this->len--;
return;
}
}
prevNode->link = node->link; /* Set a valid link to the next node to
* preserve sequential pattern.
*/
delete node;
node = nullptr;
}
this->len--;
}
/* Remove element based on element value. */
template<typename T>
bool List<T>::removeElement(const T& element) {
bool removed = false;
Node *node = nullptr;
Node *prevNode = this->head;
for (int pos = 0; pos < this->len; pos++) {
node = prevNode;
prevNode = node->link;
if (node->elem == element) {
removeAt(pos);
removed = true;
}
}
return removed;
}
/* Check if element exists. */
template<typename T>
bool List<T>::findElement(const T& toFind) const {
bool found = false;
Node *prevNode = this->head;
/* Traverse nodes and compare elements to passed arg. */
for (int i = 0; i < this->len; i++) {
if (prevNode->elem == toFind) {
found = true;
}
prevNode = prevNode->link;
}
return found;
}
/* Return amount of existing elements in list. */
template<typename T>
int List<T>::length() const {
return this->len;
}
/* Populate an array with elements from list. */
template<typename T>
void List<T>::getAll(T arr[], int cap) noexcept(false) {
if (this->len > cap) throw "Error: Length of list is larger than cap.";
for (int i = 0; i < (this->len); i++) {
arr[i] = getAt(i);
}
}
/* vim: set ts=4 sw=4 tw=79 ft=cpp ff=unix et :*/