-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.h
60 lines (56 loc) · 1.16 KB
/
queue.h
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
struct QueueElement
{
struct QUEUE_TYPE value;
struct QueueElement *next;
};
struct Queue
{
int count;
struct QueueElement *first;
struct QueueElement *last;
};
bool QueueIsEmpty(struct Queue *queue)
{
return((queue->count) < 1);
}
void AddToQueue(struct Queue *queue, struct QUEUE_TYPE value)
{
struct QueueElement *element = malloc(sizeof(struct QueueElement));
element->value = value;
element->next = NULL;
if(QueueIsEmpty(queue))
{
queue->first = element;
queue->last = element;
}
else
{
queue->last->next = element;
queue->last = element;
}
(queue->count)++;
}
struct QUEUE_TYPE GetFromQueue(struct Queue *queue)
{
if(!QueueIsEmpty(queue))
{
struct QueueElement *element;
element = queue->first;
queue->first = element->next;
(queue->count)--;
struct QUEUE_TYPE value = element->value;
free(element);
return value;
}
else
{
printf("Queue is empty!\n");
exit(-1);
}
}
void InitializeQueue(struct Queue *queue)
{
queue->count = 0;
queue->first = NULL;
queue->last = NULL;
}