1 /**********************链表笔记**********************/
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 typedef struct node
6 {
7 int data;
8 struct node *next;
9 struct node *prior;//双向链表的前驱结点
10 }LINK;
11
12 int main(void)
13 {
14 /**************定义********************/
15
16 int x;//用户输入
17 int j;//统计结点数
18 int num;//节点序号(结点序号从0开始,head是第0号结点)
19 LINK *p, *q, *r, *rB;//指向某结点
20
21 LINK *head = (LINK *)malloc(sizeof(LINK));
22 LINK *headB = (LINK *)malloc(sizeof(LINK));
23 LINK *headC = (LINK *)malloc(sizeof(LINK));//循环单链表
24 head->next = NULL;
25 head->prior = NULL;
26
27 /**************头插法*******************/
28
29 /*scanf("%d",&x);
30 while(x != -1)
31 {
32 p = (LINK *)malloc(sizeof(LINK));
33 p->data = x;
34 p->next = head->next;
35 head->next = p;
36 scanf("%d",&x);
37 }*/
38
39 /*************尾插法*********************/
40
41 r = head;
42 printf("请输入元素:");
43 scanf("%d", &x);
44 while(x != -1)
45 {
46 p = (LINK *)malloc(sizeof(LINK));
47 p->data = x;
48 r->next = p;//r是上一个结点,p是新的结点
49 r = p;//让新结点变成尾结点
50 scanf("%d", &x);
51 }
52 r->next = NULL;
53
54 /*************打印链表************/
55
56 p = head;
57 printf("head");
58 while(p->next != NULL)
59 {
60 p = p->next;
61 printf("->%d", p->data);
62 }
63
64 /**************求表长*************/
65 /*
66 p = head;
67 j = 0;
68 while(p->next != NULL)
69 {
70 p = p->next;
71 j++;
72 }
73 printf("\n表长为:%d\n", j);
74 */
75 /***********按序号查找*************/
76 /*
77 j = 0;
78 printf("请输入查找的结点序号:");
79 scanf("%d",&num);
80 p = head;
81 while(p->next != NULL && j < num)
82 {
83 p = p->next;
84 j++;
85 }
86 if (j == num)
87 {
88 printf("第%d个结点为%d\n", num, *p);
89 }
90 else
91 {
92 printf("该结点不存在\n");
93 }
94 */
95 /****************按值查找****************/
96 /*
97 printf("请输入查找的值:");
98 scanf("%d", &x);
99 while(p->next != NULL && p->data != x)
100 {
101 p = p->next;
102 }
103 if(p->data == x)
104 {
105 printf("%d\n", p->data);
106 }
107 else
108 {
109 printf("表中不存在值为%d的结点\n", x);
110 }
111 */
112 /*****************插入*******************/
113 /*
114 p = head;
115 j = 0;
116 printf("\n请输入插入结点的位置和值(逗号分隔):");
117 scanf("%d,%d", &num, &x);
118
119 while(p->next != NULL && j != (num - 1))
120 {
121 p = p->next;
122 j++;
123 }
124 if (p->next == NULL)
125 {
126 printf("不存在该结点位置\n");
127 }
128 else
129 {
130 q = (LINK *)malloc(sizeof(LINK));
131 q->data = x;
132 q->next = p->next;
133 p->next = q;
134 }
135
136 p = head;
137 printf("head");
138 while(p->next != NULL)
139 {
140 p = p->next;
141 printf("->%d", p->data);
142 }
143 */
144 /***************删除*****************/
145 /*
146 p = head;
147 j = 0;
148 printf("\n请输入删除的结点序号:");
149 scanf("%d", &num);
150
151 while(p->next != NULL && (j < (num - 2)))
152 {
153 p = p->next;
154 j++;
155 }
156 if (p->next == NULL)
157 {
158 printf("第%d个结点不存在\n", num - 1);
159 }
160 else
161 {
162 p = p->next;//定位到删除结点的前一个结点
163 }
164
165 if(p->next == NULL)
166 {
167 printf("第%d个结点不存在\n", num);
168 }
169
170 q = p->next;//q是待删除结点
171 if(q->next == NULL)//若q是尾结点
172 {
173 p->next = NULL;
174 free(q);
175 }
176 else
177 {
178 p->next = q->next;
179 free(q);
180 }
181
182 p = head;//打印删除后链表
183 printf("head");
184 while(p->next != NULL)
185 {
186 p = p->next;
187 printf("->%d", p->data);
188 }
189 */
190 /****************逆置****************/
191 /*
192 p = head->next;
193 head->next = NULL;//原链表置空
194
195 while (p)
196 {
197 q = p;
198 p = p->next;
199 q->next = head->next;
200 head->next = q;
201 }
202
203 p = head;
204 printf("\nhead");
205 while(p->next != NULL)
206 {
207 p = p->next;
208 printf("->%d", p->data);
209 }
210 */
211 /***************删除重复结点****************/
212 /*
213 p = head->next;
214 if (p == NULL)
215 {
216 printf("\n空链表");
217 }
218 else
219 {
220 while (p->next)
221 {
222 q = p;
223 while (q->next)
224 {
225 if (q->next->data == p->data)
226 {
227 r = q->next;
228 q->next = r->next;
229 free(r);
230 }
231 else
232 {
233 q = q->next;
234 }
235 }
236 p = p->next;
237 }
238 }
239
240 p = head;
241 printf("\nhead");
242 while (p->next != NULL)
243 {
244 p = p->next;
245 printf("->%d", p->data);
246 }
247 */
248 /****************两个集合的差集****************/
249 /*
250 r = headB;//尾插法输入链表B元素
251 printf("\n请输入集合B的元素:");
252 scanf("%d", &x);
253 while (x != -1)
254 {
255 p = (LINK *)malloc(sizeof(LINK));
256 p->data = x;
257 p->next = r->next;
258 r->next = p;
259 r = p;
260 scanf("%d", &x);
261 }
262 r->next = NULL;
263
264 p = headB;//打印链表B
265 printf("headB");
266 while (p->next)
267 {
268 p = p->next;
269 printf("->%d", p->data);
270 }
271
272 q = headB;//差集
273 while (q->next)
274 {
275 q = q->next;
276 p = head;
277 while (p->next)
278 {
279 if (p->next->data == q->data)
280 {
281 r = p->next;
282 p->next = r->next;
283 free(r);
284 }
285 else
286 {
287 p = p->next;
288 }
289 }
290 }
291
292 p = head;//打印差集后链表
293 printf("\nhead");
294 while (p->next)
295 {
296 p = p->next;
297 printf("->%d", p->data);
298 }
299 */
300 /********************循环单链表******************/
301 /*
302 r = headC;
303 printf("\n请输入循环单链表元素:");
304 scanf("%d", &x);
305
306 while (x != -1)
307 {
308 p = (LINK *)malloc(sizeof(LINK));
309 p->data = x;
310 p->next = r->next;
311 r->next = p;
312 r = p;
313 scanf("%d", &x);
314 }
315 r->next = headC;
316
317 p = headC->next;//打印循环单链表
318 printf("\nheadC");
319 while (p != headC)
320 {
321 printf("->%d",p->data);
322 p = p->next;
323 }
324 */
325 /****************连接两个循环单链表****************/
326 /*
327 r = head;//循环单链表
328 printf("\n请输入循环单链表元素:");
329 scanf("%d", &x);
330 while(x != -1)
331 {
332 p = (LINK *)malloc(sizeof(LINK));
333 p->data = x;
334 p->next = r->next;
335 r->next = p;
336 r = p;
337 scanf("%d",&x);
338 }
339 r->next = head;
340
341 rB = headB;//循环单链表B
342 printf("\n请输入循环单链表B元素:");
343 scanf("%d", &x);
344 while(x != -1)
345 {
346 p = (LINK *)malloc(sizeof(LINK));
347 p->data = x;
348 p->next = rB->next;
349 rB->next = p;
350 rB = p;
351 scanf("%d",&x);
352 }
353 rB->next = headB;
354
355 r->next = headB->next;//连接
356 rB->next = head;
357
358 p = head;//打印
359 printf("head");
360 while(p->next != head)
361 {
362 p = p->next;
363 printf("->%d", p->data);
364 }
365 */
366 /***************双向循环链表*******************/
367 /*
368 r = head;
369 printf("\n请输入双向循环链表的元素:");
370 scanf("%d", &x);
371 while(x != -1)
372 {
373 p = (LINK *)malloc(sizeof(LINK));
374 p->data = x;
375 p->next = r->next;
376 p->prior = r;
377 r->next = p;
378 r = p;
379 scanf("%d", &x);
380 }
381 r->next = head;
382
383 p = head->next;//打印
384 printf("head");
385 while (p != head)
386 {
387 printf("->%d", p->data);
388 p = p->next;
389 }
390
391 p = r;//倒序打印
392 printf("\nrear");
393 while (p != head)
394 {
395 printf("->%d", p->data);
396 p = p->prior;
397 }
398 */
399 /**************************************/
400 return 0;
401 }