-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
72 lines (54 loc) · 1.01 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
#include "queue.h"
#include <stdlib.h>
Queue *CreateQueue()
{
Queue *q = malloc(sizeof(Queue));
if (q == NULL)
return NULL;
q->head = q->tail = NULL;
return q;
}
void DestroyQueue(Queue *q)
{
if (q == NULL)
return;
while (Dequeue(q) != NULL)
;
free(q);
}
int IsQueueEmpty(Queue *q)
{
return (q->tail == NULL && q->head == NULL);
}
void Enqueue(Queue *q, void *data)
{
Node *node = malloc(sizeof(Node));
node->data = data;
node->next = NULL;
if (q->tail == NULL)
{
q->head = q->tail = node;
return;
}
q->tail->next = node;
q->tail = node;
}
void *Dequeue(Queue *q)
{
Node *previousHead;
void *data;
previousHead = q->head;
if (previousHead == NULL)
return NULL;
q->head = q->head->next;
if (q->head == NULL)
q->tail = NULL;
data = previousHead->data;
free(previousHead);
return data;
}
void *Top(Queue *q)
{
void *data = q->head->data;
return data;
}