-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinklist.c
105 lines (84 loc) · 1.85 KB
/
linklist.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
#include "stdio.h"
#include "stdlib.h"
typedef struct node{
int data;
struct node *next;
} Node;
Node *node_create(int data)
{
Node *newnode = (Node *)malloc(sizeof(Node));
if (!newnode) return NULL;
else {
newnode->data = data;
newnode->next=NULL;
}
return newnode;
}
void node_push(Node *first, Node *addnode)
{
Node *trace = first;
Node *previos;
while(trace) {
previos = trace;
trace = trace->next;
}
previos->next = addnode;
}
void list_free(Node *first)
{
Node *trace = first;
while(trace) {
Node *tmp = trace;
trace = trace->next;
printf("Free %d\n",tmp->data);
free(tmp);
}
}
void print_list(Node *first)
{
Node *trace = first;
if (!trace) {
printf("list is empty!!\n");
} else {
while (trace) {
printf("%d\n", trace->data);
trace = trace->next;
}
}
printf("<---%s\n",__func__);
}
int delete_node(Node *first, int data_del)
{
int status = 0;
Node *trace = first;
Node *previous;
while (trace) {
if (trace->data == data_del) {
printf("trace data: %d\n",trace->data);
previous->next = trace->next;
Node *tmp = trace->next;
printf("trace next data: %d\n",tmp->data);
free(trace);
status = 1;
break;
} else {
previous = trace;
trace = trace->next;
}
}
return status;
}
int main(int argc, char *argv[])
{
Node *first_node = node_create(5);
for (int i =0 ;i< 10;i++){
Node *nodenew = node_create(i*2);
node_push(first_node, nodenew);
}
print_list(first_node);
delete_node(first_node, 10);
print_list(first_node);
list_free(first_node);
print_list(first_node);
return 0;
}