-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkList.cpp
More file actions
221 lines (219 loc) · 8.62 KB
/
LinkList.cpp
File metadata and controls
221 lines (219 loc) · 8.62 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
//
// Created by Donghui Zheng on 2020/12/21 11:10.
//
#include "LinkList.h"
node * linkList::returnHeadPoint() {
return headPoint;
}
int & linkList::returnLinkListLength() {
return linkListLength;
}
int & linkList::returnLinkListFlag() {
return flag;
}
int linkList::linkListSize() const {
return this->linkListLength;//直接返回linkListLength成员.
}
int linkList::linkListFlag() const {
return this->flag;//直接返回flag成员.
}
linkList::linkList() {
node *headNode = new node();//创建头节点.
headNode->prev = headNode;//头节点前驱指向自己.
headNode->next = headNode;//头节点后继指向自己.
headPoint = headNode;//头指针指向头节点.
linkListLength = 0;//双向链表长度为0.
flag = 0;//正负标记设置为0.
}
linkList::linkList(const linkList &object) {
if (object.linkListSize() == 0) {//双向链表为空链表,链表中仅有头节点.
node *headNode = new node();//创建头节点.
headNode->prev = headNode;//头节点前驱指向自己.
headNode->next = headNode;//头节点后继指向自己.
headPoint = headNode;//头指针指向头节点.
linkListLength = 0;//双向链表长度为0.
flag = 0;//正负标记设置为0.
} else {//双向链表非空.
//头节点的建立.
node *headNode = new node();
headNode->prev = headNode;
headNode->next = headNode;
headPoint = headNode;
linkListLength = 0;
node *tempPoint = object.headPoint->next;//第一个节点.
/**
* frontNode : 指向第一个节点的指针.
* behindNode : 记录目前的链的最后一个节点.
* newNode : 新创建的待加入的指针.
*/
node *frontNode = new node();//指向第一个节点的指针,第一个节点的深拷贝.
frontNode->data = tempPoint->data;
node *behindNode = frontNode;//记录目前的链的最后一个节点.
tempPoint = tempPoint->next;
int size = object.linkListSize();//链表长度,即应当复制的节点数量.
int index = 1;//已复制的节点数量.
while (index < size) {
node *newNode = new node();//复制新节点.
behindNode->next = newNode;
newNode->prev = behindNode;
behindNode = behindNode->next;//更新最后一个节点为新节点.
behindNode->data = tempPoint->data;
tempPoint = tempPoint->next;
++index;
}
headNode->next = frontNode;//将新链加到头节点后.
frontNode->prev = headNode;
behindNode->next = headNode;//将最后一个节点指向头节点,构成循环链.
headNode->prev = behindNode;
linkListLength = size;
flag = object.flag;//正负标记设置为object的flag.
}
}
linkList & linkList::operator=(const linkList &rightHandObject) {
if (this == &rightHandObject) {//判断进行自我赋值.
return *this;
} else {
if (rightHandObject.linkListSize() == 0) {//双向链表为空链表,链表中仅有头节点.
node *headNode = new node();//创建头节点.
headNode->prev = headNode;//头节点前驱指向自己.
headNode->next = headNode;//头节点后继指向自己.
headPoint = headNode;//头指针指向头节点.
linkListLength = 0;//双向链表长度为0.
flag = 0;//正负标记设置为0.
} else {//双向链表非空.
//头节点的建立.
node *headNode = new node();
headNode->prev = headNode;
headNode->next = headNode;
headPoint = headNode;
linkListLength = 0;
node *tempPoint = rightHandObject.headPoint->next;//第一个节点.
/**
* frontNode : 指向第一个节点的指针.
* behindNode : 记录目前的链的最后一个节点.
* newNode : 新创建的待加入的指针.
*/
node *frontNode = new node();//指向第一个节点的指针,第一个节点的深拷贝.
frontNode->data = tempPoint->data;
node *behindNode = frontNode;//记录目前的链的最后一个节点.
tempPoint = tempPoint->next;
int size = rightHandObject.linkListSize();//链表长度,即应当复制的节点数量.
int index = 1;//已复制的节点数量.
while (index < size) {
node *newNode = new node();//复制新节点.
behindNode->next = newNode;
newNode->prev = behindNode;
behindNode = behindNode->next;//更新最后一个节点为新节点.
behindNode->data = tempPoint->data;
tempPoint = tempPoint->next;
++index;
}
headNode->next = frontNode;//将新链加到头节点后.
frontNode->prev = headNode;
behindNode->next = headNode;//将最后一个节点指向头节点,构成循环链.
headNode->prev = behindNode;
linkListLength = size;
flag = rightHandObject.flag;//正负标记设置为rightHandObject的flag.
}
return *this;
}
}
linkList::~linkList() {
node *tempPoint = headPoint;
for (int i = 0; i <= this->linkListLength; ++i) {
node *deletePoint = tempPoint;
tempPoint = tempPoint->next;
delete deletePoint;
}
}
void linkList::display(ostream &out) {
if (flag == -1) {
out << '-';
}
node *tempPoint = headPoint->next;
for (int i = 0; i < this->linkListLength; ++i) {//依个遍历.
out << tempPoint->data;
tempPoint = tempPoint->next;
}
cout << endl << "digit:" << linkListLength;
}
void linkList::createLinkListWithoutForValue(int length) {
//头节点的建立.
node *headNode = new node();
headNode->prev = headNode;
headNode->next = headNode;
headPoint = headNode;
linkListLength = 0;
node *frontNode = new node();//指向第一个节点的指针,第一个节点的创建.
node *behindNode = frontNode;//记录目前的链的最后一个节点.
int number = 1;//已创建的新节点数量.
while (number < length) {
node *newNode = new node();
behindNode->next = newNode;
newNode->prev = behindNode;
behindNode = behindNode->next;
++number;
}
headNode->next = frontNode;//头指针指向第一个节点.
frontNode->prev = headNode;
behindNode->next = headNode;//将最后一个节点指向头节点,构成循环链.
headNode->prev = behindNode;
linkListLength = length;
flag = 0;//正负标记设置为0.
}
void linkList::createLinkListWithForValue(int length) {
//头节点的建立.
node *headNode = new node(0, nullptr, nullptr);
headNode->prev = headNode;
headNode->next = headNode;
headPoint = headNode;
linkListLength = 0;
node *frontNode = new node(0, nullptr, nullptr);//指向第一个节点的指针,第一个节点的创建.
node *behindNode = frontNode;//记录目前的链的最后一个节点.
int number = 1;//已创建的新节点数量.
while (number < length) {
node *newNode = new node(0, nullptr, nullptr);
behindNode->next = newNode;
newNode->prev = behindNode;
behindNode = behindNode->next;
++number;
}
headNode->next = frontNode;//头指针指向第一个节点.
frontNode->prev = headNode;
behindNode->next = headNode;//将最后一个节点指向头节点,构成循环链.
headNode->prev = behindNode;
linkListLength = length;
flag = 0;//正负标记设置为0.
}
void linkList::insert(int item, int position) {
node *newPoint = new node(item, nullptr);//待插入的新节点.
node *prePoint = headPoint;
node *point = prePoint->next;
for (int i = 0; i < position; ++i) {
prePoint = prePoint->next;
point = point->next;
}
newPoint->next = prePoint->next;
prePoint->next = newPoint;
point->prev = newPoint;
newPoint->prev = prePoint;
linkListLength += 1;//长度自增1.
}
void linkList::erase(int position) {
node *prePoint = headPoint;
node *point = prePoint->next;
node *behindPoint = point->next;
for (int i = 1; i < position; ++i) {
prePoint = prePoint->next;//位置前.
point = point->next;//删除节点的位置.
behindPoint = behindPoint->next;//位置后.
}
prePoint->next = behindPoint;
behindPoint->prev = prePoint;
delete point;
linkListLength -= 1;//长度自减1.
}
ostream & operator<<(ostream &out, linkList &printList) {
printList.display(out);//调用display函数.
return out;
}