-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularLinkList.cpp
277 lines (246 loc) · 6.44 KB
/
CircularLinkList.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include "LinkList.h"
#include "CircularLinkList.h"
void circularLinkListDemo()
{
int data[6] = {4, 5, 6, 7, 8, 9};
int data2[3] = {1, 2, 3};
LinkNode* L = NULL;
LinkNode* Lb = NULL;
LinkNode* LNoneHead = NULL;
createCircularList(L, 6, data);
createCircularList(Lb, 3, data2);
// createCircularListNoneHead(LNoneHead, data, 6);
// displayCircularListNoneHead(LNoneHead);
// displayCircularList(L);
// reverseCircularList(L);
// leftMove(LNoneHead, 7);
// deleteNodePre(LNoneHead->next->next);
// dividedToThree(L);
// reverseFromItoM(L, 3, 6);
// mergeCircularList(Lb, 3, L, 6);
createCircularListTail(L, 6, data);
createCircularListTail(Lb, 3, data2);
//displayCircularListTail(L);
mergeCircularListTai(L, Lb);
}
void createCircularList(LinkList &L, int length, int* data)
{
L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = L;
LinkNode* pTail = L;
for(int i = 0; i < length; i++)
{
LinkNode* t = (LinkNode*)malloc(sizeof(LinkNode));
t->data = data[i];
t->next = pTail->next;
pTail->next = t;
pTail = t;
}
}
// 删除节点p的直接前驱
void deleteNodePre(LinkNode* p)
{
LinkNode* pCurrent = p->next;
LinkNode* pPre = p;
while(pCurrent->next != p) {
pPre = pCurrent;
pCurrent = pCurrent->next;
}
// 删除操作
pPre->next = pCurrent->next;
free(pCurrent);
displayCircularListNoneHead(p);
}
// 创建不带头结点的链表
void createCircularListNoneHead(LinkList &L, int* data, int length)
{
LinkNode* pTail = (LinkNode*)malloc(sizeof(LinkNode));
pTail->data = data[0];
pTail->next = pTail;
L = pTail;
for(int i = 1; i < length; i++) {
LinkNode* t = (LinkNode*) malloc(sizeof(LinkNode));
t->data = data[i];
t->next = pTail->next;
pTail->next = t;
pTail = t;
}
}
// 节点链接方向逆转
void reverseCircularList(LinkList L)
{
// 1 -> 2 -> 3 -> 4
// 1 <- 2 <- 3 <- 4
LinkNode* pCurrent = L->next;
L->next = L; // 单链表初始的时候指向NULL,循环链表之指向本身
while(pCurrent != L) {
// 头插法插入节点
LinkNode* t = pCurrent;
pCurrent = pCurrent->next;
t->next = L->next;
L->next = t;
}
displayCircularList(L);
}
// 单链表左移 k 个单位(不带头结点)
int leftMove(LinkList &L, int k)
{
if(!L) return 0;
LinkNode* pCurrent = L;
for(int i=0; i < k; i++) {
pCurrent = pCurrent->next;
}
L = pCurrent;
displayCircularListNoneHead(L);
}
void insertAlp(LinkNode* pNode, LinkList L)
{
pNode->next = L->next;
L->next = pNode;
}
// 字母数字字符
void dividedToThree(LinkList L)
{
LinkNode* pCurrent = L->next;
LinkList LNumber = (LinkList)malloc(sizeof(LinkNode));
LNumber->next = LNumber;
LinkList LAlpha = (LinkList)malloc(sizeof(LinkNode));
LAlpha->next = LAlpha;
LinkList LOther = (LinkList)malloc(sizeof(LinkNode));
LOther->next = LOther;
while(pCurrent != L)
{
LinkNode* t = pCurrent;
pCurrent = pCurrent->next;
if(t->data >= '0' && t->data<='9') {
insertAlp(t, LNumber);
} else if((t->data >='A' && t->data <='Z') ||
(t->data >='a' && t->data<='z')) {
insertAlp(t, LAlpha);
} else {
insertAlp(t, LOther);
}
}
displayCircularList(LNumber);
displayCircularList(LAlpha);
displayCircularList(LOther);
}
// 获取i号节点前驱节点
LinkNode* getPre(LinkList L, int i)
{
int cnt = 1;
LinkNode* pPre = L;
LinkNode* pCurrent = L->next;
while(cnt < i) {
pPre = pCurrent;
pCurrent = pCurrent->next;
cnt++;
}
return pPre;
}
// i 号节点到 m 号节点构成逆置循环链表
void reverseFromItoM(LinkList L, int i, int m)
{
LinkNode* pPreI = getPre(L, i);
LinkNode* pPreM = getPre(L, m);
displayCircularList(L);
LinkNode* LNew = (LinkNode*)malloc(sizeof(LinkNode));
LNew->next = LNew;
LinkNode* pCurrent = pPreI->next;
LinkNode* pLast = pPreM->next;
pPreI->next = pPreM->next->next;
pLast->next = NULL;
while(pCurrent)
{
LinkNode* t = pCurrent;
pCurrent = pCurrent -> next;
t -> next = LNew -> next;
LNew -> next = t;
}
displayCircularList(L);
displayCircularList(LNew);
}
// 将两个循环单链表合并成一个循环单链表(最小事件复杂度)
void mergeCircularList(LinkList La, int m, LinkList Lb, int n)
{
LinkNode* pCurrentA = La -> next;
LinkNode* pCurrentB = Lb -> next;
// 从较小的一条链表中找到尾节点,插入到另外一条链表的头结点
if(m < n) {
while(pCurrentA->next != La) {
pCurrentA = pCurrentA->next;
}
// 需要删除头节点
LinkNode* t = pCurrentA->next;
pCurrentA->next = Lb->next;
Lb->next = t->next;
free(t);
displayCircularList(Lb);
} else {
while(pCurrentB->next != Lb ) {
pCurrentB = pCurrentB -> next;
}
LinkNode* t = pCurrentB->next;
pCurrentB->next = La->next;
La->next = t->next;
free(t);
displayCircularList(La);
}
}
// 创建尾指针指向的循环单链表
void createCircularListTail(LinkList &L, int n, int* data)
{
L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = L;
for(int i=0; i < n; i++) {
LinkNode* t = (LinkNode*)malloc(sizeof(LinkNode));
t->data = data[i];
t->next = L->next;
L->next = t;
L = t;
}
}
// 为了合并更加快速,设置单循环链表的尾指针
void mergeCircularListTai(LinkList& La, LinkList& Lb)
{
LinkNode* pHeadLa = La->next;
LinkNode* pHeadLb = Lb->next;
La->next = pHeadLb->next;
Lb->next = pHeadLa;
free(pHeadLb);
La = Lb;
Lb = NULL;
displayCircularListTail(La);
}
void displayCircularListTail(LinkList L)
{
LinkNode* pHead = L->next;
LinkNode* pCurrent = pHead->next;
while(pCurrent != pHead) {
printf("%d ", pCurrent->data);
pCurrent = pCurrent->next;
}
printf("\n");
}
void displayCircularList(LinkList L)
{
LinkNode* pCurrent = L->next;
while(pCurrent != L) {
printf(" %d ", pCurrent->data);
pCurrent = pCurrent->next;
}
printf("\n");
}
void displayCircularListNoneHead(LinkList L)
{
LinkNode* pCurrent = L;
int isHead = 1;
while(isHead || pCurrent != L) {
printf("=>%d ", pCurrent->data);
pCurrent = pCurrent->next;
isHead = 0;
}
printf("\n");
}