forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
153 lines (136 loc) · 3.58 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
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.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()
{
struct list_head *q_head = malloc(sizeof(struct list_head));
if (!q_head) {
free(q_head);
return NULL;
}
INIT_LIST_HEAD(q_head);
return q_head;
}
/* Free all storage used by queue */
void q_free(struct list_head *l)
{
if (!l) {
return;
}
element_t *entry, *safe;
list_for_each_entry_safe (entry, safe, l, list) {
q_release_element(entry);
}
free(l);
return;
}
/* 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)
{
return q_insert_head(head->prev, s);
}
/* 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 *rm_head = list_first_entry(head, element_t, list);
if (sp) {
strncpy(sp, rm_head->value, bufsize - 1);
sp[bufsize - 1] = '\0';
}
list_del(head->next);
return rm_head;
}
/* 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 *rm_tail = list_last_entry(head, element_t, list);
if (sp) {
strncpy(sp, rm_tail->value, bufsize - 1);
sp[bufsize - 1] = '\0';
}
list_del(head->prev);
return rm_tail;
}
/* Return number of elements in queue */
int q_size(struct list_head *head)
{
if (!head || list_empty(head)) {
return 0;
}
int cnt = 0;
struct list_head *node;
list_for_each (node, head) {
cnt++;
}
return cnt;
}
/* 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/
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/
return true;
}
/* Swap every two adjacent nodes */
void q_swap(struct list_head *head)
{
// https://leetcode.com/problems/swap-nodes-in-pairs/
}
/* Reverse elements in queue */
void q_reverse(struct list_head *head) {}
/* Reverse the nodes of the list k at a time */
void q_reverseK(struct list_head *head, int k)
{
// https://leetcode.com/problems/reverse-nodes-in-k-group/
}
/* Sort elements of queue in ascending order */
void q_sort(struct list_head *head) {}
/* 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/
return 0;
}
/* Merge all the queues into one sorted queue, which is in ascending order */
int q_merge(struct list_head *head)
{
// https://leetcode.com/problems/merge-k-sorted-lists/
return 0;
}