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
253 lines (220 loc) · 5.68 KB
/
Copy pathqueue.c
File metadata and controls
253 lines (220 loc) · 5.68 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
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "harness.h"
/*
* Create empty queue.
* Return NULL if could not allocate space.
*/
queue_t *q_new()
{
queue_t *q = malloc(sizeof(queue_t));
if (q) {
q->head = NULL;
q->tail = NULL;
q->size = 0;
}
return q;
}
/* Free all storage used by queue */
void q_free(queue_t *q)
{
if (!q)
return;
list_ele_t *tmp = q->head;
while (q->head) {
q->head = q->head->next;
tmp->next = NULL;
free(tmp->value);
free(tmp);
tmp = q->head;
}
free(q);
}
/*
* Attempt to insert element at head of queue.
* Return true if successful.
* Return false if q is NULL or could not allocate space.
* Argument s points to the string to be stored.
* The function must explicitly allocate space and copy the string into it.
*/
bool q_insert_head(queue_t *q, char *s)
{
if (!q)
return false;
// Check separately to avoid extra malloc cause memory leak
list_ele_t *newh; // newh means new element in head
newh = malloc(sizeof(list_ele_t));
if (!newh) {
return false;
}
// Allocate space and copy the string
newh->value = malloc(sizeof(char) * (strlen(s) + 1));
if (!newh->value) {
free(newh);
return false;
}
memset(newh->value, '\0', strlen(s) + 1);
strncpy(newh->value, s, strlen(s));
newh->next = q->head;
q->head = newh;
// first time
if (!q->tail) {
q->tail = q->head;
} else {
while (q->tail->next) {
q->tail = q->tail->next;
}
}
q->size += 1;
return true;
}
/*
* Attempt to insert element at tail of queue.
* Return true if successful.
* Return false if q is NULL or could not allocate space.
* Argument s points to the string to be stored.
* The function must explicitly allocate space and copy the string into it.
*/
bool q_insert_tail(queue_t *q, char *s)
{
if (!q)
return false;
// Check separately to avoid extra malloc cause memory leak
list_ele_t *newt; // newt means new element in tail
newt = malloc(sizeof(list_ele_t));
if (!newt)
return false;
newt->value = malloc(sizeof(char) * strlen(s) + 1);
if (!newt->value) {
free(newt);
return false;
}
memset(newt->value, '\0', strlen(s) + 1);
strncpy(newt->value, s, strlen(s));
newt->next = NULL;
if (!q->tail) {
q->tail = q->head = newt;
} else {
q->tail->next = newt;
q->tail = newt;
}
q->size += 1;
return true;
}
/*
* Attempt to remove element from head of queue.
* Return true if successful.
* Return false if queue is NULL or empty.
* If sp is non-NULL and an element is removed, copy the removed string to *sp
* (up to a maximum of bufsize-1 characters, plus a null terminator.)
* The space used by the list element and the string should be freed.
*/
bool q_remove_head(queue_t *q, char *sp, size_t bufsize)
{
if (!q || !q->head) {
return false;
}
if (sp) {
// Insure copy size is right
size_t realbufsize = (bufsize > strlen(q->head->value))
? strlen(q->head->value)
: bufsize - 1;
memset(sp, '\0', realbufsize + 1);
strncpy(sp, q->head->value, realbufsize);
}
list_ele_t *tmp;
tmp = q->head;
q->head = q->head->next;
tmp->next = NULL;
free(tmp->value);
free(tmp);
q->size -= 1;
return true;
}
/*
* Return number of elements in queue.
* Return 0 if q is NULL or empty
*/
int q_size(queue_t *q)
{
return !q ? 0 : q->size;
}
/*
* 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(queue_t *q)
{
// No effect if q is NULL or empty or only one element
if (!q || q->size < 2) {
return;
}
list_ele_t *tmp;
tmp = q->head->next;
q->tail->next = q->head;
while (tmp != q->tail) {
tmp = tmp->next;
q->head->next->next = q->tail->next;
q->tail->next = q->head->next;
q->head->next = tmp;
}
q->tail = q->head;
q->head = q->head->next;
q->tail->next = NULL;
}
/*
* 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.
*/
void merge_sort(list_ele_t **head)
{
if (!(*head) || !((*head)->next))
return;
list_ele_t *l1 = (*head)->next; // faster pointer
list_ele_t *l2 = *head; // slower pointer
// split list
while (l1 && l1->next) {
l2 = l2->next;
l1 = l1->next->next;
}
l1 = l2->next;
l2->next = NULL;
l2 = *head;
// Recursively split until each list exist one element
merge_sort(&l2); // the left linked list
merge_sort(&l1); // the right linked list
// merge sorted l1 and sorted l2
// reuse head to record the head of new list
*head = NULL;
list_ele_t **tmp = head;
while (l1 && l2) {
if (strcmp(l1->value, l2->value) < 0) { // l1 < l2
*tmp = l1;
l1 = l1->next;
} else {
*tmp = l2;
l2 = l2->next;
}
tmp = &((*tmp)->next);
}
// Either l1 or l2 will left
*tmp = l1 ? l1 : l2;
}
void q_sort(queue_t *q)
{
// if q has only one element or q is empty, q->head == q->tail
if (!q || q->head == q->tail) {
return;
}
// Merge sort
merge_sort(&q->head);
while (q->tail->next) {
q->tail = q->tail->next;
}
}