您的位置:首页 > 其它

链表之节点插入、查找删除、遍历打印、遍历释放

2018-01-13 12:13 561 查看
1 /* FILE: p40_linkOperate_main.c */
2
3 #include "p140_common.h"
4
5 int main(int argc, char *argv[])
6 {
7         struct node *p;
8         int i;
9
10         printf("insert\n");
11         for(i=1; i<8; i++)
12                 insert(i);
13         print();
14
15         printf("delete\n");
16         del(5, &(p));
17         printf("the being deleted node: %d\n", p->val);
18         print();
19
20         printf("destroy\n");
21         destroy();
22
23         return 0;
24 }
1 /* FILE: p140_common.h */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 struct node{
7         int val;
8         struct node *next;
9 };
10
11 extern struct node *head;
12
13 extern int insert(int val);
14 extern int del(int val, struct node **res);
15 extern void print();
16 extern void destroy();
1 /* FILE: p140_linkOperate_list.c */
2
3 #include "p140_common.h"
4
5 struct node *head;
6
7 int insert(int val)
8 {
9         struct node *p, *q;
10
11         p = head;
12         q = (struct node *)malloc(sizeof(struct node));
13         if(q == NULL)
14                 return -1;
15         q->val = val;
16         q->next = NULL;
17
18         if(p == NULL)
19         {
20                 head = q;
21         }
22         else
23         {
24                 while(p->next != NULL)
25                         p = p->next;
26                 p->next = q;
27         }
28         return 0;
29 }
30
31 /* 形参:要删除节点的数值;修改二级指针指向被删除的节点 */
32 int del(int val, struct node **res)
33 {
34         struct node *p, *q;
35
36         p = head;
37         if(p == NULL)
38                 return -1;
39         if(p->val == val)
40         {
41                 *res = p;
42                 head = p->next;
43                 return 0;
44         }
45         else if(p->next == NULL)
46                 return -1;
47         q = p;
48         p = p->next;
49         while(p != NULL)
50         {
51                 if(p->val != val)
52                 {
53                         q = p;
54                         p = p->next;
55                 }
56                 else
57                 {
58                         *res = p;
59                         q->next = p->next;
60                         p->next = NULL;
61                         return 1;
62                 }
63         }
64         return -1;
65 }
66
67 void print()
68 {
69         struct node *p;
70         p = head;
71         while(p!=NULL)
72         {
73                 printf("%d ", p->val);
74                 p = p->next;
75         }
76         printf("\n");
77 }
78
79 void destroy()
80 {
81         struct node *p = head;
82         while(p != NULL)
83         {
84                 struct node *q;
85                 q = p;
86                 p = p->next;
87                 free(q);
88         }
89         head = NULL;
90 }
1 # FILE: Makefile
2
3 OBJECTS = p140_linkOperate_list.o p140_linkOperate_main.o
4
5 build: $(OBJECTS)
6         gcc -o build $(OBJECTS)
7 p140_linkOperate_list.o: p140_linkOperate_list.c p140_common.h
8         gcc -c p140_linkOperate_list.c
9 p140_linkOperate_main.o: p140_linkOperate_main.c p140_common.h
10         gcc -c p140_linkOperate_main.c
11
12 .PHONY: clean
13 clean:
14         rm build $(OBJECTS)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐