-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
205 lines (181 loc) · 3.7 KB
/
list.c
File metadata and controls
205 lines (181 loc) · 3.7 KB
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
#include <stdio.h>
#include <stdlib.h>
typedef struct node Node;
typedef Node *LIST;
typedef Node *Position;
/* node,节点 */
struct node {
int element;
Position next;
};
/*
* operations (stereotype)
* 操作
*/
LIST init_list(void);
void delete_list(LIST);
int is_null(LIST);
void insert_node(Position, int);
void delete_node(LIST, Position);
Position find_last(LIST);
Position find_value(LIST, int);
Position find_previous(LIST, Position);
void print_list(LIST);
/* for testing purpose */
int main()
{
LIST L;
Position np;
int i;
/* elements to be put into the list */
int a[] = {1, 3, 5, 7, 9};
/* initiate a list */
L = init_list();
print_list(L);
/* insert nodes. Insert just after head node */
for (i=4; i>=0; i--) {
insert_node(L, a[i]);
}
print_list(L);
/* delete first node with value 5 */
np = find_value(L, 1);
delete_node(L, np);
print_list(L);
/* delete list */
delete_list(L);
L = NULL;
print_list(L);
/* initiate a list */
L = init_list();
print_list(L);
/* insert nodes. Insert just after head node */
for (i=4; i>=0; i--) {
insert_node(L, a[i]);
}
print_list(L);
/* delete list */
delete_list(L);
return 0;
}
/*
* Traverse the list and print each element
* 打印表
*/
void print_list(LIST L)
{
Position np;
if(is_null(L)) {
printf("Empty List\n\n");
return;
}
np = L;
while(np->next != NULL) {
np = np->next;
printf("%p: %d \n", np, np->element);
}
printf("\n");
}
/*
* Initialize a linked list. This list has a head node
* head node doesn't store valid element value
* 创建表
*/
LIST init_list(void)
{
LIST L = (LIST) malloc(sizeof(Node));
//L = (LIST) malloc(sizeof(struct node));
//L = (Position) malloc(sizeof(struct node));
L->next = NULL;
return L;
}
/*
* Delete all nodes in a list
* 删除表
*/
void delete_list(LIST L)
{
Position np, next;
np = L;
do {
next = np->next;
free(np);
np = next;
} while(next != NULL);
}
/*
* if a list only has head node, then the list is null.
* 判断表是否为空
*/
int is_null(LIST L)
{
return L == NULL || L->next == NULL;
}
/*
* insert a node after Position np
* 在np节点之后,插入节点
*/
void insert_node(Position np, int value)
{
Position nodeAddr = (Position) malloc(sizeof(Node));
//nodeAddr = (Position) malloc(sizeof(struct node));
nodeAddr->element = value;
nodeAddr->next = np->next;
np->next = nodeAddr;
}
/*
* delete node at Position np
* 删除np节点
*/
void delete_node(LIST L, Position np)
{
Position previous, next;
next = np->next;
previous = find_previous(L, np);
if(previous != NULL) {
previous->next = next;
free(np);
} else {
printf("Error: np not in the list");
}
}
/*
* find the last node of the list
* 寻找表的最后一个节点
*/
Position find_last(LIST L)
{
Position np;
np = L;
while(np->next != NULL) {
np = np->next;
}
return np;
}
/*
* This function serves for 2 purposes:
* 1. find previous node
* 2. return NULL if the Position isn't in the list
* 寻找npTarget节点前面的节点
*/
Position find_previous(LIST L, Position npTarget)
{
Position np = L;
while (np->next != NULL) {
if (np->next == npTarget) return np;
np = np->next;
}
return NULL;
}
/*
* find the first node with specific value
* 查询
*/
Position find_value(LIST L, int value)
{
Position np = L;
while (np->next != NULL) {
np = np->next;
if (np->element == value) return np;
}
return NULL;
}