Queue Using Arrays
Queue Using Arrays
What is a Queue?
Queue is a linear data structure in which the insertion and deletion operations are performed at
two different ends The insertion is performed at one end called REAR and deletion is performed
at another end called FRONT. In queue data structure, the insertion and deletion operations are
performed based on FIFO (First In First Out) principle.
In a queue data structure, the insertion operation is performed using a function called
"enQueue()" and deletion operation is performed using a function called "deQueue()".
Implementation of a Queue
Queue data structure can be implemented in two ways. They are as follows...
1. Using Array
2. Using Linked List
Before we implement actual operations, first follow the below steps to create an empty queue.
Step 1 - Include all the header files which are used in the program and define a
constant 'SIZE' with specific value.
Step 2 - Declare all the user defined functions which are used in queue
implementation.
Step 3 - Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int
front = -1, rear = -1)
Step 5 - Then implement main method by displaying menu of operations list and make
suitable function calls to perform operation selected by the user on queue.
display() - Displays the elements of a Queue
We can use the following steps to display the elements of a queue...
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
int q[SIZE],front=-1,rear=-1;
void enque(int);
void deque();
void display();
int main()
int choice,ele;
char op;
do
printf("Menu");
printf("\n 1.Enque");
printf("\n 2.Deque");
printf("\n 3.DISPLAY");
scanf("%d",&choice);
switch(choice)
{
case 1:
scanf("%d",&ele);
enque(ele);
break;
case 2:deque();break;
case 3:display();break;
default:
printf("\nInvalid choice");
fflush(stdin);
scanf(" %c",&op);
}while(op=='y'||op=='Y');
return 0;
if(rear==SIZE-1)
else if(rear==-1)
rear++;
front++;
q[rear]=ele;
else
rear++;
q[rear]=ele;
void deque()
if(front==-1 || front==rear+1)
else
front++;
void display()
int i;
if(front==-1 || front==rear+1)
{
else
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);