-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.h
225 lines (204 loc) · 4.11 KB
/
DoublyLinkedList.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
#pragma once
#include <iostream>
using namespace std;
template <class T>
struct Node {
T data;
Node<T> * prev;
Node<T> * next;
};
template <class T>
class DoublyLinkedList
{
public:
DoublyLinkedList();
DoublyLinkedList(const DoublyLinkedList &list);
DoublyLinkedList& operator=(const DoublyLinkedList& list);
~DoublyLinkedList();
void add(T data);
T get(int index);
T operator[](int index);
void remove(int index);
int search(T data);
int getSize();
void printAll();
void printAllReversed();
void clear();
private:
Node<T> * first;
Node<T> * last;
const int NOT_FOUND = -1;
int size;
};
// definitions
template <class T>
DoublyLinkedList<T>::DoublyLinkedList()
{
this->first = nullptr;
this->last = nullptr;
this->size = 0;
}
template<class T>
DoublyLinkedList<T>::DoublyLinkedList(const DoublyLinkedList & list)
{
Node<T> * current = list->first;
while (current != nullptr) {
this->add(current->data);
current = current->next;
}
}
template<class T>
DoublyLinkedList<T> & DoublyLinkedList<T>::operator=(const DoublyLinkedList & list)
{
if (this == &list)
return *this;
DoublyLinkedList<T> * tempList = new DoublyLinkedList<T>();
Node<T> * current = list.first;
while (current != nullptr) {
DoublyLinkedList<T> * tempList = new DoublyLinkedList<T>();
tempList->add(current->data);
}
return *tempList;
}
template <class T>
DoublyLinkedList<T>::~DoublyLinkedList()
{
first = nullptr;
last = nullptr;
size = 0;
}
template <class T>
void DoublyLinkedList<T>::clear() {
this->first = nullptr;
this->last = nullptr;
this->size = 0;
}
template <class T>
void DoublyLinkedList<T>::add(T data)
{
if (!this->first) {
// empty list so far
this->first = new Node<T>;
this->first->data = data;
this->first->prev = nullptr;
this->first->next = nullptr;
this->last = this->first;
this->size++;
}
else {
// non empty list
if (this->last == this->first) {
// has only one element
this->last = new Node<T>;
this->last->data = data;
this->last->next = nullptr;
this->last->prev = this->first;
this->first->next = this->last;
this->size++;
}
else {
// has more than one element
Node<T> * node = new Node<T>();
node->data = data;
node->prev = this->last;
node->next = nullptr;
this->last->next = node;
this->last = node;
this->size++;
}
}
}
template <class T>
T DoublyLinkedList<T>::get(int index)
{
Node<T> * current = this->first;
for (int i = 0; i < index; i++) {
if (!current)
break;
current = current->next;
}
return current->data;
}
template <class T>
T DoublyLinkedList<T>::operator[](int index)
{
return get(index);
}
template <class T>
void DoublyLinkedList<T>::remove(int index)
{
if (!this->first) {
return;
}
else if(index == 0) {
// remove the first element
if (this->first == this->last) {
// has only one element
this->first = nullptr;
this->last = nullptr;
this->size--;
}
else {
// has more than one element
this->first = this->first->next;
this->first->prev = nullptr;
this->size--;
}
}
else {
//index to be removed > 0
Node<T> * current = this->first;
for (int i = 0; i < index - 1; i++) {
current = current->next;
}
if (current->next == this->last) {
current->next = nullptr;
this->last = current;
this->size--;
}
else {
current->next = current->next->next;
current->next->prev = current;
this->size--;
}
}
}
template <class T>
int DoublyLinkedList<T>::search(T data)
{
int i = 0;
Node<T> * current = this->first;
while (current != nullptr) {
if (current->data == data) {
return i;
}
i++;
current = current->next;
}
return this->NOT_FOUND;
}
template<class T>
inline int DoublyLinkedList<T>::getSize()
{
return this->size;
}
template <class T>
void DoublyLinkedList<T>::printAll()
{
Node<T> * current = this->first;
while (current != nullptr) {
cout << current->data << endl;
current = current->next;
}
cout << "-----------\n";
}
template <class T>
void DoublyLinkedList<T>::printAllReversed()
{
Node<T> * current = this->last;
while (current != nullptr) {
cout << current->data << endl;
current = current->prev;
}
cout << "-----------\n";
}