-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.h
247 lines (206 loc) · 6.05 KB
/
LinkedList.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include "Exceptions.h"
template <typename T>
class Node{
public:
T value;
Node<T> *prev;
Node<T> *next;
public:
explicit Node(const T &n_val = 0, Node<T> *n_prev = nullptr, Node<T> *n_next = nullptr):
value(n_val), prev(n_prev), next(n_next){};
};
template <class T>
class LinkedList{
private:
int length;
Node<T>* head;
Node<T>* tail;
private:
Node<T>* getNode (int index) const{
if (index<0 || index >= length || head == nullptr) throw IndexOutOfRange();
Node<T>* temp = head;
for (int pos = 0; pos < index; ++pos) temp = temp->next;
return temp;
}
void assertIndexCorrect(int index) const {
if (index >= length || index < 0) throw IndexOutOfRange();
};
void assertListNotEmpty() const{
if (head == nullptr) throw EmptyListException();
}
public:
//constructors
LinkedList(): length(0), head(nullptr), tail(nullptr) {};
LinkedList(T* items, const int count): length(0), head(nullptr), tail(nullptr){
if (count > 0) {
head = nullptr;
tail = nullptr;
for (int i = 0; i < count; ++i) {
append(items[i]);
}
length = count;
}
}
explicit LinkedList(const int size):length(0), head(nullptr), tail(nullptr) {
if (size < 0) throw IndexOutOfRange();
if (size > 0) {
head = new Node<T>;
tail = head;
for (int i = 1; i < size; ++i) {
tail->next = new Node<T>;
tail = tail->next;
}
}
length = size;
};
LinkedList(const LinkedList<T> &other): length(0), head(nullptr), tail(nullptr) {
Node<T> *temp = other.head;
while (temp != nullptr) {
append(temp->value);
temp = temp->next;
}
length = other.length;
}
//destructor
~LinkedList() {
Node<T> *curr = head;
while (curr) {
Node<T> *temp = curr->next;
delete curr;
curr = temp;
}
}
//methods
T& get(const int index) const{
assertIndexCorrect(index);
assertListNotEmpty();
Node<T> *temp = head;
for (int pos = 0; pos < index; ++pos) temp = temp->next;
return temp->value;
}
T& getFirst() const{
assertListNotEmpty();
return head->value;
}
T& getLast() const{
assertListNotEmpty();
return tail->value;
}
LinkedList<T>* getSubList(int startIndex, int endIndex) const {
assertListNotEmpty();
assertIndexCorrect(startIndex);
assertIndexCorrect(endIndex);
if (startIndex > endIndex) throw IndexOutOfRange();
auto *sublist = new LinkedList<T>();
Node<T> *temp = getNode(startIndex);
for (int i = startIndex; i <= endIndex;i++) {
sublist->append(temp->value);
temp = temp->next;
}
return sublist;
}
int getLength() const{
return length;
}
void append(const T& item) {
auto *el = new Node<T>(item, nullptr, nullptr);
if(head == nullptr) {
head = tail = el;
length++;
return ;
}
tail->next = el;
el->prev = tail;
tail = el;
length++;
}
void prepend(const T& item) {
auto *el = new Node<T>(item, nullptr, head);
if (head != nullptr) {
head->prev = el;
}
else {tail = el;}
head = el;
length++;
}
void set(const int index,const T& item) {
assertIndexCorrect(index);
Node<T> *curr = head;
for(int pos = 0; pos < index; ++pos) curr = curr->next;
curr->value = item;
}
void insertAt(const T& item,const int index) { // inserts node on stated index before previous
if (index<0 || index > length ) throw IndexOutOfRange();
if (index == 0) {
prepend(item);
}
else if (index == length) {
append(item);
}
else {
Node<T>* curr = head;
auto *new_node = new Node<T>(item, nullptr, nullptr);
for (int pos = 0; pos < index ; pos++) curr= curr->next;
new_node->prev= curr->prev;
curr->prev = new_node;
new_node->next = curr;
new_node->prev->next = new_node;
length++;
}
}
LinkedList<T>* concat(LinkedList<T> *list) {
auto *new_list = new LinkedList<T>(*this);
Node<T> *el1 = new_list->head;
Node<T> *el2 = list->head;
while (el1->next != nullptr) el1 = el1->next;
while (el1->next!=nullptr) {
el1->next = new Node<T>(el2->value);
el1->next->prev = el1;
el1 = el1->next;
el2 = el2->next;
++new_list->length;
}
return new_list;
}
void print(){
Node<T> *temp = head;
int pos = 0;
if (length == 1) {std::cout << head->value; return;}
while (temp != nullptr){
if (temp != head) std::cout<< " ";
std::cout<< this->get(pos);
temp = temp->next;
pos++;
}
}
//
// const T& operator [] (int index) const{
// assertIndexCorrect(index);
// Node<T> *el = head;
// for (int i = 0; i < index; i++) el = el->next;
// return el->value;
// }
//
// T& operator [] (int index) {
// assertIndexCorrect(index);
// Node<T> *el = head;
// for (int i = 0; i < index; i++) el = el->next;
// return el->value;
// }
bool operator==(const LinkedList<T>& other) {
if (this == &other) return true;
if (length == other.getLength()){
for (int i = 0; i < length; ++i) {
if ((*this).get(i) != (other).get(i)) {
return false;
}
}
return true;
}
return false;
}
};
#endif //LINKEDLIST_H