forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
320 lines (295 loc) · 8.68 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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "harness.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
*/
/* Create an empty queue */
struct list_head *q_new()
{
element_t *node = malloc(sizeof(element_t));
if (!node)
return NULL;
struct list_head *head = &node->list;
INIT_LIST_HEAD(head);
return head;
}
/* Free all storage used by queue */
void q_free(struct list_head *l)
{
if (!l)
return;
struct list_head *ptr = l->next;
while (ptr != l) {
element_t *node = list_entry(ptr, element_t, list);
ptr = ptr->next;
q_release_element(node);
}
element_t *node = list_entry(l, element_t, list);
free(node);
}
/* Insert an element at head of queue */
bool q_insert_head(struct list_head *head, char *s)
{
if (!head)
return false;
element_t *node = malloc(sizeof(element_t));
if (!node)
return false;
int charsize = strlen(s);
char *value = malloc(charsize + 1);
if (!value) {
free(node);
return false;
}
memcpy(value, s, charsize);
value[charsize] = '\0';
node->value = value;
list_add(&node->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 *node = malloc(sizeof(element_t));
if (!node)
return false;
int charsize = strlen(s);
char *value = malloc(charsize + 1);
if (!value) {
free(node);
return false;
}
memcpy(value, s, charsize);
value[charsize] = '\0';
node->value = value;
list_add_tail(&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_entry(head->next, element_t, list);
if (sp) {
memcpy(sp, node->value, bufsize - 1);
sp[bufsize - 1] = '\0';
}
list_del(head->next);
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_entry(head->prev, element_t, list);
if (sp) {
memcpy(sp, node->value, bufsize - 1);
sp[bufsize - 1] = '\0';
}
list_del(head->prev);
return node;
}
/*
* WARN: This is for external usage, don't modify it
* Attempt to release element.
*/
/* Return number of elements in queue */
int q_size(struct list_head *head)
{
int num = 0;
struct list_head *node;
if (head)
list_for_each (node, head)
num += 1;
return num;
}
/*
* Delete the middle node in list.
* The middle node of a linked list of size n is the
* ⌊n / 2⌋th node from the start using 0-based indexing.
* If there're six element, the third member should be return.
* Return true if successful.
* Return false if list is NULL or empty.
* Ref: https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/
*/
bool q_delete_mid(struct list_head *head)
{
if (!head || list_empty(head))
return false;
struct list_head *slowptr = head->next;
for (struct list_head *fastptr = head->next;
fastptr != head && fastptr->next != head;
fastptr = fastptr->next->next) {
slowptr = slowptr->next;
}
element_t *node = list_entry(slowptr, element_t, list);
list_del(slowptr);
q_release_element(node);
return true;
}
/*
* Delete all nodes that have duplicate string,
* leaving only distinct strings from the original list.
* Return true if successful.
* Return false if list is NULL.
*
* Note: this function always be called after sorting, in other words,
* list is guaranteed to be sorted in ascending order.
* Ref: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
*/
bool q_delete_dup(struct list_head *head)
{
if (!head || list_empty(head))
return false;
struct list_head *ckp = head->next;
element_t *ckpnode = list_entry(ckp, element_t, list);
bool ckpisdup = false;
for (struct list_head *ptr = ckp->next; ptr != head; ptr = ckp->next) {
element_t *ptrnode = list_entry(ptr, element_t, list);
if (strcmp(ckpnode->value, ptrnode->value) == 0) {
list_del(ptr);
q_release_element(ptrnode);
ckpisdup = true;
} else {
if (ckpisdup) {
list_del(ckp);
q_release_element(ckpnode);
ckpisdup = false;
}
ckp = ptr;
ckpnode = list_entry(ckp, element_t, list);
}
}
if (ckpisdup) {
list_del(ckp);
q_release_element(ckpnode);
}
return true;
}
/* Swap every two adjacent nodes */
void q_swap(struct list_head *head)
{
if (!head || list_empty(head))
return;
struct list_head *ptr = head;
while (ptr->next != head && ptr->next->next != head) {
// swap next pointer
struct list_head *tmp = ptr->next->next->next;
ptr->next->next->next = ptr->next;
ptr->next = ptr->next->next;
ptr->next->next->next = tmp;
// swap prev pointer
ptr->next->next->next->prev = ptr->next->next;
ptr->next->next->prev = ptr->next;
ptr->next->prev = ptr;
ptr = ptr->next->next;
}
}
/*
* Reverse elements in queue
* No effect if q is NULL or empty
* This function should not allocate or free any list elements
* (e.g., by calling q_insert_head, q_insert_tail, or q_remove_head).
* It should rearrange the existing ones.
*/
void q_reverse(struct list_head *head)
{
struct list_head *ptr = head;
if (ptr && !list_empty(ptr)) {
do {
struct list_head *tmp = ptr->prev;
ptr->prev = ptr->next;
ptr->next = tmp;
ptr = ptr->prev;
} while (ptr != head);
}
}
/*
* Sort elements of queue in ascending order
* No effect if q is NULL or empty. In addition, if q has only one
* element, do nothing.
*/
struct list_head *mergeTwoLists(struct list_head *L1, struct list_head *L2)
{
struct list_head *head = NULL, **ptr = &head, **cur;
for (cur = NULL; L1 && L2; *cur = (*cur)->next) {
// compare accending pair by pair
element_t *node1 = list_entry(L1, element_t, list);
element_t *node2 = list_entry(L2, element_t, list);
cur = (strcmp(node1->value, node2->value) <= 0) ? &L1 : &L2;
*ptr = *cur;
ptr = &(*ptr)->next;
}
*ptr = (struct list_head *) ((uintptr_t) L1 | (uintptr_t) L2);
return head;
}
static struct list_head *mergesort_list(struct list_head *head)
{
if (!head->next)
return head;
struct list_head *slow = head;
for (struct list_head *fast = head->next; fast && fast->next;
fast = fast->next->next)
slow = slow->next;
struct list_head *mid = slow->next;
slow->next = NULL;
struct list_head *left = mergesort_list(head), *right = mergesort_list(mid);
return mergeTwoLists(left, right);
}
void q_sort(struct list_head *head)
{
if (!head || list_empty(head))
return;
head->prev->next = NULL;
head->next = mergesort_list(head->next);
// reassign the prev ptr
struct list_head *ptr = head;
for (; ptr->next; ptr = ptr->next)
ptr->next->prev = ptr;
ptr->next = head;
head->prev = ptr;
}
// iterate version
// void q_sort(struct list_head *head)
// {
// if (!head || list_empty(head))
// return;
// head->prev->next = NULL;
// int listsSize = 0;
// struct list_head *lists[150000] = {NULL};
// // divide to single node
// struct list_head *node = head->next;
// while (node) {
// struct list_head *iter = node;
// while (iter && iter->next &&
// list_entry(iter, element_t, list)->value <
// list_entry(iter->next, element_t, list)->value)
// iter = iter->next;
// lists[listsSize++] = node;
// node = iter->next;
// iter->next = NULL;
// }
// // merge K sorted lists
// while (listsSize > 1) {
// for (int i = 0, j = listsSize - 1; i < j; i++, j--)
// lists[i] = mergeTwoLists(lists[i], lists[j]);
// listsSize = (listsSize + 1) / 2;
// }
// // reassign the prev ptr
// head->next = lists[0];
// struct list_head *ptr = head;
// for (; ptr->next; ptr = ptr->next)
// ptr->next->prev = ptr;
// ptr->next = head;
// head->prev = ptr;
// }