Queue is a linear data structure that follows the FIFO (first-in, first-out) principle. Elements are added to the rear of the queue and removed from the front. A queue can be implemented using an array or linked list, with front and rear pointers indicating the first and last elements. To add an element, the rear pointer is incremented and the element is inserted at that position. To remove an element, it is accessed via the front pointer before incrementing the front pointer. This allows efficient O(1) insertion and removal of elements from the queue.
1. Queue
Definition
Queue is a linear list where all additions are made at one end, called rear, and all deletions
(accesses) are made from another end called front of the list. So, in a queue there must be two
indicators or pointers. One is rear to add elements and another is front used to delete (access) the
elements from the queue.
Queue is a FIFO (First In First Out) structure. That means the element that is added first will be
deleted (accessed) first. As, stack. Queue can be implemented using array and linked list.
1 2 3 4 5 6 7 8 9 10
A C F J P
a) Array based queue
40 48 59 67 63
(b) A link based queue
Fig. 1: Graphical representation of queue
Array based queue
The queue that will be created using an array is called array based queue. Here, we have to use
two indicators (two index identifiers). One indicator will mark the front element and another will
mark the rear element of the queue.
Addition of an element in an array based queue
We know in a queue element is added at the rear. So, to add an item at first we increase rear index
and then place the element in the array based queue using the rear index.
Algorithm to add an element to queue
1. Declare array based queue and other variables:
que[1…..M], item, front, rear;
2. if(rear=0)
{
front=rear=1;
que[rear]=item;
}
3. if(rear<M)
front rear
frontptr reartptr
2. {
rear=rear+1;
que[rear]=item;
}
else print “Over Flow message”
4. Output: Updated queue.
Example
1 2 3 4 5 6
(a) An empty queue and item (35)
1 2 3 4 5 6
35
(b) Adding the first element of the queue
1 2 3 4 5 6
35 48 25 13
(c) An array based queue and item (59)
1 2 3 4 5 6
35 48 25 13
(d) Advancement of the rear
1 2 3 4 5 6
35 48 25 13 59
(e) Insert item in que[rear]
Fig.2: Addition of new items in an array based queue (Pictorial view)
que[6] front=rear=0
item=35
front=rear=1
que[1]=35
front rear
item=59
front=1
rear=4
front rear
rear=rear+1=5
front rear
que[rear]=item
que[5]=59
front rear
3. Deletion of an element from a queue
We know that, the element is deleted from the front of the queue. So, at first we access the
element, and then we increase the front index.
Algorithm to delete an element from a queue
1. Declare array based queue and other variables:
que[1….M], item, rear, front;
2. if(front=rear)
{
item=que[front];
front=rear=0;
}
3. if(front !=0)
{
item=que[front];
front=front+1;
}
else print “Queue is empty”
4. Output: Updated queue
Example
1 2 3 4 5 6
35 48 25 13 59
(a) An array based queue
1 2 3 4 5 6
48 25 13 59
(b) Deleting the first element of the queue
front=1
rear=5
front rear
que[6]
item=que[front]=35
front=front+1
front=2 rear=5
front rear
4. 1 2 3 4 5 6
59
(c) The queue with only one element
1 2 3 4 5 6
(d) Deletion of the last element of the queue
Fig. 3: Deletion process of an array based queue (Pictorial view)
Sample Code
#include <stdio.h>
void insert();
void delete();
void display();
int rear = - 1;
int front = - 1;
int MAX;
int queue_array[100];
main()
{
int choice;
printf("Enter the size of the queue: ");
scanf("%d",&MAX);
while (1)
{
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
front=rear
front rear
item=que[front]=59
front=rear=0
5. printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice n");
}
}
}
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow n");
else
6. {
if (front == - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow n");
return ;
}
else
{
printf("Element deleted from queue is : %dn",
queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if (front == - 1)
printf("Queue is empty n");
else
7. {
printf("Queue is : n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
}