-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
51 lines (42 loc) · 723 Bytes
/
queue.c
File metadata and controls
51 lines (42 loc) · 723 Bytes
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
#include "queue.h"
#include <stdlib.h>
Queue QueueCreate() {
Queue q = calloc (1, sizeof(struct queue));
return q;
}
void Enqueue(Queue q, void *data) {
if (q == NULL)
return;
QNode new = calloc (1, sizeof(struct node));
new->data = data;
if (q->size == 0) {
q->front = (q->back = new);
q->size++;
return;
}
q->back->next = new;
q->back = new;
q->size++;
}
void *Dequeue(Queue q) {
if (q == NULL || q->size == 0)
return NULL;
QNode del = q->front;
void *d;
q->size--;
q->front = del->next;
d = del->data;
free(del);
return d;
}
int QueueSize(Queue q) {
return q->size;
}
int QueueEmpty(Queue q) {
return q->size == 0;
}
void QueueFree(Queue q) {
if (q == NULL)
return;
free(q);
}