这个小代码是我凭自己对指针和链表的理解和认识,自己实现的,没有参考其他人的代码,如果有相同的地方,那真的只是巧合,代码我在ubuntu 15.04下测试通过,可能存在很多错误和漏洞.
doublelist.c
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/************************************************************************* > File Name: doublelist.c > Author: ChenYiLiang > Mail: chenyiliangex@163.com > Created Time: Sat 21 Mar 2015 07:32:22 PM CST ************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> struct userdata{ int userid; char username[30]; struct userdata *previous; struct userdata *next; }; struct userdata *header; size_t scanf_id; char scanf_name[30]; int yesno; int deletePosition; int alterPosition; int alterId; char alterName[30]; int searchPosition; FILE *ptr_fpid; /*向链表中插入数据*/ int insert_list( struct userdata *header, size_t position, char name[], size_t id); /*删除链表中指定的节点*/ int delete_node( struct userdata *header, size_t position); /*修改指定位置的节点信息*/ int alter_node( struct userdata *header, size_t position, size_t id, char name[]); /*查找链表中的数据*/ struct userdata *search_node( struct userdata *header, size_t position); /*遍历链表*/ int travel_list( struct userdata *header); /*判断链表是空*/ int isempty( struct userdata *header); /*将链表结构写入文件*/ int write_into_file( struct userdata *header, FILE *fp); /*从文件读取数据放到链表中*/ int read_from_file( struct userdata *header, FILE *fp); int main(){ struct userdata *header_node = ( struct userdata *) malloc ( sizeof ( struct userdata)); header_node -> previous = NULL; header_node -> next = NULL; read_from_file(header_node, ptr_fpid); travel_list(header_node); while (1){ //scanf("%*[^\n]"); //scanf("%*c"); //scanf("%*[^\n]"); printf ( "please input id - " ); scanf ( "%d" , &scanf_id); //scanf("%*c"); //scanf("%*[^\n]"); printf ( "please input your name - " ); scanf ( "%s" , scanf_name); printf ( "%d - %s\n\n" , scanf_id, scanf_name); //isempty(header_node); /*0表示默认插入到链表的尾部*/ insert_list(header_node, 0, scanf_name, scanf_id); //write_into_file(header_node, ptr_fpid); //isempty(header_node); printf ( "input anymore - " ); scanf ( "%d" , &yesno); if (yesno == -1){ break ; } scanf ( "%*c" ); scanf ( "%*[^\n]" ); // travel_list(header_node); } getchar (); //printf("delete position data - "); //scanf("%d", &deletePosition); //delete_node(header_node, deletePosition); // printf("alter data for position - "); // scanf("%d", &alterPosition); // printf("please inout new id - "); // scanf("%d",&alterId); // printf("please input new name - "); // scanf("%s", alterName); // alter_node(header_node, alterPosition, alterId, alterName); write_into_file(header_node, ptr_fpid); travel_list(header_node); printf ( "\n\n" ); printf ( "please input position to search - " ); scanf ( "%d" , &searchPosition); struct userdata *searchData = search_node(header_node, searchPosition); printf ( "%d\n" , searchData -> userid); printf ( "%s\n" , searchData -> username); return 0; } /* 插入节点 */ int insert_list( struct userdata *header, size_t position, char name[], size_t id ){ struct userdata *temp_newuser = header; struct userdata *getMemory = ( struct userdata *) malloc ( sizeof ( struct userdata)); getMemory -> userid = id; strncpy (getMemory -> username, name, 30); /*当position == 0时,表示默认插入到链表的尾部*/ if (0 == position){ if (NULL != temp_newuser -> next){ while (NULL != temp_newuser -> next){ temp_newuser = temp_newuser -> next; } } } /*当position > 1时则寻找合适的位置插入*/ if (1 <= position){ for ( int i = 0; i <= position; i++){ /*当执行此处的代码时表示,链表已经到达尾部或者是空链表*/ if (NULL == temp_newuser -> next){ break ; } temp_newuser = temp_newuser -> next; } } getMemory -> previous = temp_newuser; if (temp_newuser -> next == NULL){ temp_newuser -> next = getMemory; getMemory -> next = NULL; } else { temp_newuser -> next -> previous = getMemory; getMemory -> next = temp_newuser -> next; temp_newuser -> next = getMemory; } return 0; } /*删除链表中指定的节点*/ int delete_node( struct userdata *header, size_t position){ int is_empty = isempty(header); if (0 == is_empty){ printf ( "this si a empty list!\n\n" ); return -1; } struct userdata *deleteNode = header; for ( int i = 0; i < position; i++ ){ /*当执行此处的代码时表示,链表已经到达尾部或者是空链表*/ if (NULL == deleteNode -> next){ break ; } deleteNode = deleteNode -> next; } /**/ deleteNode -> next -> previous = deleteNode -> previous; deleteNode -> previous -> next = deleteNode -> next; free (deleteNode); return 0; } /*修改指定位置的节点信息*/ int alter_node( struct userdata *header, size_t position, size_t id, char name[]){ int isEmpty = isempty(header); if (0 == isEmpty){ printf ( "this is a empty list\n\n" ); return -1; } struct userdata *alterNode = header; for ( int i = 0; i < position; i++ ){ /*当执行此处的代码时表示,链表已经到达尾部或者是空链表*/ if (NULL == alterNode -> next){ break ; } alterNode = alterNode -> next; } alterNode -> userid = id; strncpy (alterNode -> username, name, 30); return 0; } /*查找链表中的数据*/ struct userdata *search_node( struct userdata *header, size_t position){ int isEmpty = isempty(header); if (0 == isEmpty){ printf ( "this is a empty!\n" ); return NULL; } struct userdata *searchNode = header; for ( int i = 0; i < position; i++){ if (NULL == searchNode -> next){ break ; } searchNode = searchNode -> next; } return searchNode; } /*遍历链表*/ int travel_list( struct userdata *header){ struct userdata *travel = header; if (NULL == travel -> next){ printf ( "This is a empty list!!\n" ); return 1; } for (travel = travel -> next ; ; travel = travel -> next){ printf ( "%d\n" ,travel -> userid); printf ( "%s\n" , travel -> username); if (NULL == travel -> next){ break ; } } return 1; } /*判断链表是空*/ int isempty( struct userdata *header){ if (header -> next == NULL){ return 0; } else { return 1; } } /*将链表结构写入文件*/ int write_into_file( struct userdata *header, FILE *fp){ fp = fopen ( "listdata" , "wb" ); if (NULL == fp){ perror ( "open file failed when write into file!" ), exit (-1); } printf ( "come into write!\n" ); for ( struct userdata *move = header -> next; ; move = move -> next){ fwrite (move, sizeof ( struct userdata), 1, fp); if (NULL == move -> next){ break ; } } fclose (fp); fp = NULL; return 0; } /*从文件读取数据放到链表中*/ int read_from_file( struct userdata *header, FILE *fp){ struct userdata *readfile = header; fp = fopen ( "listdata" , "rb" ); if (NULL == fp){ perror ( "open file failed when read - " ); return -1; } while (1){ struct userdata *newread = ( struct userdata *) malloc ( sizeof ( struct userdata)); fread (newread, sizeof ( struct userdata), 1, fp); if ( feof (fp)){ /*当读取到文件的尾部时.跳出循环*/ break ; } readfile -> next = newread; newread -> next = NULL; newread -> previous = readfile; readfile = newread; } fclose (fp); fp = NULL; return 0; } |
C语言实现双向链表删除节点、插入节点、双向输出等操作
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
|
#include<cstdio> #include<cstdlib> typedef struct DoubleLinkedList { int data; struct DoubleLinkedList *pre; struct DoubleLinkedList *next; }DlinkedList_Node; //建立链表 DlinkedList_Node* createDLink() { DlinkedList_Node *head,*p,*s; int x; head = (DlinkedList_Node*) malloc ( sizeof (DlinkedList_Node)); p = head; while (1) { printf ( "please input the data: \n" ); scanf ( "%d" ,&x); if (x != 65535) { s = (DlinkedList_Node*) malloc ( sizeof (DlinkedList_Node)); s ->data = x; s-> pre = p; p->next = s; p=s; } else { printf ( "\n数据输入结束\n" ); break ; } } p->next = NULL; head = head ->next; head->pre = NULL; return head; } //顺序、反序打印链表 void printDLink(DlinkedList_Node *head) { DlinkedList_Node *p,*s; p = head; printf ( "正序输出双向链表:\n" ); while (p) { printf ( "%d " ,p->data); s = p; p = p->next; } printf ( "\n 逆序输出双向链表: \n" ); while (s) { printf ( "%d " ,s->data); s = s->pre; } printf ( "\n \n" ); } //删除一个结点 DlinkedList_Node* deleteDlinkedList_Node(DlinkedList_Node *head, int i) { DlinkedList_Node *p; p = head; if (p->data == i) { head = p->next; head->pre = NULL; free (p); return head; } while (p) { if (p->data == i) { p->pre->next = p->next; p->next->pre = p->pre; free (p); return head; } p = p->next; } printf ( "没有找到想要删除的数据\n" ); return head; } //插入一个结点 DlinkedList_Node* insertDlinkedList_Node(DlinkedList_Node *head, int i) { DlinkedList_Node *p,*temp; p = head; temp = (DlinkedList_Node*) malloc ( sizeof (DlinkedList_Node)); temp ->data = i; if (i < p->data) //比头结点数据小,插入到链表头部 { head = temp; head->next = p; //此处p为原来的head head->pre = NULL; p->pre = head; //此处p为原来的head return head; } while (p != NULL && i > p->data) //寻找合适的插入位置 { p = p->next; } if (i < p->data) //在链表中间某处找到合适插入位置 { temp ->next = p; temp ->pre = p->pre; p ->pre->next = temp; p ->pre = temp; return head; } else //没有找到合适的位置,只有将数据插入到链表尾部 { p->next = temp; //遍历到链表尾部,p==NULL temp ->pre = p; temp ->next = NULL; return head; } } int main() { DlinkedList_Node *head; head = createDLink(); printDLink(head); head = insertDlinkedList_Node(head,1012); head = deleteDlinkedList_Node(head,1991); printDLink(head); } /***************************** 运行结果如下: please input the data: 1991 please input the data: 1992 please input the data: 2013 please input the data: 2014 please input the data: 512 please input the data: 420 please input the data: 65535 数据输入结束 正序输出双向链表: 1991 1992 2013 2014 512 420 逆序输出双向链表: 420 512 2014 2013 1992 1991 正序输出双向链表: 1012 1992 2013 2014 512 420 逆序输出双向链表: 420 512 2014 2013 1992 1012 ******************************/ |
以上就是本文给大家分享的全部内容了,希望对大家更加熟悉C语言双向链表能够有所帮助。