-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLinkList.h
247 lines (240 loc) · 5.08 KB
/
DLinkList.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
#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
// 节点类
template<typename T>
class Node {
private:
T data;
Node<T>* pre;
Node<T>* next;
public:
// 构造析构
Node(Node<T>* next = nullptr, Node<T>* pre = nullptr) {
this->next = next;
this->pre = pre;
}
Node(const T& data, Node<T>* next = nullptr, Node<T>* pre = nullptr) {
this->data = data;
this->next = next;
this->pre = pre;
}
// 节点操作
Node<T>* getNext() {
return this->next;
}
Node<T>* getPre() {
return this->pre;
}
T getData() {
return this->data;
}
void setNext(Node<T>* next) {
this->next = next;
}
void setPre(Node<T>* pre) {
this->pre = pre;
}
void setData(const T& item) {
this->data = item;
}
// 运算符重载
bool operator==(Node<T>& obj) {
if (this->getData() == obj.getData()) {
return true;
}
else {
return false;
}
}
bool operator!=(Node<T>& obj) {
if (this->getData() == obj.getData()) {
return false;
}
else {
return true;
}
}
friend ostream& operator<< (ostream& out, Node<T>& t) {
out << t.getData();
return out;
}
friend istream& operator>> (istream& in, Node<T>& t) {
T temp;
cin >> temp;
t.setData(temp);
return in;
}
Node<T>& operator=(Node<T>& obj) {
this->setData(obj.data);
this->next = obj.next;
this->pre = obj.pre;
return *this;
}
};
// 双向链表类
template<typename T>
class DLinkList {
private:
Node<T>* head;
Node<T>* tail;
public:
// 构造析构
DLinkList() {
head = new Node<T>;
tail = new Node<T>;
head->setNext(tail);
tail->setPre(head);
}
DLinkList(const T& data) {
this->head = new Node<T>;
this->tail = new Node<T>;
this->head->setNext(new Node<T>(data));
tail->setPre(head->getNext());
}
DLinkList(const DLinkList<T>& list) {
// 创建头尾节点
head = new Node<T>;
tail = new Node<T>;
head->setNext(tail);
tail->setPre(head);
// 复制list中的所有节点
Node<T>* temp = list.head->getNext();
while (temp != list.tail) {
this->addNode(temp->getData());
temp = temp->getNext();
}
}
~DLinkList() {
this->clearList();
delete this->head;
delete this->tail;
this->head = nullptr;
this->tail = nullptr;
}
// 链表操作
Node<T>* getHead() {
return this->head;
}
Node<T>* getTail() {
return this->tail;
}
bool addNode(T data) {
Node<T>* newNode = new Node<T>(data);
Node<T>* temp = head;
while (temp->getNext() != tail) {
temp = temp->getNext();
}
temp->setNext(newNode);
newNode->setPre(temp);
newNode->setNext(tail);
tail->setPre(newNode);
return true;
}
void printList() {
Node<T>* temp = head;
while (temp->getNext() != tail) {
cout << temp->getNext()->getData() << endl;
temp = temp->getNext();
}
return;
}
void rprintList() {
Node<T>* temp = tail;
while (temp->getPre() != head) {
cout << temp->getPre()->getData() << endl;
temp = temp->getPre();
}
return;
}
bool deleteNode(T data) {
Node<T>* temp = tail;
while (temp->getPre() != head) {
if (temp->getPre()->getData() == data) {
Node<T>* toDelete = temp->getPre();
toDelete->getPre()->setNext(toDelete->getNext());
toDelete->getNext()->setPre(toDelete->getPre());
delete toDelete;
toDelete = nullptr;
return true;
}
temp = temp->getPre();
}
return false;
}
bool modifyNode(T from, T to) {
Node<T>* temp = tail;
while (temp->getPre() != head) {
if (temp->getPre()->getData() == from) {
temp->getPre()->setData(to);
return true;
}
temp = temp->getPre();
}
return false;
}
bool searchNode(T data) {
Node<T>* temp = tail;
while (temp->getPre() != head) {
if (temp->getPre()->getData() == data) {
return true;
}
temp = temp->getPre();
}
return false;
}
void clearList() {
Node<T>* toDelete = head->getNext();
// 遍历链表删除所有节点
while (toDelete != this->tail) {
Node<T>* nextNode = toDelete->getNext();
delete toDelete;
toDelete = nextNode;
}
this->head->setNext(this->tail);
this->tail->setPre(this->head);
}
// 运算符重载
bool operator==(DLinkList<T>& obj) {
if (this->head == obj.getHead() && this->tail == obj.tail) {
return true;
}
else {
return false;
}
}
bool operator!=(DLinkList<T>& obj) {
if (this->head == obj.getHead() && this->tail == obj.tail) {
return false;
}
else {
return true;
}
}
friend ostream& operator<< (ostream& out, DLinkList<T>& t) {
Node<T>* temp = t.getHead();
if (temp->getNext() == t.getTail()) {
return out;
}
while (temp->getNext() != t.getTail()) {
out << temp->getNext()->getData() << endl;
temp = temp->getNext();
}
return out;
}
DLinkList<T>& operator=(const DLinkList<T>& list) {
if (this != &list) {
// 先清空当前链表
this->clearList();
// 复制list中的所有节点
Node<T>* temp = list.head->getNext();
while (temp != list.tail) {
this->addNode(temp->getData());
temp = temp->getNext();
}
}
return *this;
}
};