Queues using arrays

the queue is one type of linear data structure,it follows (FIFO) first in first out. the elements which are inserted first, that element will be removed first

create queue using array

define a MAX variable for a maximum size of the queue

#define MAX 5

create a queue integer array with size MAX

int Q[MAX]

create other two integers variables as front, rear, these are used to do some tasks inside queue

int front, rear;

rear is used to track of ending element

front is used to track starting element

the values of both rear, front will be zero at the beginning

insertion in queue

if (rear==MAX),then the queue is full display “Queue is full”

else, use rear value as the index number for Q,assign data into it

Q[rear] = data;

increment rear by 1 after assigning data rear++

rear is used to track the top element, so every time value of rear is incrsed while we increase the number of elements

deleting in queue

if (rear==front),there are no elements inside “queue”

else use the front as the index number for the Q and print the value out , then increment front by 1

printf("\n Deleted element from Queue is %d", Q[front]);
front++;

here ,front will be at the zeroth index at starting,if user remove zeroth element then front pointers to 2nd index element

Last updated on by vishal devxo