forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
508 lines (430 loc) · 12.6 KB
/
queue.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "queue.h"
/* Notice: sometimes, Cppcheck would find the potential NULL pointer bugs,
* but some of them cannot occur. You can suppress them by adding the
* following line.
* cppcheck-suppress nullPointer
*/
int cmp(void *priv, const struct list_head *a, const struct list_head *b)
{
element_t *ela, *elb;
ela = list_entry(a, element_t, list);
elb = list_entry(b, element_t, list);
return strcmp(ela->value, elb->value);
}
/* Create an empty queue */
struct list_head *q_new()
{
struct list_head *head = malloc(sizeof(struct list_head));
if (!head)
return NULL;
INIT_LIST_HEAD(head);
return head;
}
/* Free all storage used by queue */
void q_free(struct list_head *l)
{
if (!l)
return;
element_t *node, *safe = NULL;
list_for_each_entry_safe (node, safe, l, list) {
if (node->value)
free(node->value);
free(node);
}
free(l);
}
/* Insert an element at head of queue */
bool q_insert_head(struct list_head *head, char *s)
{
element_t *new_node = malloc(sizeof(element_t));
if (!new_node) {
return false;
}
s = strdup(s);
new_node->value = s;
if (!new_node->value) {
free(new_node);
return false;
}
list_add(&new_node->list, head);
return true;
}
/* Insert an element at tail of queue */
bool q_insert_tail(struct list_head *head, char *s)
{
element_t *new_node = malloc(sizeof(element_t));
if (!new_node) {
return false;
}
new_node->value = strdup(s);
if (!new_node->value) {
free(new_node);
return false;
}
list_add_tail(&new_node->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 *node = list_first_entry(head, element_t, list);
list_del_init(head->next);
if (sp) {
strncpy(sp, node->value, bufsize);
strcat(sp, "\0");
}
return node;
}
/* 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 *node = list_last_entry(head, element_t, list);
list_del_init(head->prev);
if (sp) {
strncpy(sp, node->value, bufsize);
strcat(sp, "\0");
}
return node;
}
/* Return number of elements in queue */
int q_size(struct list_head *head)
{
if (!head)
return 0;
int len = 0;
struct list_head *li;
list_for_each (li, head)
len++;
return len;
}
/* Delete the middle node in queue */
bool q_delete_mid(struct list_head *head)
{
// https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list//
if (!head)
return false;
struct list_head *cur = head;
element_t *mid;
int size = q_size(head);
for (int i = 0; i < size / 2 + 1; i++) {
cur = cur->next;
}
// cur->prev->next = cur->next;
// cur->next->prev = cur->prev;
list_del_init(cur);
mid = container_of(cur, element_t, list);
free(mid->value);
free(mid);
return true;
}
/* Delete all nodes that have duplicate string */
bool q_delete_dup(struct list_head *head)
{
// https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
element_t *cur, *safe;
bool comp = 0;
list_for_each_entry_safe (cur, safe, head, list) {
if (safe != container_of(head, element_t, list) &&
strcmp(cur->value, safe->value) == 0) {
list_del_init(&cur->list);
free(cur->value);
free(cur);
comp = 1;
} else {
if (comp == 1) {
list_del_init(&cur->list);
free(cur->value);
free(cur);
}
comp = 0;
}
}
//&safe->list != &head
return true;
}
/* Swap every two adjacent nodes */
void q_swap(struct list_head *head)
{
// https://leetcode.com/problems/swap-nodes-in-pairs//
struct list_head *cur = head;
struct list_head *first = head->next;
struct list_head *second = first->next;
while (first != head) {
if (second == head) {
cur->next = first;
first->prev = cur;
first->next = head;
head->prev = first;
break;
} else {
first->next = second->next;
second->next->prev = first;
second->next = first;
first->prev = second;
cur->next = second;
second->prev = cur;
cur = first;
first = first->next;
second = first->next;
}
cur->next = head;
head->prev = cur;
}
}
/* Reverse elements in queue */
void q_reverse(struct list_head *head)
{
struct list_head *h = head->prev;
struct list_head *cur = head;
struct list_head *t = NULL;
list_for_each (t, head) {
cur->next = cur->prev;
cur->next->prev = cur;
h = cur;
cur = t;
}
cur->next = h;
h->prev = cur;
}
/* Reverse the nodes of the list k at a time */
void q_reverseK(struct list_head *head, int k)
{
// base cases...
if (head == NULL)
return;
if (head->next == NULL)
return;
// 1 case jo hum handle krenge...
struct list_head *cur = head;
struct list_head *first = head->next;
struct list_head *tail = first;
int len, i;
len = q_size(head);
while (len >= k) {
for (i = 1; i < k; i++) {
tail = tail->next;
}
cur->next = tail->next;
tail->next->prev = cur;
first->prev = tail;
tail->next = first;
q_reverse(first->prev);
first->next = cur->next;
cur->next->prev = first;
cur->next = tail;
tail->prev = cur;
cur = first;
first = cur->next;
tail = first;
len = len - k;
}
// // https://leetcode.com/problems/reverse-nodes-in-k-group/
}
struct list_head *merge(struct list_head *l1, struct list_head *l2)
{
// merge with pseudo node
struct list_head new_queue;
struct list_head tmp1;
struct list_head tmp2;
struct list_head *cur;
INIT_LIST_HEAD(&new_queue);
INIT_LIST_HEAD(&tmp1);
INIT_LIST_HEAD(&tmp2);
list_add_tail(&tmp1, l1);
list_add_tail(&tmp2, l2);
struct list_head *t1 = (&tmp1)->next;
struct list_head *t2 = (&tmp2)->next;
element_t *element_l1;
element_t *element_l2;
while (t1 && t2) {
if (t1->next == t1) {
list_splice_tail(&tmp2, &new_queue);
break;
} else if (t2->next == t2) {
list_splice_tail(&tmp1, &new_queue);
break;
} else {
element_l1 = container_of(t1, element_t, list);
element_l2 = container_of(t2, element_t, list);
if (strcmp(element_l1->value, element_l2->value) <= 0) {
cur = t1;
t1 = t1->next;
list_del_init(cur);
list_add_tail(cur, &new_queue);
} else {
cur = t2;
t2 = t2->next;
list_del_init(cur);
list_add_tail(cur, &new_queue);
}
}
}
l1 = new_queue.next;
list_del(&new_queue);
return l1;
}
struct list_head *mergeSortList(struct list_head *head)
{
// merge sort
if (!head || head->next == head)
return head;
// struct list_head *first = head;
struct list_head *fast = head->next;
struct list_head *slow = head;
// split list
while (fast != head && fast->next != head) {
slow = slow->next;
fast = fast->next->next;
}
// list_cut_position(&new, head, slow);
// split two list
fast = slow->next;
fast->prev = head->prev;
fast->prev->next = fast;
slow->next = head;
head->prev = slow;
// sort each list
struct list_head *l1 = mergeSortList(head);
struct list_head *l2 = mergeSortList(fast);
// merge sorted l1 and sorted l2
return merge(l1, l2);
}
/* Sort elements of queue in ascending/descending order */
void q_sort(struct list_head *head, bool descend)
{
if (!head || q_size(head) <= 1)
return;
// Bubble sort
// q_bubble_sort(head);
struct list_head *first = head->next;
list_del_init(head);
struct list_head *result = mergeSortList(first);
list_add_tail(head, result);
if (descend)
q_reverse(head);
}
/* Remove every node which has a node with a strictly less value anywhere to
* the right side of it */
int q_ascend(struct list_head *head)
{
// https://leetcode.com/problems/remove-nodes-from-linked-list/
struct list_head *cur = head->prev;
struct list_head *tmp;
char *max = container_of(head->prev, element_t, list)->value;
int size;
while (cur->prev != head) {
if (strcmp(max, container_of(cur->prev, element_t, list)->value) < 0) {
tmp = cur->prev;
cur->prev = tmp->prev;
// removemin tmp
list_del_init(tmp);
// printf("%s \n", container_of(tmp, element_t, list)->value);
free(container_of(tmp, element_t, list)->value);
free(container_of(tmp, element_t, list));
} else {
max = container_of(cur->prev, element_t, list)->value;
cur = cur->prev;
}
}
size = q_size(head);
return size;
}
/* Remove every node which has a node with a strictly greater value anywhere to
* the right side of it */
int q_descend(struct list_head *head)
{
// https://leetcode.com/problems/remove-nodes-from-linked-list/
struct list_head *cur = head->prev;
struct list_head *tmp;
char *max = container_of(head->prev, element_t, list)->value;
int size;
while (cur->prev != head) {
if (strcmp(max, container_of(cur->prev, element_t, list)->value) > 0) {
tmp = cur->prev;
cur->prev = tmp->prev;
// removemin tmp
list_del_init(tmp);
// printf("%s \n", container_of(tmp, element_t, list)->value);
free(container_of(tmp, element_t, list)->value);
free(container_of(tmp, element_t, list));
} else {
max = container_of(cur->prev, element_t, list)->value;
cur = cur->prev;
}
}
size = q_size(head);
return size;
}
/* Merge all the queues into one sorted queue, which is in ascending/descending
* order */
int __merge(struct list_head *l1, struct list_head *l2)
{
if (!l1 || !l2)
return 0;
LIST_HEAD(tmp_head);
while (!list_empty(l1) && !list_empty(l2)) {
element_t *ele_1 = list_first_entry(l1, element_t, list);
element_t *ele_2 = list_first_entry(l2, element_t, list);
element_t *ele_min =
strcmp(ele_1->value, ele_2->value) < 0 ? ele_1 : ele_2;
list_move_tail(&ele_min->list, &tmp_head);
}
list_splice_tail_init(l1, &tmp_head);
list_splice_tail_init(l2, &tmp_head);
list_splice(&tmp_head, l1);
return q_size(l1);
}
int q_merge(struct list_head *head, bool descend)
{
// https://leetcode.com/problems/merge-k-sorted-lists/
if (!head || list_empty(head))
return 0;
else if (list_is_singular(head))
return q_size(list_first_entry(head, queue_contex_t, chain)->q);
int size = q_size(head);
int count = (size % 2) ? size / 2 + 1 : size / 2;
int queue_size = 0;
for (int i = 0; i < count; i++) {
queue_contex_t *first = list_first_entry(head, queue_contex_t, chain);
queue_contex_t *second =
list_entry(first->chain.next, queue_contex_t, chain);
while (!list_empty(first->q) && !list_empty(second->q)) {
queue_size = __merge(first->q, second->q);
list_move_tail(&second->chain, head);
first = list_entry(first->chain.next, queue_contex_t, chain);
second = list_entry(first->chain.next, queue_contex_t, chain);
}
}
return queue_size;
}
void shuffle(struct list_head *head)
{
if (!head || list_empty(head))
return;
int len = q_size(head);
struct list_head *rnd;
struct list_head *tail;
struct list_head *tmp;
for (tail = head->prev; tail != head; tail = tail->prev, len--) {
rnd = head->next;
int j = rand() % (len); // Generate random index
for (int k = 0; k < j; k++) {
rnd = rnd->next;
}
if (tail == rnd) {
continue;
}
tmp = rnd->prev;
list_move(rnd, tail);
list_move(tail, tmp);
tail = rnd;
}
}