本文实例为大家分享了C链表操作演示的具体代码,供大家参考,具体内容如下
头文件:link_0505.h
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
|
/* 链表演示 */ #ifndef __LINK_0505 #define __LINK_0505 typedef struct node{ int num; struct node* p_next; }node; typedef struct { node head,tail; }link; //链表的初始化函数 void link_init(link *); //链表的清理函数 void link_deinit(link *); //判断链表是否空的函数 int link_empty(link *); //判断链表是否满的函数 int link_full(link *); //统计有效数字个数的函数 int link_size(link *); //在最前边插入数字的函数 int link_add_head(link *, int ); //在最后边插入新的数字的函数 int link_append(link *, int ); //把数字按照顺序插入到链表的函数 int link_insert(link *, int ); //删除最前面数字的函数 int link_remove_head(link *); //删除最后一个有效数字 int link_remove_tail(link *); //删除某个给定数字的函数 int link_remove(link *, int ); //获得第一个有效数字的函数 int link_get_head(link *, int *); //获得最后一个有效数字的函数 int link_get_tail(link *, int *); //获得指定编号数字的函数 int link_get(link *, int *, int ); #endif |
实现代码: link_0505.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
|
/* 链表演示 */ #include "stdlib.h" #include "link_0505.h" //链表的初始化函数 void link_init(link *p_link) { p_link->head.p_next = &(p_link->tail); } //链表的清理函数 void link_deinit(link *p_link) { while (p_link->head.p_next != &(p_link->tail)) { node *p_first = &(p_link->head); node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; p_first->p_next = p_last; free (p_mid); p_mid = NULL; } } //判断链表是否空的函数 int link_empty(link *p_link) { return p_link->head.p_next == &(p_link->tail); } //判断链表是否满的函数 int link_full(link *p_link) { return 0; } //统计有效数字个数的函数 int link_size(link *p_link) { int cnt = 0; node *p_node = NULL; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_mid != &(p_link->tail)) { cnt++; } } return cnt; } //在最前边插入数字的函数 int link_add_head(link *p_link, int num) { node *p_temp = (node *) malloc ( sizeof (node)); if (!p_temp) { return 0; } p_temp->num = num; node *p_first = &(p_link->head); node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; p_first->p_next = p_temp; p_temp->p_next = p_mid; return 1; } //在最后边插入新的数字的函数 int link_append(link *p_link, int num) { node *p_tmp = (node *) malloc ( sizeof (node)); node *p_node = NULL; if (!p_tmp) { return 0; } p_tmp->num = num; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_mid == &(p_link->tail)) { p_first->p_next = p_tmp; p_tmp->p_next = p_mid; break ; } } return 1; } //把数字按照顺序插入到链表的函数 int link_insert(link *p_link, int num) { node* p_temp = (node *) malloc ( sizeof (node)); node* p_node = NULL; if (!p_temp) { return 0; } p_temp->num = num; p_temp->p_next = NULL; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_mid == &(p_link->tail) || p_mid->num > p_temp->num) { p_first->p_next = p_temp; p_temp->p_next = p_mid; break ; } } return 0; } //删除最前面数字的函数 int link_remove_head(link *p_link) { node *p_first = &(p_link->head); node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_link->head.p_next == &(p_link->tail)) { return 0; } p_first->p_next = p_last; free (p_mid); p_mid = NULL; } //删除最后一个有效数字 int link_remove_tail(link *p_link) { node *p_node = NULL; for (p_node = &(p_link->head);p_node !=&(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_last == &(p_link->tail)) { p_first->p_next = p_last; free (p_mid); p_mid = NULL; return 1; } } return 0; } //删除某个给定数字的函数 int link_remove(link *p_link, int num) { node *p_node = NULL; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_mid != &(p_link->tail) && p_mid->num == num) { p_first->p_next = p_last; free (p_mid); p_mid = NULL; return 1; } } return 0; } //获得第一个有效数字的函数 int link_get_head(link *p_link, int *p_num) { if (p_link->head.p_next == &(p_link->tail)) { return 0; } node *p_first = &(p_link->head); node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; p_first->p_next = p_last; *p_num = p_mid->num; return 1; } //获得最后一个有效数字的函数 int link_get_tail(link *p_link, int *p_num) { node *p_node = NULL; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_last == &(p_link->tail)) { *p_num = p_mid->num; return 1; } } return 0; } //获得指定编号数字的函数 int link_get(link *p_link, int *p_num, int num) { int cnt = 0; node *p_node = NULL; for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next) { node *p_first = p_node; node *p_mid = p_first->p_next; node *p_last = p_mid->p_next; if (p_mid != &(p_link->tail) && cnt == num) { *p_num = p_mid->num; return 1; } cnt++; } return 0; } |
测试函数:
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
|
/* * 链表测试 * */ #include <stdio.h> #include "link_0505.h" int main() { int size = 0, num = 0, val = 0; link lnk = {0}; link_init(&lnk); link_add_head(&lnk, 30); link_add_head(&lnk, 20); link_append(&lnk, 90); link_append(&lnk, 100); link_insert(&lnk, 50); link_insert(&lnk, 60); link_insert(&lnk, 40); link_insert(&lnk, 80); link_insert(&lnk, 70); size = link_size(&lnk); for (num = 0;num <= size - 1;num++) { link_get(&lnk, &val, num); printf ( "%d " , val); } printf ( "\n" ); printf ( "------------------" ); link_remove_head(&lnk); link_remove_tail(&lnk); link_remove(&lnk, 70); size = link_size(&lnk); for (num = 0;num <= size - 1;num++) { link_get(&lnk, &val, num); printf ( "%d " , val); } printf ( "\n" ); link_get_head(&lnk, &val); printf ( "最前面的数字是%d\n" , val); link_get_tail(&lnk, &val); printf ( "最后面的数字是%d\n" , val); link_deinit(&lnk); return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/chulijun3107/article/details/80211830