基础数据结构_循环单链表

声明

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
#ifndef CLIST_H
#define CLIST_H
typedef int ElemType;
typedef struct CNode
{
ElemType data;
struct CNode* next;//指针域
}CNode, * PCNode;
//可执行操作
//初始化
void Init_clist(struct CNode* plist);
//头插
bool Push_Front(PCNode plist, int val);
//尾插
bool Push_Back(PCNode plist, int val);
//按位置插
bool Insert_Pos(PCNode plist, int pos, int val);
//头删
bool Pop_Front(PCNode plist);
//尾删
bool Pop_Back(PCNode plist);
//按位置删
bool Del_Pos(PCNode plist, int pos);
//按值删除
bool Del_Val(PCNode plist, int val);
//查找
struct CNode* Search(PCNode plist, int val);
//找值为val的节点前驱
struct CNode* Get_Prev(PCNode plist, int val);
//找值为val的节点后继
struct CNode* Get_Next(PCNode plist, int val);
//获取其有效节点的个数
int Get_Length(PCNode plist);
//清空
void Clear(PCNode plist);
//销毁1
void Destroy1(PCNode plist);
//销毁2
void Destroy2(PCNode plist);
//判空
bool IsEmpty(PCNode plist);
//打印
void Show(PCNode plist);
#endif


实现

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
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"clist.h"

struct CNode* Buynode()
{
CNode* newnode = (CNode*)malloc(sizeof(CNode));
if (NULL == newnode)
{
printf("memory error");
exit(1);
}
return newnode;
}
//可执行操作
//初始化
void Init_clist(struct CNode* head)
{
assert(head != NULL);
head->next = head;
}
//查到ptr下一个
bool Insert_Next(CNode* ptr, int val)
{
assert(ptr);
CNode* pnewnode = Buynode();
pnewnode->next = ptr->next;
ptr -> next = pnewnode;
pnewnode->data = val;
return true;
}
//头插
bool Push_Front(PCNode head, int val)
{
assert(head);
return Insert_Next(head, val);
}
//尾插
bool Push_Back(PCNode head, int val)
{
assert(head);
CNode* p = head;
while (p->next != head)
{
p = p->next;
}
return Insert_Next(p, val);
}
//按位置插
bool Insert_Pos(PCNode head, int pos, int val)
{
assert(head);
if (pos < 0)return false;
CNode* pnode = head;
while (pnode->next != head && pos--)
{
pnode = pnode->next;
}
if (pos != 0)
{
return false;
}
return Insert_Next(pnode, val);
}
//删除下一个
bool Del_Next(CNode* ptr)
{
assert(ptr);
CNode* p = ptr->next;
ptr->next = p->next;
free(p);
return true;
}
//头删
bool Pop_Front(PCNode head)
{
assert(head);
if (head == head->next)//只有头结点不行
{
return false;
}
return Del_Next(head);
}
//尾删
bool Pop_Back(PCNode head)
{
assert(head);
CNode* p = head;
if (head == p->next)//只有头结点不行
{
return false;
}
while (p->next->next != head)
{
p = p->next;
}
return Del_Next(p);
}
//按位置删
bool Del_Pos(PCNode head, int pos)
{
assert(head);
if (pos < 0)return false;
CNode* p = head;
if (NULL == p->next)return false;
int count = pos - 1;
while (p->next != head && count--)
{
p = p->next;
}
if (count != -1)
{
return false;
}
return Del_Next(p);
}
//按值删除
bool Del_Val(PCNode head, int val)
{
assert(head);
CNode* p = head;
if (NULL == p->next)return false;
while (p->next != head && val != p->next->data)
{
p = p->next;
}
if (p->next == head)
{
return false;
}
return Del_Next(p);
}
//查找
struct CNode* Search(PCNode plist, int val);
//找值为val的节点前驱
struct CNode* Get_Prev(PCNode plist, int val);
//找值为val的节点后继
struct CNode* Get_Next(PCNode plist, int val);
//获取其有效节点的个数
int Get_Length(PCNode head)
{
assert(head);
CNode* p = head;
int count = 0;
while (p->next != head)
{
count++;
}
return count;
}
//判空
bool IsEmpty(PCNode head)
{
assert(head);
if (head->next == head)return true;
return false;
}
//清空
void Clear(PCNode head)
{
assert(head);
while (!IsEmpty(head))
{
Pop_Front(head);
}
}
//销毁1
void Destroy1(PCNode head)
{
assert(head);
Clear(head);
}
//销毁2
void Destroy2(PCNode plist);
//打印
void Show(PCNode head)
{
assert(head);
CNode* p = head->next;
while (p != head)
{
printf("%3d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
CNode cn;
Init_clist(&cn);
for (int i = 0;i < 10;i++)
{
Insert_Pos(&cn, i,i+1);

}
Show(&cn);
Push_Front(&cn, 100);
Push_Back(&cn, 200);
Show(&cn);

Pop_Front(&cn);
Pop_Back(&cn);
Show(&cn);

Del_Val(&cn, 6);
Show(&cn);
Del_Pos(&cn, 6);
Show(&cn);
Destroy1(&cn);
Show(&cn);
}