Module3 DSA
Module3 DSA
17/12/2023 2
Queues
• Queue: a collection whose elements are added at one
end (the rear or tail of the queue) and removed from
the other end (the front or head of the queue)
• A queue is a FIFO (first in, first out) data structure
• Any waiting line is a queue:
• The check-out line at a grocery store
• The cars at a stop light
• An assembly line
6-3
Conceptual View of a Queue
Adding an element
Front of queue
6-4
Conceptual View of a Queue
Removing an element
Element is removed
from the front of the
queue
6-5
Uses of Queues in Computing
• For any kind of problem involving FIFO data
• Printer queue
• Keyboard input buffer
• In simulation studies, where the goal is to reduce waiting time:
• Handling website traffic
• Maintaining the playlist in media players
• Optimize the flow of traffic at a traffic light
• Determine number of cashiers to have on duty at a grocery
store at different times of day
6-6
Types of Queues
• Simple/Linear Queue:
• Most basic version of a queue
• Enqueue operation takes place at the rear end and dequeue operation takes place at the
front end
• Circular Queue:
• The element of the queue act as a circular ring
• Last element is connected to the first element
• Its advantage is that the memory is utilized in a better way
• Priority Queue:
• Special type of queue which arranges the elements in a queue based on some priority.
• The priority can be something where the element with the highest value has the priority so it
creates a queue with decreasing order of values.
• The priority can also be such that the element with the lowest value gets the highest priority
so in turn it creates a queue with increasing order of values
Queue Operations
Operation Description
• Step 1 - Check
whether queue is FULL. Void enqueue (int value)
i.e. (rear == MAX-1) Step 1:if rear==max-1 then;
If it is FULL, then display "Queue is write overflow
FULL!!! Insertion is not return
possible!!!" and terminate the end of if
function. Step 2: If front==-1 && rear==-1,then
• Step 2:If it is first element of queue, • set front=front+1 and rear=rear+1
then set front=front+1 and end of if
rear=rear+1
Step 3: else
• Step 3 - If it is NOT first element, then
set rear=rear+1
increment rear value by one (rear++)
end of else
• Step 4 -set queue[rear] = value
Step 4:set queue(rear) = value
• Step 5: Exit Step 5: Exit
Dequeue operation
• Step 1 - Check whether queue is EMPTY. Dequeue()
(front==-1) Step 1:If front==-1
If it is EMPTY, then display "Queue is
write underflow and return
EMPTY!!! Deletion is not possible!!!" and
terminate the function. end of if
• Step 2 - If it is NOT EMPTY, then print the front Step 2: else
value i.e. queue[front] to be deleted
set val = queue(front)
• Step 3-Check the value of front pointer. If it is
equal to rear it means it is the last element of print(“The deleted value is %d”,val)
the queue. The queue will become empty after Step 3: if(front==rear)
deleting this element. In this case set front and
rear both pointers to -1. set front==rear==-1
• Step 4: Else increment the front value by one Step 4: else set front=front+1
(front ++). end of loop
• Step 5:Exit Step 5: Exit
Displaying a queue
• Step 1 - Check whether queue is EMPTY. • Step 1:If front == -1,
(front == -1)
write empty queue
If it is EMPTY, then display "Queue is
• Step 2: else
EMPTY!!!" and terminate the function.
int i;
• Step 2 - If it is NOT EMPTY, then traverse the
queue from front position to rear position. for(i=front; i<=rear; i++)
Define an integer variable 'i' and set 'i = front'. printf("%d",queue[i]);
Display 'queue[i]' value and increment 'i' • Step 3:Exit
value by one (i++). Repeat the same until
'i' value reaches to rear (i <= rear)
• Step 3: Exit
Implementation of queue as array
• 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 - Implement main method by displaying menu of operations list and
make suitable function calls to perform operation selected by the user on
queue.
Second Approach: Queue as a Circular Array
6-13
Enqueue operation
• Step 1 - Check whether queue is FULL. • Step 1:if (front==0 and rear==MAX-1) or
((rear == MAX-1 && front == 0) || (front (front=rear+1)then;
== rear+1))
write overflow and return
• If it is FULL, then display "Queue is FULL!!!
Insertion is not possible!!!" and terminate the • Step 2:
function. elseif front==-1 & rear==-1,then;
• Step 2:If the inserted element is first front=front+1 and rear=rear+1
element of the queue then set Step 3: elseif rear==max-1
front=front+1 and rear=rear+1
set rear=0
• Step 3 - If rear is at max index value of
array (rear == SIZE - 1), then implement else
queue in circular fashion set rear = 0. • Step 4: set rear=rear+1
• Step 4 - else, Increment rear value by one end of if
(rear++) • Step 5: set queue(rear)=num
• Step 5 - set queue[rear] = value • Step 6: Exit
• Step 6: Exit
Dequeue operation
• Step 1 - Check whether queue is EMPTY. (front == -
1)
• Step 1:If If front==-1
• If it is EMPTY, then display "Queue is EMPTY!!!
write underflow and return
Deletion is not possible!!!" and terminate the
function. • Step2: else
• Step 2 - If it is NOT EMPTY, then set val = queue(front)
display queue[front] as deleted element print deleted value is val
• Step 3-If front is equal to rear it means that it is • Step 3- if (front == rear)
the only element of the queue. The queue will
become empty after deleting this element. In this set front =rear= -1;
case set front and rear both pointers to -1. • Step 4- elseif (front == MAX-1)
• Step 4-If front is pointing to the last index value set front = 0;
then implement queue in the circular fashion. Set
front to point first index value of the array. • Step 5- else front = front+1;
• Step 5- else increase the value of front pointer by 1 • Step 6-Exit
• Step 6-Exit
Display operation
• Step 1 - Check whether queue is EMPTY. (front • Step 1:If If front==-1 && rear==-1
==rear== -1 ) write underflow
• If it is EMPTY, then display "Queue is EMPTY!!! • Step2: else
Deletion is not possible!!!" and terminate the
function. set val = queue(rear)
• Step 2 - If it is NOT EMPTY, then write deleted value is val
display queue[rear] as deleted element • Step 3- if (front == rear)
• Step 3-If front is equal to rear it means deleted set front =rear= -1;
element is the last element of the queue. The
queue will become empty after deleting this • Step 4- elseif (rear==0)
element. In this case set front and rear both set rear = max-1;
pointers to -1.
• Step 5- else rear = rear-1;
• Step 4-If rear is pointing to the first index value
• Step 6-Exit
then set it to the max index value(circular array)
• Step 5- else decrease the value of rear pointer by 1
• Step 6-Exit
Delete from Front end
• Step 1 - Check whether queue is EMPTY. (front == • Step 1:If If front==-1 && rear==-1
rear==-1 ) write underflow
• If it is EMPTY, then display "Queue is EMPTY!!! • Step2: else
Deletion is not possible!!!" and terminate the
function. set val = queue(front)
• Step 2 - If it is NOT EMPTY, then write deleted value is val
display queue[front] as deleted element • Step 3- if (front == rear)
• Step 3-If front is equal to rear it means deleted set front =rear= -1;
element is the last element of the queue. The
queue will become empty after deleting this • Step 4- elseif (front == MAX-1)
element. In this case set front and rear both set front = 0;
pointers to -1.
• Step 5- else front = front+1;
• Step 4-If front is pointing to the last index value
• Step 6-Exit
then set it to the first index value(circular array)
• Step 5- else increase the value of front pointer by 1
• Step 6-Exit
Classwork
Consider array of 5 elements and
perform the following operations:
• InsertFront(10); • DeleteFront();
• InsertFront(20); • DeleteFront();
• InsertRear(30); • DeleteRear();
• InsertRear(40); • DeleteFront();
• InsertFront(50); • DeleteRear();
• InsertFront(60);
Display operation
• Priority queue can be implemented using an array, a linked list, a heap data structure, or a binary
search tree. Among these data structures, heap data structure provides an efficient
implementation of priority queues.
• Array Approach: The idea is to create a structure to store the value and priority of the element
and then create an array of that structure to store elements or create two arrays one for inserting
values and another for inserting priority. Below are the functionalities that are to be
implemented:
• enqueue(): It is used to insert the element at the end of the queue.
• peek():
• Traverse across the priority queue and find the element with the highest priority and return its index.
• In the case of multiple elements with the same priority, find the element with the highest.
• dequeue():
• Find the index with the highest priority using the peek() function let’s call that position
as ind, and then shift the position of all the elements after the position ind one position to
the left.
Priority queue implementation using array
• Create two arrays one for inserting values(array queue) and another
for inserting priority(array priority).
Priority queue implementation using array
• Enqueue operation(Insert value and priority in queue via rear end)
• Step 1 - Check whether queue is FULL.
i.e. (rear == MAX-1)
If it is FULL, then display "Queue is FULL!!! Insertion is not
possible!!!" and terminate the function.
• Step 2:If queue is not full then increment rear value by one (rear++)
• Step 4 -set queue[rear] = value, priority[rear]=pri
• Step 5: Exit
Priority queue implementation using array
• Peek operation(Traverse across the priority queue and find the element
with the highest priority and return its index)
• Dequeue operation
• Step 1 - Check whether queue is Empty i.e. (rear == -1)
If it is empty, then display "Queue is empty!!! deletion is not
possible!!!" and terminate the function.
• Step 2:Get the index value(suppose idx) returned by peek function.
• Step 3: If queue is not empty then use a for loop which starts from idx
and goes till rear end. Print the element which is present at idx index
and then shift all the elements one position left from index where the
highest priority item was found.
• Step 4: Exit
Application of queue data structure
• Queues are widely used as waiting lists for a single shared resource
like printer, disk, CPU.
• Queues are used as buffers in most of the applications like MP3 media
player, CD player, etc.
• Queue are used to maintain the play list in media players in order to
add and remove the songs from the play-list.
• Queues are used in operating systems for handling interrupts.
• It is used in traffic management to manage traffic lights.
• Whatsapp uses a queue to queue up the messages in the WhatsApp
server if the recipient’s internet connection is turned off.