-
Notifications
You must be signed in to change notification settings - Fork 1
/
3_7链表.c
356 lines (286 loc) · 6.31 KB
/
3_7链表.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/********************************************************************
* 单向链表的创建
* 在前面连入结点
*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}node_t;
node_t *create(node_t *head)
{
int n;
node_t *cur, *tail;
while(1){
scanf("%d", &n);
if(n == 0)
break;
cur = malloc(sizeof(node_t));//第1步:申请空间
if(cur == NULL){
perror("malloc fail");
exit(1);
}
cur->data = n;//第2步:向空间中存放数据
cur->next = NULL;
cur->next = head;//第3步:插入链表——头部链入或尾部链入,此为头部链入
head = cur;
}
return head;
}
void link_print_destroy(node_t *head)
{
node_t *cur, *next;
for(cur = head; cur != NULL; cur = next){
next = cur->next;
printf("--%d--\n", cur->data);
free(cur);
}
}
int main(void)
{
node_t *head = NULL;
head = create(head);
link_print_destroy(head);
return 0;
}
/************************************************************************
* 此为尾部链入
* *********************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}node_t;
node_t *create(node_t *head)//创建节点和连入链表放在一个里面了
{
int n;
node_t *cur, *tail;
while(1)
{
scanf("%d", &n);
if(n == 0)
break;
cur = malloc(sizeof(node_t));//1
if(cur == NULL)
{
perror("malloc fail");
exit(1);
}
cur->data = n;//2
cur->next = NULL;
if(head == NULL)//3
head = tail = cur;
else
tail->next = cur;
tail = cur;
}
return head;
}
void link_print_destroy(node_t *head)
{
node_t *cur, *next;
for(cur = head; cur != NULL; cur = next){
next = cur->next;
printf("--%d--\n", cur->data);
free(cur);
}
}
int main(int argc, const char *argv[])
{
node_t *head = NULL;
head = create(head);
return 0;
}
/****************************************************************
* malloc的使用
* *************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char *p = malloc(20);//p:stack(栈); *p:pead(堆)
if(p == NULL){
perror("malloc fail");
exit(1);
}
printf("malloc:p = %p\n", p);//13行和16行
printf("%s\n", strcpy(p, "hello!"));
free(p);//free heap memorys(释放的是堆上的)
printf("free:p = %p\n", p);//p变量本身是statck中的而p中的值是pead中的数据
//free释放的是堆区的空间所以p值没变
p = NULL;//防止野指针出现 (如果它的上一行有*p=??)可段错误
return 0;
}
/******************************************************************
* realloc的使用
* ***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char *p = malloc(10);
if(p == NULL){
perror("malloc fail");
//fprintf(stderr,"malloc
//fail");stderr与stdout的区别stdout有行缓存特性stderr没有
exit(1);
}
printf("%s\n",strcpy(p, "hello!"));
p = realloc(p, 20);//20为最终使用的字节数 p为malloc或者calloc申请的地址值
printf("%s\n",strcpy(p, "hello!"));//数据没变
free(p);
p = NULL;
return 0;
}
/************************************************************************
* 输入若干自然数,若输入数据为0则结束输
* 入,把以上数据保存到堆中,然后打印。
*(关键在于如何动态分配空间)
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define ITEM 10
int main(void)
{
int *p, n, i, max_i = ITEM, cur_i = 0;
p = malloc(sizeof(int) * ITEM);
if(p == NULL){
fprintf(stderr, "malloc fail!\n");
exit(1);
}
while(1){
printf("enter a number:");
scanf("%d", &n);
if(n == 0)
break;
if(cur_i == max_i){
max_i += ITEM;
p = realloc(p, sizeof(int) * max_i);
if(p == NULL){
fprintf(stderr, "realloc fail!\n");
exit(1);
}
}
p[cur_i++] = n;
}
for(i = 0; i < cur_i; i++)
printf("%d ", p[i]);
printf("\n");
free(p);
p = NULL;
return 0;
}
//链表
//Makefile这样写将main函数(此处为测试函数)在调试的时候打开
//在应用中关闭
//make CPPFLAGS=-DDEBUG(打开main)
//直接make没有打开main函数
/***********************************************************
*src = $(wildcard *.c)
*targets = $(patsubst %.c, %, $(src))
*CC = gcc
*CFLAGS = -Wall -g
*CPPFLAGS =
*all:$(targets)
*$(targets):%:%.c
* $(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@
*
*clean:
* -rm -rf $(targets)
*.PHONY:clean all
**********************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}node_t;
static node_t *mk_node(int val)
{
node_t *p = malloc(sizeof(node_t));
if(p != NULL){
p->data = val;
p->next = NULL;
}
return p;
}
node_t *link_insert(node_t *head, int val)
{
node_t *cur = mk_node(val);
node_t *tail;
if(cur == NULL || head == NULL){
if(head == NULL)
head = cur;
return head;
}
for(tail = head; tail->next != NULL; tail = tail->next)
;
tail->next = cur;
return head;
}
void link_show(node_t *head)
{
node_t *cur;
for(cur = head; cur != NULL; cur = cur->next)
printf("--%d--\n", cur->data);
}
node_t *link_remove(node_t *head, int val)
{
node_t *pre, *cur;
if(head == NULL)
return NULL;
if(head->data == val){
head = head->next;
return head;
}
for(pre = head, cur = head->next; cur != NULL; pre = pre->next, cur = cur->next){
if(cur->data == val){
pre->next = cur->next;
cur->next = NULL;
return cur;
}
}
return NULL;
}
void link_destroy(node_t *head)
{
node_t *cur, *next;
for(cur = head; cur != NULL; cur = next){
next = cur->next;
free(cur);
}
}
void link_print_destroy(node_t *head)
{
node_t *cur, *next;
for(cur = head; cur != NULL; cur = next){
next = cur->next;
printf("--%d--\n", cur->data);
free(cur);
}
}
#ifdef DEBUG
int main(void)
{
node_t *head = NULL, *rmp;
int n;
while(1){
scanf("%d", &n);
if(n == 0)
break;
head = link_insert(head, n);
}
link_show(head);
rmp = link_remove(head, 3);
printf("rmp->data = %d\n", rmp->data);
free(rmp);
rmp = NULL;
link_destroy(head);
head = NULL;
return 0;
}
#endif