A queue is a list with the restriction that you can insert at the front of the list and you can delete at the end of the list.
A circular queue has the advantage of constantly reusing empty spaces in the list.
typedef struct{
int que[size];
int front;
int rear;
} Queue;
The actual array of Queue.
The front of the Queue, this is where you ### [EnQueue] in the Queue. front starts at index 0.
The rear of the Queue, this is where you ### [DeQueue] in the Queue. rear starts at index 0.
The function inserts a value in rear and increases rear. if rear is greater than size we modulo front so it resets to 0.
void EnQueue(Queue* queue, int val){
queue->que[queue->rear] = val;
queue->rear = (queue->rear+1)%size;
}
The function saves front in data; increments front; and returns data. if front is greater than size we modulo front so it resets to 0.
return DeQueue(Queue* queue){
int data queue->que[queue->front];
queue->front = (queue->front+1)%size;
return data;
}
The function check if the Queue is full by checking if rear an index behind front.
bool isFull(int rear){
bool full = false;
if((rear+1)%size == front)
full = true;
return true;
}
The function check if the Queue is full by checking if rear is equal to full.
bool isEmpty(int rear){
bool empty = false;
if(rear == front)
empty = true;
return true;
}