-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.c
55 lines (42 loc) · 955 Bytes
/
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
#include "queue.h"
queue *createQueue(int size) {
queue *q;
if((q = malloc(sizeof(queue))) == NULL)
return NULL;
if((q->list = malloc(sizeof(btNode) * size)) == NULL)
return NULL;
q->size = size;
q->front = 0;
q->rear = -1;
q->itemCount = 0;
return q;
}
btNode peek(queue *q) {
return q->list[q->front];
}
bool isEmpty(queue *q) {
return q->itemCount == 0;
}
bool isFull(queue *q) {
return q->itemCount == q->size;
}
int size(queue *q) {
return q->itemCount;
}
void insert(queue *q ,btNode data) {
if(!isFull(q)) {
if(q->rear == q->size-1) {
q->rear = -1;
}
q->list[++q->rear] = data;
q->itemCount++;
}
}
btNode removeData(queue *q) {
btNode data = q->list[q->front++];
if(q->front == q->size) {
q->front = 0;
}
q->itemCount--;
return data;
}