-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.c
320 lines (247 loc) · 5.8 KB
/
list.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
#include "list.h"
struct data {
unsigned key;
int value;
};
struct node {
struct data *data;
struct node *next;
};
Node* create(void) {
return NULL;
}
void insertFirst(Node **head, unsigned int key, int value) {
Node *node = (Node*) malloc(sizeof(Node));
if (node == NULL) {
puts("Error: Unable to allocate memory.");
exit(EXIT_FAILURE);
}
Data *data = (Data*) malloc(sizeof(Data));
if (data == NULL) {
puts("Error: Unable to allocate memory.");
exit(EXIT_FAILURE);
}
data->key = key;
data->value = value;
node->data = data;
if (*head == NULL)
node->next = node;
else {
node->next = *head;
Node *last = *head;
while (last->next != *head)
last = last->next;
last->next = node;
}
*head = node;
}
void insertLast(Node *head, unsigned int key, int value) {
if (head == NULL) {
puts("Error: Empty list. Cannot insert last node.");
exit(EXIT_FAILURE);
}
Node *node = (Node*) malloc(sizeof(Node));
if (node == NULL) {
puts("Error: Unable to allocate memory.");
exit(EXIT_FAILURE);
}
Data *data = (Data*) malloc(sizeof(Data));
if (data == NULL) {
puts("Error: Unable to allocate memory.");
exit(EXIT_FAILURE);
}
data->key = key;
data->value = value;
node->data = data;
node->next = head;
Node *last = head;
while (last->next != head)
last = last->next;
last->next = node;
}
Node* findLast(Node *head) {
if (head == NULL) {
puts("Error: Empty list. Cannot find last node.");
exit(EXIT_FAILURE);
}
Node *last = head;
while (last->next != head)
last = last->next;
return last;
}
Node* findNode(Node *head, unsigned int key) {
if (head == NULL) {
puts("Error: Empty list. Cannot find node.");
exit(EXIT_FAILURE);
}
Node *current = head;
while (current->next != head && current->data->key != key)
current = current->next;
if (current->data->key != key) {
puts("Error: Not Found. Cannot find node.");
exit(EXIT_FAILURE);
}
return current;
}
void updateNode(Node *head, unsigned int key, int value) {
if (head == NULL) {
puts("Error: Empty list. Cannot update node.");
exit(EXIT_FAILURE);
}
Node *current = head;
while (current->next != head && current->data->key != key)
current = current->next;
if (current->data->key != key) {
puts("Error: Not Found. Cannot update node.");
exit(EXIT_FAILURE);
}
current->data->value = value;
}
void removeFirst(Node **head) {
if (*head == NULL) {
puts("Error: Empty list. Cannot remove first node.");
exit(EXIT_FAILURE);
}
Node *first = *head;
if (first->next == *head)
*head = NULL;
else {
Node *last = *head;
while (last->next != *head)
last = last->next;
*head = first->next;
last->next = *head;
}
free(first);
}
void removeLast(Node **head) {
if (*head == NULL) {
puts("Error: Empty list. Cannot remove last node.");
exit(EXIT_FAILURE);
}
Node *last = *head;
Node *previous = NULL;
while (last->next != *head) {
previous = last;
last = last->next;
}
if (last == *head)
*head = NULL;
else
previous->next = last->next;
free(last);
}
void removeNode(Node **head, unsigned int key) {
if (*head == NULL) {
puts("Error: Empty list. Cannot remove node.");
exit(EXIT_FAILURE);
}
Node *previous = NULL;
Node *current = *head;
while (current->next != *head && current->data->key != key) {
previous = current;
current = current->next;
}
if (current->data->key != key) {
puts("Error: Not found. Cannot remove node.");
exit(EXIT_FAILURE);
}
if (current == *head) {
Node *last = *head;
while (last->next != *head)
last = last->next;
*head = current->next;
last->next = *head;
}
else
previous->next = current->next;
free(current);
}
void reverseList(Node **head) {
if (*head == NULL)
return;
Node *last = *head;
while (last->next != *head)
last = last->next;
Node *next;
Node *previous = last;
Node *current = *head;
do {
next = current->next;
current->next = previous;
previous = current;
current = next;
} while (current != *head);
*head = previous;
}
void printNode(Node *node) {
if (node == NULL) return;
printf("(%d, %d)\n", node->data->key, node->data->value);
}
void printList(Node *head) {
if (head == NULL)
return;
Node *p = head;
do {
printf("(%d, %d); ", p->data->key, p->data->value);
p = p->next;
} while (p != head);
putchar('\n');
}
void printReverseList(Node *head, Node *current) {
if (current->next != head)
printReverseList(head, current->next);
printf("(%d, %d); ", current->data->key, current->data->value);
}
void printReverse(Node *head) {
if (head == NULL) return;
printReverseList(head, head);
putchar('\n');
}
bool isEmpty(Node *head) {
return head == NULL;
}
unsigned int length(Node *head) {
if (head == NULL)
return 0;
int length = 0;
Node *p = head;
do {
length++;
p = p->next;
} while (p != head);
return length;
}
unsigned int sum(Node *head) {
if (head == NULL)
return 0;
int sum = 0;
Node *p = head;
do {
sum += p->data->value;
p = p->next;
} while (p != head);
return sum;
}
void sortList(Node *head) {
Node *current, *next;
int i, j, k, tempKey, tempData;
int size = length(head);
k = size;
for (i = 0; i < size - 1; i++, k--) {
current = head;
next = current->next;
for (j = 1; j < k; j++) {
if (current->data->value > next->data->value) {
tempData = current->data->value;
current->data->value = next->data->value;
next->data->value = tempData;
tempKey = current->data->key;
current->data->key = next->data->key;
next->data->key = tempKey;
}
current = current->next;
next = next->next;
}
}
}