-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked-list.h
185 lines (157 loc) · 4.39 KB
/
linked-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
#ifndef DDLINKED_LIST_H
#define DDLINKED_LIST_H
#include <functional>
template<class T>
class LinkedListNode {
public:
T data;
LinkedListNode<T>* next;
LinkedListNode() : next(nullptr) {}
LinkedListNode(T data, LinkedListNode<T>* next = nullptr) : data(data), next(next) {}
~LinkedListNode();
};
template<typename T>
LinkedListNode<T>::~LinkedListNode() {
delete next;
}
template<class T>
class LinkedList {
private:
LinkedListNode<T>* dummyHead;
int count;
public:
LinkedList();
~LinkedList();
bool isEmpty();
bool isLast(LinkedListNode<T>* node);
int length();
// find index of a node with specified value, -1 will returned if the node doesn't exist
// NOTE: the returned index may become invalid after any node insert/delete operation
int find(LinkedListNode<T> target);
int find(T target);
// retrieve the pointer which point to the node indexed by index
// NOTE: the index of node which the returned pointer point to may change after any node insert/add/delete operation
LinkedListNode<T>* retrieve(int index);
// insert a node to position index, and move all node behind one step back
// i.e. if index set to zero, the new node will be first node
// this function cannot append a node, if you want to do that, use add()
void insert(T data, int index);
void insert(LinkedListNode<T>* node, int index);
// add a node back to the linked list
void add(T data);
// traverse through all node of the linked list, and for each node call provided function
// first int value in callback function is index of the node, and second value is the value of the node
void traverse(std::function<void(int, T)> callback);
void traverse(std::function<void(int, LinkedListNode<T>* node)> callback);
};
template<typename T>
LinkedList<T>::LinkedList() {
dummyHead = new LinkedListNode<T>();
count = 0;
}
template<typename T>
LinkedList<T>::~LinkedList() {
LinkedListNode<T>* p = dummyHead->next;
while (p) {
LinkedListNode<T>* temp = p->next;
delete p;
p = temp;
}
}
template<typename T>
bool LinkedList<T>::isEmpty() {
return dummyHead->next == nullptr;
}
template<typename T>
bool LinkedList<T>::isLast(LinkedListNode<T>* node) {
return node->next == nullptr;
}
template<typename T>
int LinkedList<T>::length() {
return count;
}
template<typename T>
int LinkedList<T>::find(LinkedListNode<T> target) {
return find(target.data);
}
template<typename T>
int LinkedList<T>::find(T target) {
int index = 0;
bool flag = false;
LinkedListNode<T>* p = dummyHead->next;
while(p) {
if (target == p->data) {
flag = true;
break;
}
index++;
}
return flag ? index : -1;
}
template<typename T>
LinkedListNode<T>* LinkedList<T>::retrieve(int index) {
if (index >= count || index < 0) {
return nullptr;
}
LinkedListNode<T>* p = dummyHead->next;
int i = 0;
while(p) {
if ( index == i) {
return p;
}
p = p->next;
i++;
}
return nullptr;
}
template<typename T>
void LinkedList<T>::insert(T data, int index) {
if (index >= count || index < 0) return;
LinkedListNode<T>* nodePrev;
if (index == 0) {
nodePrev = dummyHead;
} else {
nodePrev = retrieve(index - 1);
}
LinkedListNode<T>* newNode = new LinkedListNode<T>(data);
newNode->next = nodePrev->next;
nodePrev->next = newNode;
count++;
}
template<typename T>
void LinkedList<T>::insert(LinkedListNode<T>* node, int index) {
insert(node->data, index);
}
template<typename T>
void LinkedList<T>::add(T data) {
LinkedListNode<T>* last;
if (isEmpty()) {
last = dummyHead;
} else {
last = retrieve(count - 1);
}
LinkedListNode<T>* newNode = new LinkedListNode<T>(data);
last->next = newNode;
count++;
}
template<typename T>
void LinkedList<T>::traverse(std::function<void(int, T)> callback) {
LinkedListNode<T>* p = dummyHead->next;
int index = 0;
while(p) {
callback(index, p->data);
index++;
p = p->next;
}
}
template<typename T>
void LinkedList<T>::traverse(std::function<void(int, LinkedListNode<T>* node)> callback) {
LinkedListNode<T>* p = dummyHead->next;
int index = 0;
while(p) {
callback(index, p);
index++;
p = p->next;
}
}
#endif