forked from nicolas-flasque-efrei/MARC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
41 lines (37 loc) · 930 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
//
// Created by flasque on 19/10/2024.
//
#include <assert.h>
#include <stdlib.h>
#include "queue.h"
/**
* @brief Function to create a queue
* @param size : the size of the queue
* @return the queue
*/
t_queue createQueue(int size)
{
// the size of the queue must be positive
assert(size > 0);
t_queue queue;
queue.size = size;
queue.first = 0;
queue.last = 0;
queue.values = (t_position *)malloc(size * sizeof(t_position));
return queue;
}
void enqueue(t_queue *p_queue, t_position pos)
{
// the queue must not be full
assert((p_queue->last - p_queue->first) < p_queue->size);
p_queue->values[(p_queue->last) % p_queue->size] = pos;
p_queue->last++;
return;
}
t_position dequeue(t_queue *p_queue)
{
// the queue must not be empty
assert(p_queue->last != p_queue->first);
p_queue->first++;
return p_queue->values[(p_queue->first - 1) % p_queue->size];
}