-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoubleLinkedLinearList.h
More file actions
313 lines (291 loc) · 7.67 KB
/
Copy pathDoubleLinkedLinearList.h
File metadata and controls
313 lines (291 loc) · 7.67 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// Created by 葛尧 on 2017/7/26.
//
#ifndef LINEARLIST_DOUBLELINKEDLINEARLST_H
#define LINEARLIST_DOUBLELINKEDLINEARLST_H
#include "LinearList.h"
#include "DNode.h"
#include <iostream>
using namespace std;
/**
* LinearList with double linked Node
* Operations will be easy
* If there is only one DNode, it's next and before should be itself.
* @tparam T
*/
template <class T>
class DoubleLinkedLinearList{
private:
DNode<T>* head = nullptr;
public:
virtual DNode<T> *getHead() const ;
virtual void setHead(DNode<T> *head) ;
virtual bool empty() const ;
virtual int getSize() const ;
virtual DNode<T> *getNode(const int index) const ;
virtual bool insert(const int index, DNode<T> *node) ;
virtual bool remove(DNode<T> *node) ;
virtual bool remove(const int index) ;
virtual bool add(DNode<T> *node) ;
virtual bool circleJudge() const ;
virtual DNode<T> *getTailNode() const ;
virtual void info() const ;
};
template <class T>
DNode<T> *DoubleLinkedLinearList<T>::getHead() const {
return this->head;
}
template <class T>
void DoubleLinkedLinearList<T>::setHead(DNode<T> *head) {
if (head == nullptr){
this->head = nullptr;
return;
}
if (head->getNextNode() == nullptr){
head->setNextNode(head);
}
if (head->getBefore() == nullptr){
head->setBefore(head);
}
this->head = head;
}
template <class T>
bool DoubleLinkedLinearList<T>::empty() const {
return head == nullptr;
}
/**
* Count the size of list.
* It can apply normal list, circle list, empty list.
* @tparam T
* @return
*/
template <class T>
int DoubleLinkedLinearList<T>::getSize() const {
DNode<T>* node = getHead();
if (node == nullptr){
return 0;
}
DNode<T>const * head = getHead();
int i = 1;
while( node->getNextNode() != head
&& node->getNextNode()->getBefore() == node){
i ++;
node = node->getNextNode();
}
return i;
}
/**
* get a DNode at the index
* @tparam T
* @param index
* @return
*/
template <class T>
DNode<T> *DoubleLinkedLinearList<T>::getNode(int index) const {
DNode<T>* target = getHead();
if (target == nullptr){
return nullptr;
}
if (index >= 0){
// Traversal the list with next
int i = 0;
while (i < index){
i ++;
if (target != nullptr){
target = target->getNextNode();
} else{
return nullptr;
}
}// After loop, node1 is the target
} else{
//Traversal the list with before
int i = 0;
while(i > index){
i--;
if (target != nullptr){
target = target->getBefore();
} else{
return nullptr;
}
}
}
return target;
}
/**
* Insert a not after a clear index
* @tparam T
* @param index
* @param node
* @return
*/
template <class T>
bool DoubleLinkedLinearList<T>::insert(const int index, DNode<T> *node) {
DNode<T>* indexNode = getNode(index);
// empty
if (indexNode == nullptr){
node->setNextNode(node);
node->setBefore(node);
setHead(node);
return true;
} else{
//Not Empty
node->setBefore(indexNode);
node->setNextNode(indexNode->getNextNode());
indexNode->getNextNode()->setBefore(node);
indexNode->setNextNode(node);
return true;
}
}
/**
* delete a node from the list
* @tparam T
* @param node
* @return
*/
template <class T>
bool DoubleLinkedLinearList<T>::remove(DNode<T> *node) {
if (empty()){
return false;
}
if (getSize() == 1 && node == getHead()){
setHead(nullptr);
return true;
}
if (node->getBefore()== nullptr){ // this Node may probably not in this list
return false;
} else{
if (node->getNextNode() == nullptr){
node->setNextNode(this->getHead());
}
node->getBefore()->setNextNode(node->getNextNode());
node->getNextNode()->setBefore(node->getBefore());
if (node == getHead()){
this->setHead(node->getNextNode());
}
return true;
}
}
/**
* Delete a node from the list and memory
* @tparam T
* @param index
* @return
*/
template <class T>
bool DoubleLinkedLinearList<T>::remove(const int index) {
DNode<T>* node1 = getHead();
DNode<T>* target = nullptr;
if (node1 == nullptr){
return false;
}
if (index >= 0){
// Traversal the list with next
int i = 0;
while (i <= index){
i ++;
if (node1 != nullptr){
node1 = node1->getNextNode();
} else{
return false;
}
}// After loop, node1 is the target
} else{
//Traversal the list with before
int i = 0;
while(i > index){
i--;
if (node1 != nullptr){
node1 = node1->getBefore();
} else{
return false;
}
}
}
target = node1;
//delete the target
target->getBefore()->setNextNode(target->getNextNode());
target->getNextNode()->setBefore(target->getBefore());
if (target == getHead()){
if (getSize() == 1){
setHead(nullptr);
} else{
setHead(target->getNextNode());
}
}
delete target;
target = nullptr;
return false;
}
/**
* Add a new DNode after the tail, before the head.
* If this is an empty node, set this node as head.
* I strongly suggest that this node is a single node which means that you cannot get other nodes from this node.
* Next and before of this node will be reset to apply this linearList, unless the linearList is empty.
* @tparam T
* @param node
* @return
*/
template <class T>
bool DoubleLinkedLinearList<T>::add(DNode<T> *node) {
DNode<T>* tail = getTailNode();
if (tail == nullptr){
setHead(node);
return true;
}
node->setNextNode(getHead());
node->setBefore(tail);
getHead()->setBefore(node);
tail->setNextNode(node);
return true;
}
/**
* If headNode -> next -> before is not itself, there must be a circle
* @tparam T
* @return
*/
template <class T>
bool DoubleLinkedLinearList<T>::circleJudge() const {
DNode<T>* head = getHead();
return head != nullptr && head->getBefore()->getNextNode() != head;
}
template <class T>
DNode<T> *DoubleLinkedLinearList<T>::getTailNode() const {
//Empty
if (getHead() == nullptr){
return nullptr;
}
DNode<T>* node1 = getHead();
//Normally situation, no circle
if (node1->getBefore()->getNextNode() == node1){
while (node1->getNextNode() != getHead() && node1->getNextNode() != nullptr){
node1 = node1->getNextNode();
}
} else{
// circle occur
// if a node -> next -> before is not himself, the node is the tail
while( node1->getNextNode()->getBefore() != node1 ){
node1 = node1->getNextNode();
}
}
return node1;
}
/**
* print information
*/
template <class T>
void DoubleLinkedLinearList<T>::info() const {
cout << "\n --- now print info --- \n";
cout << " - size : " << this->getSize() << endl;
if (this->getSize() == 0){
return;
}
cout << " - head : " << this->getHead()->getDataNode() << endl;
cout << " - circle: " << this->circleJudge()<< endl;
cout << " - tail : " << this->getTailNode()->getDataNode() << endl;
for (int i = 0; i < getSize(); ++i) {
const int j = i;
//FIXME
cout << " - index = " << i<< " DNode's value is " << getNode(j)->getDataNode() << endl;
}
}
#endif //LINEARLIST_DOUBLELINKEDLINEARLST_H