0% found this document useful (0 votes)
15 views35 pages

Module3 DSA

The document discusses the queue data structure. It defines a queue as a collection where elements are added to the rear and removed from the front, following a FIFO (first in, first out) approach. It describes conceptual views of enqueue and dequeue operations on a queue. It also discusses different types of queues like simple/linear, circular, and priority queues. Key queue operations like enqueue, dequeue, first, isFull, isEmpty and size are explained. Implementation of queues as arrays and circular arrays are covered.

Uploaded by

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

Module3 DSA

The document discusses the queue data structure. It defines a queue as a collection where elements are added to the rear and removed from the front, following a FIFO (first in, first out) approach. It describes conceptual views of enqueue and dequeue operations on a queue. It also discusses different types of queues like simple/linear, circular, and priority queues. Key queue operations like enqueue, dequeue, first, isFull, isEmpty and size are explained. Implementation of queues as arrays and circular arrays are covered.

Uploaded by

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

The Queue ADT

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

New element is added


to the rear of the queue

6-4
Conceptual View of a Queue
Removing an element

New front element of queue

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

enqueue : add an element to the tail(rear)of a queue


dequeue : remove an element from the head(front) of a queue
first : examine the element at the front of the queue (“peek”)

Operation Description

dequeue Removes an element from the front of the queue

enqueue Adds an element to the rear of the queue

first Examines the element at the front of the queue


IsFull Determines whether the queue is full
isEmpty Determines whether the queue is empty

size Determines the number of elements in the queue


6-8
Enqueue operation

• 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

• Circular array is an array that


conceptually loops around on
itself
• The last index is thought to
“precede” index 0
• In an array whose last index is n,
the location “before” index 0 is
index n; the location “after” index n
is index 0
• Need to keep track of where the
front as well as the rear of the
queue are at any given time

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==-1)


• Step 1: if(front ==-1)
• If it is EMPTY, then display "Queue is EMPTY!!!" and
terminate the function. write queue is empty
• Step 2 - If it is NOT EMPTY, then define an integer else
variable 'i' and set 'i = front'. • Step 2 - int i = front;
• Step 3 - Check whether 'front <= rear', if it is TRUE, it if(front <= rear)
means rear is right or equal to the front ,then display • Step 3- while(i <= rear)
'queue[i]' value and increment 'i' value by one (i++). print cQueue[i]);
Repeat the same until 'i <= rear' becomes FALSE. i=i+1
• Step 4 - If 'front <= rear' is FALSE, then display else
'queue[i]' value and increment 'i' value by one (i++). • Step 4- while(i <= MAXSIZE - 1)
Repeat the same until'i <= SIZE - 1' becomes FALSE.
print cQueue[i]);
• Step 6 - Set i to 0. i=i+1
• Step 7 - Again display 'cQueue[i]' value and • Step 5- i = 0;
increment i value by one (i++). Repeat the same until • Step 6- while(i <= rear)
'i <= rear' becomes FALSE.
print cQueue[i]);
i=i+1
Implementation of Circular 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 user defined functions used in circular queue
implementation.
• Step 3 - Create a one dimensional array with above defined SIZE (int
cQueue[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
circular queue.
Deque (Double ended queue)
• Generalized form of queue data structure which allows insertion and
removal of elements from both the ends, i.e , front and back.
• Inherits the properties of both queues and stacks.
Types of Deque in Data Structure

Input-Restricted Deque: Output-Restricted Deque:


• performs deletion at both ends • performs insertion at both ends
• performs the insertion at only one • performs the deletion of elements at
end. only one end.
Operations on Deque

Four basic operations are performed on deque, they are as follows:


• Insertion at Rear (InsertRear())
• Insertion at Front (InsertFront())
• Deletion at Front (DeleteFront())
• Deletion at Rear (DeleteRear())
• Along with these primary operations, isEmpty(), isFull() and Peek() operations can
also be performed
Insert from Rear end
• Void InsertRear(int num)
• Step 1 - Check whether queue is FULL. ((rear • Step 1:if (front==0 && rear==max-1) or
== MAX-1 && front == 0) || (front == rear+1)) (front=rear+1)then;
• If it is FULL, then display "Queue is FULL!!! write overflow
Insertion is not possible!!!" and terminate the • Step 2:
function. elseif front==-1 && rear==-1,then;
• Step 2:Else if inserted element is first • set front=front+1 and rear=rear+1
element of the queue then set front=front+1
and rear=rear+1 • Step 3: elseif rear==max-1
set rear=0
• Step 3 - If rear is at max value (rear == SIZE -
1), then implement queue in circular fashion else
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] = num • Step 6: Exit
• Step 6: Exit
Insert from Front end
• Void InsertFront(int num)
• Step 1 - Check whether queue is FULL. ((rear • Step 1:if (front==0 && rear==max-1) ||
== SIZE-1 && front == 0) || (front == rear+1)) (front=rear+1)then;
• If it is FULL, then display "Queue is FULL!!! write overflow
Insertion is not possible!!!" and terminate the • Step 2:
function. elseif front==-1 && rear==-1,then;
• Step 2:Else if inserted element is first • set front=front+1 and rear=rear+1
element of the queue then set front=front+1 • Step 3: elseif front==0
and rear=rear+1
set front=max-1
• Step 3 - If space is there in the array towards
RHS , then implement queue in circular else
fashion set front = max-1 • Step 4: set front=front-1
• Step 4 - else, Decrement front value by one end of if
(front--) • Step 5: set queue[front]=num
• Step 5 - set queue[front] = num • Step 6: Exit
• Step 6: Exit
Classwork
Consider array of 5 elements and perform the following operations:
• InsertFront(10);
• InsertFront(20);
• InsertRear(30);
• InsertRear(40);
• InsertFront(50);
• InsertFront(60);
Delete from Rear 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(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

• Step 1 - Check whether queue is EMPTY. (front==-1)


• Step 1: if(front ==-1)
• If it is EMPTY, then display "Queue is EMPTY!!!" and
terminate the function. write queue is empty
• Step 2 - If it is NOT EMPTY, then define an integer else
variable 'i' and set 'i = front'. • Step 2 - int i = front;
• Step 3 - Check whether 'front <= rear', if it is TRUE, it if(front <= rear)
means rear is right or equal to the front ,then display • Step 3- while(i <= rear)
'queue[i]' value and increment 'i' value by one (i++). print cQueue[i]);
Repeat the same until 'i <= rear' becomes FALSE. i=i+1
• Step 4 - If 'front <= rear' is FALSE, then display else
'queue[i]' value and increment 'i' value by one (i++). • Step 4- while(i <= MAXSIZE - 1)
Repeat the same until'i <= SIZE - 1' becomes FALSE.
print cQueue[i]);
• Step 6 - Set i to 0. i=i+1
• Step 7 - Again display 'cQueue[i]' value and • Step 5- i = 0;
increment i value by one (i++). Repeat the same until • Step 6- while(i <= rear)
'i <= rear' becomes FALSE.
print cQueue[i]);
i=i+1
Priority queue
• Special type of queue in which each element is associated with a priority value.
• Elements are served on the basis of their priority. That is, higher priority elements
are served first.
• Same priority elements are served according to their order in the queue.
• A priority queue is an extension of a queue that contains the following
characteristics:
• Every element in a priority queue has some priority associated with it.
• An element with the higher priority will be deleted before the deletion of the
lesser priority.
• If two elements in a priority queue have the same priority, they will be
arranged using the FIFO principle/they will be arranged according to their
values.
Difference between Priority Queue and Normal Queue

• In a queue, the first-in-first-out rule is implemented whereas, in a


priority queue, the values are removed on the basis of priority. The
element with the highest priority is removed first.
• When an element is popped out of the priority queue, the result will
be in the sorted order of priority, it can be either increasing or
decreasing. While in queue elements are popped out in the order of
FIFO (First in First out).
Implementation of priority queue

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

• Step 1 - Check whether queue is Empty i.e. (rear == -1)


If it is empty, then display "Queue is empty!!! search is not
possible!!!" and terminate the function.
• Step 2 - If queue is not empty then to search the highest priority element,
use a for loop to traverse priority array which starts from index 0 to rear
end and return the index value(suppose idx) of highest priority element.
• Step 3 - Exit
Priority queue implementation using array

• 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.

You might also like