0% found this document useful (0 votes)
9 views14 pages

Unit - 3 Queue

The document provides an overview of queues as a linear data structure, explaining their operations such as enqueue (insertion) and dequeue (deletion). It discusses the limitations of simple queues implemented with arrays and introduces circular queues as a solution to these limitations. Additionally, it covers priority queues and double-ended queues (DEQUE), highlighting their unique characteristics and operational methods.

Uploaded by

rabinbruhh777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views14 pages

Unit - 3 Queue

The document provides an overview of queues as a linear data structure, explaining their operations such as enqueue (insertion) and dequeue (deletion). It discusses the limitations of simple queues implemented with arrays and introduces circular queues as a solution to these limitations. Additionally, it covers priority queues and double-ended queues (DEQUE), highlighting their unique characteristics and operational methods.

Uploaded by

rabinbruhh777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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.

The Queue as an ADT:


If queue is implemented using arrays, we must be sure about the exact number of
elements we want to store in the queue, because we have to declare the size of the array at
design time or before the processing starts. The beginning of array will become the front for
the queue and the last location of the array will act as rear for the queue. When implemented
using arrays. Front – rear +. Also note that of rear<front then there will be no elements in the
queue or queue will always empty
▪ In programming, a queue is a sequence of data elements.
▪ In the sequence:
o Items can be removed only at the Front/Head(f)
o Items can be added only at the Rear/Tail(R)
o Referred to as a "First In First Out" or FIFO Container.
o Also called "First Come First Served" of FCFS
▪ Basic Operations:
o bool empty()
Returns True if the queue is empty, and False otherwise.
o bool full()
Returns True if the queue is full, and False otherwise
o void Enqueue
add element to back
o void Dequeue
remove element from front and print it
[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 1
o void Front
retrieve element from front and print it
o void Clear
remove all elements

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)
{

[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 2


Front=0;
Rear=0;
Queue[rear]=item;
}
Else
{
Rear=rear+1;
Queue[rear]=item;
}
}
Else
{ Printf(”queue is full”); }
}

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];

[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 3


If(front==rear)
{
Front=-1;
Rear=-1;
}
Else
{
front=front+1;
printf(”deleted item is:”,item);
}
}
Else
{
Printf(”queue is Empty”);
}
}

Full Program of Queue (Static Implementation using Array):


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define N 5
int queue[N];
int front=-1;
int rear=-1;
void enqueue(int x)
{
if(rear==N-1)
{ printf("Queue overflow"); }
else if(front==-1&&rear==-1)
{
front=rear=0;
queue[rear]=x;
}
else
{
rear=rear+1;
queue[rear]=x;
}

[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 4


}
void dequeue()
{
if(front==-1&&rear==-1)
{ printf("queue is empty"); }
else if(front==rear)
{ front=rear=-1; }
else
{
printf("deleted item is : %d \n",queue[front]);
front=front+1;
}
}
void display()
{
if(front==-1&&rear==-1)
{ printf("queue empty"); }
else
{
for(int i=front;i<rear+1;i++)
{ printf("queue index[%d]=%d\n",i,queue[i]); }
}
}
void peek()
{
if(front==-1&&rear==-1)
{ printf("queue is empty"); }
else
{ printf("front data is %d\n", queue[front]); }
}
void main()
{
enqueue(2);
enqueue(5);
enqueue(-1);
display();
peek();
dequeue();
peek();
display();
[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 5
}
//Static implementation of Queue using array – Example 2
#include<stdio.h>
#include<conio.h>
#define N 5
int queue[N];
int front=-1, rear=-1;

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;
}

Limitations of Linear/Simple queue:


There are certain problems associated with a simple queue when queue is implemented
using arrays. In a situation, when we insert some elements in queue and delete some
elements from them and also if REAR points to the last location of the array. Then if we
attempt to add more elements, the elements can't be inserted, because in a queue the new
elements are always added from the rear end, and rear here indicates to last location of the
array. Hence though the space is there in the queue we are not able to insert the element is
deleted, shift all afterward elements to the left by one position. But if the queue is too large of
say 5000 elements it will be a difficult job to do and time consuming too. To remove this
problem we use circular queue.

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

[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 8


Algorithm for adding an element in a circular queue using arrays:
QINSERT (QUEUE[MAX_SIZE], item)
Step 1: if (FRONT = (REAR+1) % MAX_SIZE)
Print(“Queue is Overflow”)
Exit
Else
Read item
If(front=-1) then
Set FRONT=0
Set REAR=0
Else
REAR=(REAR+1)%MAX_SIZE
Endif
QUEUE[REAR]=item
End if
Setp 2: Exit
Program for adding an element in a circular queue using arrays:
void cqinsert()
{
int num;
if(front==(rear+1)%MAXSIZE)
{ printf("Queue is full\n") }
else
{
printf("Enter the element to be insert");
scanf("%d",&num);
if(front==-1)
front=rear=0;
else
rear=(rear+1)%MAXSIZE;
queue[rear]=num;
[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 9
}
}

Algorithm for deleting an element in a circular queue using arrays:


QDELETE (queue[MAXSIZE], item)
Step 1: if (FRONT = -1) then
Print(“Queue is Underflow”)
Exit
Else

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

Program for adding an element in a circular queue using arrays:


void cqdelete()
{
int num;
if(front==-1)
{ printf("Queue is Empty\n") }
else
{
num=cq[front];
printf("Deleted element is = %d\n",queue[front]);

if(front==rear)
front=rear=-1;
else
front=(front+1)%MAXSIZE;
}
}

Full program of Circular Queue in C using Array:

[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 10


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 6
int queue[max];
int front=-1;
int rear=-1;
void enqueue(int element)
{
if(front==-1 && rear==-1)
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front)
{
printf("Queue is Full");
}
else
{
rear=(rear+1)%max;
queue[rear]=element;
}
}
void dequeue()
{
if((front==-1) && (rear==-1))
{ printf("\n queue is underflow"); }
else if(front==rear)
{
printf("\n the queued element is; %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\n the queued element is %d", queue[front]);
front=(front+1)%max;
}
[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 11
}
void display()
{
int i=front;
if(front==-1 && rear==-1)
{ printf("\n Queue is empty"); }
else
{
printf("\n Elements in a queue are: ");
while(i!=rear)
{
printf("\n queue[%d]=%d",i,queue[i]);
i=(i+1)%max;
}
printf("\n queue[%d]=%d",i,queue[rear]);
}
}
void main()
{
clrscr();
int choice=1,x;
while(choice<4 && choice!=0)
{
printf("\n Press 1: Insert and Element");
printf("\n Press 2: Delete an Element");
printf("\n Press 3: Display the element");

printf("\n Enter your choice: ");


scanf("%d", &choice);

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

There can be two types of implementations of priority queue:


i) Ascending priority queue ii) Descending priority queue
A collection of items into which items can be inserted arbitrarily and from which only the
smallest item can be removed is called ascending priority queue.
In descending priority queue only the largest item is deleted. The elements of priority queue
need not be numbers of characters that can be composed directly.
An ascending priority queue is a collection of items into which items can be inserted arbitrarily
and from which only the smallest item can be removed. On the other hand a descending
priority queue allows only the largest item to be removed.
Insertion: The insertion in priority queues is the same as in non-priority queues.
Deletion: Deletion requires a search from the element of highest priority and deletes the
element with highest priority. The following methods can be used for deletion/removal from a
given priority queue:
1. An empty indicator replaces deleted elements
2. After each deletion elements can be moved up in the array decrementing the rear
3. The array in the queue can be maintained as an ordered circular array.

Double Ended Queue (DEQUE)


It is also a homogeneous list of elements in which insertion and deletion operations are
performed from both the ends i.e., we can insert elements from the REAR end or from the
FRONT ends. Hence it is called double-ended queue. It is commonly referred to as DEQUE.
There are two types of DEQUEs. These two types are due to the restrictions put to perform
either the insertions or deletions only at one end. They are:
1. Input - restricted queue
2. Output - restricted queue
Since both insertion and deletion are performed from either end, it is necessary to design an
algorithm to perform the following four operations:
1. Insertion of an element at the REAR end of the queue
[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 13
2. Deletion of an element from the FRONT end of the queue
3. Insertion of an element at the FRONT end of the queue
4. Deletion of an element from the REAR end of the queue.

https://www.programiz.com/dsa/deque ===➔ for more details

https://www.javatpoint.com/ds-types-of-queues

Applications of queue in data structure:


• Task Scheduling: Queues can be used to schedule tasks based on priority or the order in
which they were received.
• Resource Allocation: Queues can be used to manage and allocate resources, such as
printers or CPU processing time.
• Batch Processing: Queues can be used to handle batch processing jobs, such as data
analysis or image rendering.
• Message Buffering: Queues can be used to buffer messages in communication systems,
such as message queues in messaging systems or buffers in computer networks.
• Event Handling: Queues can be used to handle events in event-driven systems, such as
GUI applications or simulation systems.
• Traffic Management: Queues can be used to manage traffic flow in transportation
systems, such as airport control systems or road networks.
• Operating systems: Operating systems often use queues to manage processes and
resources. For example, a process scheduler might use a queue to manage the order in
which processes are executed.
• Network protocols: Network protocols like TCP and UDP use queues to manage packets
that are transmitted over the network. Queues can help to ensure that packets are
delivered in the correct order and at the appropriate rate.
• Printer queues: In printing systems, queues are used to manage the order in which print
jobs are processed. Jobs are added to the queue as they are submitted, and the printer
processes them in the order they were received.
• Web servers: Web servers use queues to manage incoming requests from clients.
Requests are added to the queue as they are received, and they are processed by the
server in the order they were received.
• Breadth-first search algorithm: The breadth-first search algorithm uses a queue to
explore nodes in a graph level-by-level. The algorithm starts at a given node, adds its
neighbors to the queue, and then processes each neighbor in turn.

[Data Structure and Algorithm ©Raj Kumar Shrestha] Page 14

You might also like