-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
34 lines (27 loc) · 724 Bytes
/
queue.c
File metadata and controls
34 lines (27 loc) · 724 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
#include "queue.h"
#include "safe_stdlib.h"
void initQueue(Queue *queue, int n){
queue->begin = 0;
queue->end = 0;
queue->n = n + 1;
queue->list = (int *) safeMalloc(queue->n * sizeof (int));
}
void releaseQueue(Queue *queue){
free(queue->list);
}
void push(Queue *queue, int elem){
queue->list[queue->end] = elem;
queue->end = (queue->end + 1) % queue->n;
}
int pop_front(Queue *queue){
int elem = queue->list[queue->begin];
queue->begin = (queue->begin + 1) % queue->n;
return elem;
}
int pop_back(Queue *queue){
queue->end = (queue->end - 1 + queue->n) % queue->n;
return queue->list[queue->end];
}
bool empty(Queue *queue){
return queue->begin == queue->end;
}