-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
171 lines (142 loc) · 3.5 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
/*
* queue.c by Tao Wang (Peter)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h"
struct node {
struct node *next;
void *content;
};
struct head {
struct node *root;
};
void nclose(struct node *qp);
int nput(struct node *qp, void *elementp);
void napply(struct node *qp, void (*fn)(void *elementp));
/* create an empty queue */
queue_t *
qopen(void) {
struct head *ret = calloc(1, sizeof(struct node *));
ret->root = calloc(2, sizeof(void *));
return (queue_t *)ret;
}
/* deallocate a queue, frees everything in it, a wrapper */
void
qclose(queue_t *qp) {
nclose(((struct head *)qp)->root);
free(qp);
}
/* encapsulated close function */
void
nclose(struct node *qp) {
if (qp == NULL)
return;
if (qp->next != NULL)
nclose(qp->next);
free(qp);
}
/* put element at end of queue */
int
qput(queue_t *qp, void *elementp) {
return nput(((struct head *)qp)->root, elementp);
}
/* encapsulated put function */
int
nput(struct node *qp, void *elementp) {
struct node *temp = qp;
if (elementp != NULL && temp->content == elementp) {
return -1;
}
if (temp->next == NULL) {
temp->next = calloc(2, sizeof(void *));
temp->content = elementp;
return 0; /* success */
}
else {
return nput(temp->next, elementp);
}
}
/* get first element from queue, and remove it */
void *
qget(queue_t *qp) {
struct node *temp;
void *ret;
if (((struct head *)qp)->root == NULL)
return NULL;
temp = ((struct head *)qp)->root;
ret = temp->content;
((struct head *)qp)->root = temp->next;
if (((struct head *)qp)->root == NULL)
((struct head *)qp)->root = calloc(2, sizeof(void *));
free(temp);
return ret;
}
/* apply a void function to every element of a queue */
void
qapply(queue_t *qp, void (*fn)(void *elementp)) {
napply(((struct head *)qp)->root, fn);
}
/* encapsulated apply function */
void
napply(struct node *qp, void (*fn)(void *elementp)) {
if (qp->next != NULL)
napply(qp->next, fn);
if (qp->content != NULL)
fn(qp->content);
}
/* search a queue using a supplied boolean function, returns an element */
void *
qsearch(queue_t *qp, int (*searchfn)(void* elementp, const void* keyp), const void* skeyp) {
struct node *temp = ((struct head *)qp)->root;
if (temp == NULL || temp->next == NULL)
return NULL;
while (!searchfn(temp->content, skeyp)) {
temp = temp->next;
if (temp == NULL || temp->next == NULL)
return NULL;
}
return temp->content;
}
/* search a queue using a supplied boolean function, removes and
* returns the element */
void *
qremove(queue_t *qp, int (*searchfn)(void* elementp,const void* keyp), const void* skeyp) {
struct node *temp = ((struct head *)qp)->root;
void *pre = NULL;
void *ret;
if (temp == NULL || temp->content == NULL)
return NULL;
while (!searchfn(temp->content, skeyp)) {
if (temp->next == NULL)
return NULL;
else {
pre = temp;
temp = temp->next;
if (temp->content == NULL)
return NULL;
}
}
ret = temp->content;
if (pre == NULL)
((struct head *)qp)->root = temp->next;
else
((struct node *)pre)->next = temp->next;
return ret;
}
/* concatenatenates elements of q2 into q1, q2 is dealocated upon completion */
void
qconcat(queue_t *q1p, queue_t *q2p) {
struct node *temp = ((struct head *)q1p)->root;
if (((struct head *)q2p)->root->content == NULL) {
return;
}
if (((struct head *)q1p)->root->content == NULL) {
((struct head *)q1p)->root = ((struct head *)q2p)->root;
return;
}
while (temp->next->next != NULL)
temp = temp->next;
temp->next = ((struct head *)q2p)->root;
}