-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.c
More file actions
65 lines (60 loc) · 1.33 KB
/
Copy pathQueue.c
File metadata and controls
65 lines (60 loc) · 1.33 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
64
65
#include <stdlib.h>
#include <stdio.h>
#include "Queue.h"
Node NULL_NODE = {-1, -1, -1, -1};
Queue Q_init()
{
Queue q;
q.head = NULL;
q.tail = NULL;
return q;
}
void Q_enqueue(Queue* q, Node n)
{
if (q->head == NULL)
{
q->nodes[0] = n;
q->head = &q->nodes[0];
q->tail = &q->nodes[0];
}
else
{
q->tail++;
if(q->tail > &q->nodes[MAX_QUEUE_LENGTH - 1]) q->tail = &q->nodes[0];
if(q->tail != q->head)
{
*(q->tail) = n;
}
else
{
printf("Queue is full\n");
q->tail--;
if(q->tail < &q->nodes[0]) q->tail = &q->nodes[MAX_QUEUE_LENGTH - 1];
}
}
//printf("head points to %#0x8, tail points to %#0x8\n", q->head, q->tail);
}
Node Q_dequeue(Queue* q)
{
Node res = NULL_NODE;
if(q->head != NULL)
{
res = *(q->head);
q->head++;
if(q->head > &q->nodes[MAX_QUEUE_LENGTH - 1]) q->head = &q->nodes[0];
if(q->head == q->tail+1 || (q->tail == &q->nodes[MAX_QUEUE_LENGTH - 1] && q->head == &q->nodes[0]))
{
q->head = NULL;
q->tail = NULL;
}
}else
{
printf("Empty queue!\n");
}
return res;
}
bool Q_notEmpty(Queue* q)
{
bool res = q->head == NULL ? false : true;
return res;
}