-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.c
More file actions
27 lines (23 loc) · 671 Bytes
/
queue.c
File metadata and controls
27 lines (23 loc) · 671 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
#include <stdlib.h>
#include "queue.h"
#define QUEUE_ENQUEUE_INDEX(queue) (queue->tail % queue->capacity)
#define QUEUE_DEQUEUE_INDEX(queue) (queue->head % queue->capacity)
queue *queue_new(unsigned int capacity)
{
queue *this = malloc(sizeof(queue));
this->head = -1;
this->tail = -1;
this->capacity = capacity;
this->elements = malloc((capacity)*sizeof(unsigned int));
return this;
}
void queue_enqueue(queue *queue, unsigned int element)
{
queue->tail++;
queue->elements[QUEUE_ENQUEUE_INDEX(queue)] = element;
}
unsigned int queue_dequeue(queue *queue)
{
queue->head++;
return queue->elements[QUEUE_DEQUEUE_INDEX(queue)];
}