数据结构之双向循环链表
实例代码:
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
|
#include <stdlib.h> #include <stdio.h> #include <malloc.h> typedef struct Node{ struct Node *pNext; int data; struct Node *prior; } NODE,*PNODE; PNODE CreatList(); void TreNode(PNODE pHead); bool isEmpty(PNODE pHead); int getCount(PNODE pHead); bool insertNode (PNODE pHead, int pos, int val); bool detNode (PNODE pHead, int pos, int *pVal); void sort(PNODE pHead); int main(){ int val; PNODE pHead = NULL; pHead = CreatList(); TreNode(pHead); printf ( "链表不为空!\n" ); printf ( "链表不为空!\n" ); if (insertNode(pHead, 4, 7)) printf ( "插入成功!插入的元素为:%d\n" , 7); else printf ( "插入失败!\n" ); //重新遍历链表 TreNode(pHead); //删除元素测试 if (detNode(pHead, 3, &val)) printf ( "元素删除成功!删除的元素是:%d\n" , val); else printf ( "元素删除失败!\n" ); TreNode(pHead); return 0 ; } PNODE CreatList(){ int i ; int val; int length; PNODE pHead = (PNODE) malloc ( sizeof (NODE)); if (NULL==pHead) { printf ( "创建链表失败!\n" ); exit (-1); } pHead->pNext = NULL; pHead->prior = NULL; PNODE pTail = pHead; printf ( "请输入您想要创建链表结点的个数:len = " ); scanf ( "%d" , &length); for (i = 0;i < length;i++){ printf ( "请输入第%d个结点的值:" , i+1); scanf ( "%d" , &val); PNODE pnew = (PNODE) malloc ( sizeof (NODE)); if (NULL==pHead) { printf ( "创建链表失败!\n" ); exit (-1); } pnew->data = val; // pTail->pNext = pnew; pTail->pNext = pnew; pnew->prior = pTail; pnew->pNext = pHead; pHead->prior = pnew; pTail = pnew; } return pHead; } void TreNode(PNODE pHead){ PNODE p = pHead->pNext; while (p!= pHead ){ printf ( "%d" ,p->data); p = p->pNext; } printf ( "\n" ); return ; } bool isEmpty(PNODE pHead){ if (NULL==pHead->pNext) return true ; else return false ; } int getCount(PNODE pHead) { int i =0 ; PNODE p = pHead->pNext; while (NULL != p){ p = p->pNext; i++; } return i ; } bool insertNode (PNODE pHead, int pos, int val){ int i = 0; // PNODE p = pHead; PNODE p =pHead; while (NULL !=p && i<pos-1){ p = p->pNext; i++; } if (NULL == p || i>pos-1) { return false ; } scanf ( "%d" , &val); PNODE pNew = (PNODE) malloc ( sizeof (NODE)); if (NULL==pNew) { printf ( "创建链表失败!\n" ); exit (-1); } pNew ->data = val; PNODE q = p->pNext; p->pNext = pNew; pNew->prior = p; pNew->pNext = q; return true ; } bool detNode (PNODE pHead, int pos, int *pVal){ int i = 0; // PNODE p = pHead; PNODE p = pHead; while (NULL !=p && i<pos-1){ p = p->pNext; i++; } if (NULL == p || i>pos-1) { return false ; } *pVal = p->pNext->data; PNODE q = p->pNext; p->pNext = p->pNext->pNext; free (q); q = NULL; return true ; } void sort(PNODE pHead){ int i,j,k,t; k = getCount(pHead); PNODE p,q; for (i = 0,p=pHead->pNext;i < k-1;i++,p = p->pNext) { for (j = i+1,q = p->pNext;j<k;j++,q = q->pNext){ if (p->data > q->data ){ t = p->data; p->data = q->data; q->data = t; } } } } |
实现效果:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!