-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
63 lines (56 loc) · 1.07 KB
/
Copy pathqueue.c
File metadata and controls
63 lines (56 loc) · 1.07 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
#include "queue.h"
QUEUElink QUEUEnewElement(void *item, QUEUElink next)
{
QUEUElink x = (QUEUElink)malloc(sizeof(struct QUEUEnode));
x->item = item;
x->next = next;
return x;
}
Q QUEUEinit()
{
Q q = (Q)malloc(sizeof(struct queue));
q->head = NULL;
q->tail = NULL;
q->size = 0;
return q;
}
int QUEUEempty(Q q)
{
return q->head == NULL;
}
void QUEUEinsert(Q q, void* item)
{
q->size += 1;
if (q->head == NULL) {
q->tail = QUEUEnewElement(item, q->head);
q->head = q->tail;
return;
}
q->tail->next = QUEUEnewElement(item, q->tail->next);
q->tail = q->tail->next;
}
void* QUEUEdequeue(Q q)
{
if (q->head == NULL) {
return NULL;
}
q->size -= 1;
void* item = q -> head -> item;
QUEUElink t = q -> head -> next;
free(q->head);
q->head = t;
return item;
}
void QUEUEclear(Q q)
{
while (q->head != NULL) {
q->size -= 1;
QUEUElink t = q -> head -> next;
free(q->head);
q->head = t;
}
}
int QUEUEsize(Q q)
{
return q->size;
}