Unit - 3 Queue
Unit - 3 Queue
Introduction:
A queue is a non-primitive linear data structure. it is a homogeneous collection of elements in
which new elements are added at one end called the REAR end, and the existing elements are
deleted from other end called FRONT end. A queue is logically a First in First out (FIFO) or First
Come First Serve (FCFS) type of list. Queue means line. Example- The line for ticket in movie
hall or cars in line at a petrol station.
Operation of queue:-
Queue as an ADT performs following operation
1. Insertion operation:- data inserted in a queue is called enqueue. At the time of enqueue
operation it checks the rear pointer, if it is greater than queue size then show “queue
overflow” message and exit
Algorithm
QINSERT [QUEUE [maxSIZE],ITEM
This algorithm inserts an item at the rear of the queue (maxSIZE)
Step 1 : initialization
Set front =-1
Set rear = -1
Step 2 : repeat steps 3 to 5 – until rear < maxSIZE-1
Step 3 : read item
Step 4 : if front == -1 then
Front=0
Rear=0
Else
Rear=rear+1
Endif
Step 5 : set queue [rear]=item
Step 6 : print, queue overflow
Example:-
Queue is declared as
int queue[5],front=-1and rear=-1;
Void enqueue()
{
Int item;
If(rear<=4)
{
Printf(”enter the number”);
Scanf(“%d”,item);
If(front==-1)
{
2. Deletion operation:- data deletion form queue is called dequeue. At the time of
dequeue operation it checked the front pointer; if it is greater than rear then it shows
“queue is underflow” message and exit.
Algorithm
QDELETE [QUEUE [maxSIZE],ITME
This algorithm deletes an item at the front of the queue (maxSIZE)
Step 1 : repeat stages 2 to 4 until front>=0
Step 2 : set item= queue [front]
Step 3 : if front == rear
Set Front=-1
Set Rear=-1
Else
front=front+1
Endif
Step 4 : print, no deleted is, item
Step 5 : print, “queue is empty”
Example:-
Queue is declared as
int queue[5],front=-1and rear=-1;
Void delete()
{
Int item;
If(front!=-1)
{
Item=queue[front];
void enqueue()
{
int x;
printf("\nEnter the data you want to insert in queue: ");
scanf("%d",&x);
if(rear==N-1)
{
printf("\nQueue is Overflow");
}
else if(front==-1&&rear==-1)
{
front=rear=0;
queue[rear]=x;
// printf("\n\tInserted item is: %d",x);
}
else
{
rear=rear+1;
queue[rear]=x;
// printf("\n\tInserted item is: %d",x);
}
}
void dequeue()
{
if(front==-1&&rear==-1)
{
printf("\nQueue is Underflow");
}
else if(front==rear)
{
[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 6
front=rear=-1;
}
else
{
printf("\n\tDeleted item is: %d",queue[front]);
front=front+1;
}
}
void display()
{
int i;
if(front==-1&&rear==-1)
{
printf("\nQueue is Empty");
}
else
{
for(i=front;i<rear+1;i++)
{
printf("\nQueue[%d]=%d",i,queue[i]);
}
}
}
void main()
{
int ch;
do
{
printf("\nEnter the choice: 1-Enqueue, 2-Dequeue, 3-Display, 0-Exit : ");
scanf("%d",&ch);
switch(ch)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 7
default:
printf("\nInvalid choice, please choose 0-3");
}
}while(ch!=0);
return 0;
}
Circular Queue:
A circular queue is one in which the insertion of a new element is done at the very first
location of the queue if the last location of the queue is full. In other words if we have a queue
Q of say N elements, then after inserting an element last ) i.e., in the n-1th) location of the
array the next elements will be inserted at the very first location (i.e., location with subscript
0) of the array. It is possible to insert new elements, if and only if those locations are empty. A
circular queue overcomes the problems of unutilized space in linear queues implemented as
arrays. A circular queue also has a FRONT and REAR to keep track of the elements to be
deleted and inserted and therefore maintains the unique characteristics of the queue. The
below assumptions are made:
1. FRONT will always be pointing to the first element (as in the linear queue)
2. If FRONT=REAR the queue will be empty
3. Each time a new element is inserted into the queue the REAR is incremented by one
REAR=REAR+1
4. Each time an element is deleted from the queue the value of FRONT is increased by one.
FRONT=FRONT+1
Item=queue[FRONT]
If(FRONT=REAR) then
Set FRONT=-1
Set REAR=-1
Else
FRONT=(FRONT+1)%MAX_SIZE
End if
End if
Setp 2: Exit
if(front==rear)
front=rear=-1;
else
front=(front+1)%MAXSIZE;
}
}
switch(choice)
{
case 1: printf("enter the element which is to be inserted:");
scanf("%d",&x);
enqueue(x);
break;
case 2: dequeue();
break;
case 3: display();
}
[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 12
}
getch();
}
Priority Queue:
A priority queue is a collection of elements such that each element has been assigned a
priority and the order in which elements are deleted and processed comes from the following
rules:
1. An elements with the same priority is processed before any element of lower priority
2. Two elements with the same priority are processed according to the order in which
they were added to the queue
https://www.javatpoint.com/ds-types-of-queues