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

Unit3 Queue 2024

This document provides an overview of queues, a linear data structure that operates on a first-in first-out (FIFO) basis. It discusses various implementations including arrays and linked lists, as well as different types of queues such as circular queues, deques, and priority queues. Additionally, it outlines the algorithms for enqueueing and dequeueing operations, their time complexities, and applications in various fields.
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)
2 views35 pages

Unit3 Queue 2024

This document provides an overview of queues, a linear data structure that operates on a first-in first-out (FIFO) basis. It discusses various implementations including arrays and linked lists, as well as different types of queues such as circular queues, deques, and priority queues. Additionally, it outlines the algorithms for enqueueing and dequeueing operations, their time complexities, and applications in various fields.
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/ 35

Unit 3 - Queues

March 3, 2025

Data Structures & Algorithms March 3, 2025 1 / 31


Queues

• Queue is an ordered collection of homogeneous data elements


• Items are inserted at one end (the rear) and deleted at the other end
(the front)
• A linear data structure
• First-in first-out (FIFO) list

• Insertion: ENQUEUE
• Deletion: DEQUEUE

Data Structures & Algorithms March 3, 2025 2 / 31


Queues..

• Elements in Queue are called ITEM


• Number of elements in a queue is length

Representation of Queues
• Using an array
• Using single linked list

Data Structures & Algorithms March 3, 2025 3 / 31


Representation using array

• 1D array Q[1, ..., N ]


• Two pointers FRONT and REAR to indicate two ends of the queue
• ENQUEUE: FRONT
• DEQUEUE: REAR

Data Structures & Algorithms March 3, 2025 4 / 31


Queues..

Three states of queue


• Queue is empty
• FRONT = 0
• REAR = 0
• Queue is full

• FRONT = 0
• REAR = N − 1
• Queue contains elements ≥ 1
• FRONT ≤ REAR
• Number of element = REAR − F RON T + 1

Data Structures & Algorithms March 3, 2025 5 / 31


ENQUEUE using Array

Algorithm ENQUEUE(ITEM)
Input: An element ITEM that has to be inserted
Output: The ITEM is at the REAR of the queue
1 If (REAR=N-1) then
1 Print “Queue is full”
2 Exit
2 Else
1 If( REAR=-1) and (FRONT=-1) //Queue is empty
1 FRONT = 0
2 EndIf
3 REAR=REAR+1
4 Q[REAR]=ITEM

3 EndIf
4 Stop

Data Structures & Algorithms March 3, 2025 6 / 31


DEQUEUE using Array

1 If (Front=-1) then
1 Print “ Queue is empty”
2 Exit
2 Else
1 ITEM=Q[FRONT] //Get the element
2 IF (FRONT=REAR) // When Queue contains single element
1 REAR=-1 // The queue becomes empty
2 FRONT = -1
3 Else
1 1. FRONT=FRONT+1
4 EndIf
3 EndIf
4 Stop

Data Structures & Algorithms March 3, 2025 7 / 31


Limitations of array implementation

• Problem of fixed size


• Memory wastage while resolving fixed size problem

Data Structures & Algorithms March 3, 2025 8 / 31


Limitations of array implementation

• Problem of fixed size


• Memory wastage while resolving fixed size problem

• Memory wastage due to deletion


of data-elements

Data Structures & Algorithms March 3, 2025 8 / 31


Representation using Linked List

• In a linked queue, each node consists of two fields, data field and
reference field
• Containing two pointers
1 Front
2 Rear
• Front: point to the head of the node
• Rear: point to the end of the list (last node).
• Enque operates upon the rear pointer
• Deque operates upon the front pointer

Data Structures & Algorithms March 3, 2025 9 / 31


Linked List Representation

Data Structures & Algorithms March 3, 2025 10 / 31


Linked List Representation

Data Structures & Algorithms March 3, 2025 10 / 31


Various Queue Structures

1 Circular Queue
2 Priority Queue
3 Dequeue

Data Structures & Algorithms March 3, 2025 11 / 31


Circular Queue

• A circular queue is the extended version of a regular queue, which


solve issue of non-usable empty space of regular queue (when
implementing using array).
• In a circular queue, the last node is connected to the first node
• We can insert an item at the first node of the queue when the last
node is full and the first node is free.
• It’s also called a ring buffer

Data Structures & Algorithms March 3, 2025 12 / 31


Circular Queue..

Data Structures & Algorithms March 3, 2025 13 / 31


• Each pop operation causes Front to move right by 1
• When Rear = n − 1 and Front > 0
• Number of elements is less than arraylength and there is space for
elements in the left end of array
• How to use this space?

Data Structures & Algorithms March 3, 2025 14 / 31


• Each pop operation causes Front to move right by 1
• When Rear = n − 1 and Front > 0
• Number of elements is less than arraylength and there is space for
elements in the left end of array
• How to use this space?
• Left shift all elements
• But increases the worse case time of push operation O(1) to O(n)

Data Structures & Algorithms March 3, 2025 14 / 31


Circular Queue..

• The worse case time of push and pop operation become O(1) if array
positions are arranged in circular way
• Mapping function:
location(i) = (location(front element) + i)% arraylength

Data Structures & Algorithms March 3, 2025 15 / 31


• Consider an array of length 5, used to implement a circular queue

• location(front) = 3
• The rear of the queue is at index 1

Data Structures & Algorithms March 3, 2025 16 / 31


Circular Queue..

The position for 2nd element (i = 1)


location(i) = (location(front) + i) % arrayLength
= (3 + 1) % 5
=4%5
=4

Data Structures & Algorithms March 3, 2025 17 / 31


Circular Queue..

cases of queue
• Circular Queue is empty
• FRONT = −1 (if index from 0)
• REAR = −1
• Queue is full
• (REAR + 1)% arrayLength = = FRONT

Data Structures & Algorithms March 3, 2025 18 / 31


Algorithm Enqueue(CQueue, ITEM)

Input: An element to be inserted in the circular queue


Output: The queue with ITEM added at the rear, if the queue is not full
1 If(front = -1), then
1 FRONT =0, REAR =0 //Queue is empty
2 CQ[REAR] = ITEM
2 Else
1 next = (REAR + 1)% arrayLength
2 If next 6= FRONT //If Queue is not full
1 REAR = next
2 CQ[REAR] = ITEM
3 Else
1 Print ”Queue is full”
2 EndIf
4 EndIf

Data Structures & Algorithms March 3, 2025 19 / 31


Algorithm DEqueue(CQueue, ITEM)

1 if (front == -1)
1 print ”Queue is empty”
2 Exit
2 Else
1 ITEM = CQueue[front] // Retrieve the element at the front
2 if (front == rear) // If the queue has only one element
• front = -1
• rear = -1
3 front = (front + 1) % arrayLength // Move front one position ahead,
circularly
4 EndIf

3 EndIf
4 Return ITEM

Data Structures & Algorithms March 3, 2025 20 / 31


Algorithm DEqueue(CQueue, ITEM)

1 if (front == -1)
1 print ”Queue is empty”
2 Exit
2 Else
1 ITEM = CQueue[front] // Retrieve the element at the front
2 if (front == rear) // If the queue has only one element
• front = -1
• rear = -1
3 front = (front + 1) % arrayLength // Move front one position ahead,
circularly
4 EndIf

3 EndIf
4 Return ITEM
Enqueue and dequeue operations in a circular queue have a time
complexity of O(1)

Data Structures & Algorithms March 3, 2025 20 / 31


Applications

• Resource Pool Management


• CPU Scheduling
• Buffering Data Streams
• Networking
• Audio/Video Playback
• Real-time Data Processing

Data Structures & Algorithms March 3, 2025 21 / 31


Deque (Double Ended Queue)

• Insertion and deletion can be made at either end of the queue


• Flexible: Inherits the properties of both queues and stacks
• No Fixed Direction: Elements can be enqueued or dequeued from
either end, providing more control over data flow.

Data Structures & Algorithms March 3, 2025 22 / 31


Operations on Deques

• InsertFront: Add an element at the front of the deque


• InsertLast: Add an element at the rear of the deque
• DeleteFront: Remove and return the front element of the deque
• DeleteLast: Remove and return the rear element of the deque

Data Structures & Algorithms March 3, 2025 23 / 31


Algorithm: Insert at Front

1 If the Deque is Full


1 (rear + 1) %N == front
2 Exit
2 Else
1 If the Deque is empty (front = -1)
1 front = 0, rear =0
2 DQ[front] = ITEM
2 If (front = 0) //If front is at the beginning of the array
1 front = N - 1
2 DQ[front] = ITEM
3 Else //If the deque already has elements and front is not at the
beginning
1 front = front-1
2 DQ[front] = ITEM

3 Exit

Data Structures & Algorithms March 3, 2025 24 / 31


Algorithm: DeleteLast
1 If (front = -1)
1 Print deque is empty”
2 Exit
2 Else
1 If (front =rear) // single element
1 ITEM = DQ(rear)
2 front = -1, rear = -1
2 else if (rear = 0) //Rear at the Beginning of the Array
1 ITEM = DQ(rear)
2 rear = N-1
3 Else
1 ITEM = DQ(rear) //If the deque has more than one element and rear
is not at the beginning
2 rear = rear -1
4 Endif
3 Endif
4 return ITEM
5 Exit
Data Structures & Algorithms March 3, 2025 25 / 31
InsertLast and DeleteF ront are same as in circular Queue

Time complexity: O(1)

Data Structures & Algorithms March 3, 2025 26 / 31


Priority Queue

• Each element is assigned with a Priority


• Element can be inserted or deleted at any position on the queue
• an element with high priority is served before an element with low
priority

Data Structures & Algorithms March 3, 2025 27 / 31


Characteristics of Priority Queues

• Priority: Elements are dequeued based on their priority rather than


their position in the queue
• Ordering: Elements with higher priority are dequeued before those
with lower priority
• Dynamic: Elements can be added at any time, and the priority queue
will reorganize itself to maintain the priority order

Data Structures & Algorithms March 3, 2025 28 / 31


Examples

• Traffic Management
• Operating System Processes
• Data compression
• Dijkstra’s Algorithm

Data Structures & Algorithms March 3, 2025 29 / 31


Stack Application: Recursion

Computing the factorial of a number


int factorial(int n) {
if (n ≤ 1) return 1; // Base case
return n ∗ f actorial(n − 1); // Recursive case
}

Data Structures & Algorithms March 3, 2025 30 / 31


Algorithm: Factorial Using a Stack

1: Populate the Stack


For i = n to 2
If S.top < SIZE - 1
S.top = S.top + 1
S.items[S.top] = i
Else
Print ”Stack Overflow”
Return -1
2: Initialize the result
3: Process the stack to calculate factorial
While S.top 6= -1
Integer element = S.items [S.top]
S.top = S.top - 1
result = result ∗ element
4: Return the result

Data Structures & Algorithms March 3, 2025 31 / 31

You might also like