forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
402 lines (326 loc) · 10.2 KB
/
queue.c
File metadata and controls
402 lines (326 loc) · 10.2 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
/* Create an empty queue */
struct list_head *q_new()
{
struct list_head *new = malloc(sizeof(struct list_head));
if (!new)
return NULL;
/* 善用 list.h 中的函式 */
INIT_LIST_HEAD(new);
return new;
}
/* Free all storage used by queue */
void q_free(struct list_head *head)
{
if (!head)
return;
struct list_head *node, *safe;
list_for_each_safe (node, safe, head) {
element_t *e = list_entry(node, element_t, list);
/* 使用 queue.h 中的函式替代直接呼叫 free() */
q_release_element(e);
}
test_free(head);
}
/* Insert an element at head of queue */
bool q_insert_head(struct list_head *head, char *s)
{
if (!head)
return false;
element_t *new_element = malloc(sizeof(element_t));
if (!new_element)
return false;
new_element->value = strdup(s);
/* 再次檢查是否有記憶體上的問題發生 */
if (!new_element->value) {
free(new_element);
return false;
}
list_add(&new_element->list, head);
return true;
}
/* Insert an element at tail of queue */
bool q_insert_tail(struct list_head *head, char *s)
{
if (!head)
return false;
element_t *new_element = malloc(sizeof(element_t));
if (!new_element)
return false;
new_element->value = strdup(s);
if (!new_element->value) {
free(new_element);
return false;
}
list_add_tail(&new_element->list, head);
return true;
}
/* Remove an element from head of queue */
element_t *q_remove_head(struct list_head *head, char *sp, size_t bufsize)
{
if (!head || list_empty(head))
return NULL;
element_t *e = list_first_entry(head, element_t, list);
list_del(&e->list);
if (sp && bufsize > 0) {
strncpy(sp, e->value, bufsize - 1);
sp[bufsize - 1] = '\0';
}
return e;
}
/* Remove an element from tail of queue */
element_t *q_remove_tail(struct list_head *head, char *sp, size_t bufsize)
{
if (!head || list_empty(head))
return NULL;
element_t *e = list_last_entry(head, element_t, list);
list_del(&e->list);
if (sp && bufsize > 0) {
strncpy(sp, e->value, bufsize - 1);
sp[bufsize - 1] = '\0';
}
return e;
}
/* Return number of elements in queue */
int q_size(struct list_head *head)
{
if (!head || list_empty(head))
return 0;
int len = 0;
struct list_head *node;
list_for_each (node, head)
len++;
return len;
}
bool q_delete_mid(struct list_head *head)
{
// https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/
if (!head || list_empty(head))
return false;
if (list_is_singular(head)) {
element_t *elem = list_first_entry(head, element_t, list);
list_del(&elem->list);
q_release_element(elem);
return true;
}
struct list_head *slow = head->next;
struct list_head *fast = head->next;
while (fast != head && fast->next != head) {
fast = fast->next->next;
slow = slow->next;
}
element_t *mid = list_entry(slow, element_t, list);
list_del(slow);
q_release_element(mid);
return true;
}
bool q_delete_dup(struct list_head *head)
{
// https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
if (!head || list_empty(head))
return false;
if (list_is_singular(head))
return true;
LIST_HEAD(to_discard);
element_t *curr = NULL, *safe = NULL;
struct list_head *last_unique = head;
list_for_each_entry_safe (curr, safe, head, list) {
/* 如果 safe 不是 head 且與 curr 值相同,跳過,表示仍在重複區段內 */
if (&safe->list != head && !strcmp(safe->value, curr->value))
continue;
if (curr->list.prev != last_unique) {
/* 臨時存放切割出的重複區段 */
LIST_HEAD(tmp);
/* 切割重複區段 */
list_cut_position(&tmp, last_unique, &curr->list);
/* 將重複區段加入待丟棄列表 */
list_splice(&tmp, &to_discard);
}
last_unique = safe->list.prev; // 更新
}
list_for_each_entry_safe (curr, safe, &to_discard, list)
q_release_element(curr);
return true;
}
/* Reverse elements in queue */
void q_reverse(struct list_head *head)
{
if (!head || list_empty(head))
return;
// LIST_HEAD(new_head);
struct list_head *node, *safe;
list_for_each_safe (node, safe, head)
list_move(node, head);
/* 將反轉後的串鏈拼接到原始 head */
// list_splice(&new_head, head);
}
/* Reverse the nodes of the list k at a time */
void q_reverseK(struct list_head *head, int k)
{
if (!head || list_empty(head) || list_is_singular(head))
return;
struct list_head *list_tail = head;
struct list_head *curr = head->next;
int count = q_size(head);
while (count >= k) {
LIST_HEAD(tmp);
for (int i = 0; i < k; i++) {
struct list_head *next_node = curr->next;
list_move_tail(curr, &tmp);
curr = next_node;
}
q_reverse(&tmp);
list_splice(&tmp, list_tail);
/* 將反轉的終點更新 */
for (int i = 0; i < k; i++)
list_tail = list_tail->next;
count -= k;
}
}
/* Swap every two adjacent nodes */
void q_swap(struct list_head *head)
{
// https://leetcode.com/problems/swap-nodes-in-pairs/
q_reverseK(head, 2);
/*
if (!head || list_empty(head) || list_is_singular(head))
return;
struct list_head *node = head->next;
while (node != head && node->next != head) {
struct list_head *next = node->next;
struct list_head *next_next = next->next;
list_del(node);
list_add(node, next);
node = next_next;
}
*/
}
/* Merges two sorted circular doubly-linked lists into 'left'
* in ascending / descending (descend=true) order, clearing 'right'. */
void q_merge_two(struct list_head *left, struct list_head *right, bool descend)
{
if (!left || !right || list_empty(right))
return;
if (list_empty(left)) {
list_splice_init(right, left);
return;
}
LIST_HEAD(merged);
struct list_head *l_node = left->next;
struct list_head *r_node = right->next;
while (l_node != left && r_node != right) {
const element_t *l_elem = list_entry(l_node, element_t, list);
const element_t *r_elem = list_entry(r_node, element_t, list);
int cmp = strcmp(l_elem->value, r_elem->value);
if (descend ? cmp > 0 : cmp <= 0) {
struct list_head *next = l_node->next;
list_move_tail(l_node, &merged);
l_node = next;
} else {
struct list_head *next = r_node->next;
list_move_tail(r_node, &merged);
r_node = next;
}
}
if (l_node != left)
list_splice_tail_init(left, &merged);
if (r_node != right)
list_splice_tail_init(right, &merged);
list_splice_init(&merged, left);
}
/* Sort elements of queue in ascending/descending order */
void q_sort(struct list_head *head, bool descend)
{
if (!head || list_empty(head) || list_is_singular(head))
return;
struct list_head *fast = head->next;
struct list_head *slow = head->next;
while (fast != head && fast->next != head) {
fast = fast->next->next;
slow = slow->next;
}
LIST_HEAD(left);
list_cut_position(&left, head, slow->prev);
q_sort(&left, descend);
q_sort(head, descend);
q_merge_two(&left, head, descend);
INIT_LIST_HEAD(head);
list_splice(&left, head);
}
/* Remove every node which has a node with a strictly less value anywhere to
* the right side of it */
int q_descend(struct list_head *head)
{
// https://leetcode.com/problems/remove-nodes-from-linked-list/
if (!head || list_empty(head))
return 0;
if (list_is_singular(head))
return 1;
element_t *cur = list_last_entry(head, element_t, list);
const char *max_value = cur->value;
int kept_count = 1;
while (cur->list.prev != head) {
element_t *prev = list_last_entry(&cur->list, element_t, list);
if (strcmp(prev->value, max_value) > 0) {
max_value = prev->value;
kept_count++;
cur = prev;
} else {
list_del(&prev->list);
q_release_element(prev);
}
}
return kept_count;
}
/* Remove every node which has a node with a strictly greater value anywhere to
* the right side of it */
int q_ascend(struct list_head *head)
{
// https://leetcode.com/problems/remove-nodes-from-linked-list/
if (!head || list_empty(head))
return 0;
if (list_is_singular(head))
return 1;
element_t *cur = list_last_entry(head, element_t, list);
const char *min_value = cur->value;
int kept_count = 1;
while (cur->list.prev != head) {
element_t *prev = list_last_entry(&cur->list, element_t, list);
if (strcmp(prev->value, min_value) < 0) {
min_value = prev->value;
kept_count++;
cur = prev;
} else {
list_del(&prev->list);
q_release_element(prev);
}
}
return kept_count;
}
/* Merge all the queues into one sorted queue, which is in ascending/descending
* order */
int q_merge(struct list_head *head, bool descend)
{
// https://leetcode.com/problems/merge-k-sorted-lists/
if (!head || list_empty(head))
return 0;
if (list_is_singular(head))
return list_first_entry(head, queue_contex_t, chain)->size;
queue_contex_t *target_queue =
list_first_entry(head, queue_contex_t, chain);
struct list_head *target_list = target_queue->q;
struct list_head *node, *safe;
list_for_each_safe (node, safe, head) {
if (node == &target_queue->chain)
continue;
queue_contex_t *curr_queue = list_entry(node, queue_contex_t, chain);
struct list_head *curr_list = curr_queue->q;
if (curr_list) {
q_merge_two(target_list, curr_list, descend); // 合併到 target_list
}
}
return q_size(target_list);
}