0% found this document useful (0 votes)
87 views25 pages

Queues: Chapter 6 - Principles of Data Structures Using C by Vinu V Das

The document discusses queues and dequeues. It defines queues as first-in first-out (FIFO) data structures where new elements are added to the rear and existing elements are removed from the front. It describes operations on queues like insertion and deletion. It also discusses circular queues and types of queues. Dequeues are defined as double-ended queues where elements can be added or removed from both ends. The document outlines insertion and deletion algorithms for queues and dequeues.

Uploaded by

Aamir Chohan
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)
87 views25 pages

Queues: Chapter 6 - Principles of Data Structures Using C by Vinu V Das

The document discusses queues and dequeues. It defines queues as first-in first-out (FIFO) data structures where new elements are added to the rear and existing elements are removed from the front. It describes operations on queues like insertion and deletion. It also discusses circular queues and types of queues. Dequeues are defined as double-ended queues where elements can be added or removed from both ends. The document outlines insertion and deletion algorithms for queues and dequeues.

Uploaded by

Aamir Chohan
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/ 25

Queues

Chapter 6 - Principles of Data Structures using C by Vinu V Das


Contents

Queues
Deques

Circular Operations
Queue on
Deques
Operations
on Queue Types of
Queues

Instructor: Aatif Shahbaz 2


Lecture 13
Queues. Operations on Queue. Circular Queue.
Queues
 A queue is logically a first in first out (FIFO or first come first serve) linear data structure.

 The concept of queue can be understood by our real life problems.


• For example a customer come and join in a queue to take the train ticket at the end (rear) and the
ticket is issued from the front end of queue. That is, the customer who arrived first will receive the
ticket first. It means the customers are serviced in the order in which they arrive at the service
centre.

 It is a homogeneous collection of elements in which new elements are added at one end called rear,
and the existing elements are deleted from other end called front.

Operations on Queue :
Insert :
 Adding new element in queue (from rear), incrementing the array index
Delete :
 Removing an existing element from queue (from front), decrementing the array index

Instructor: Aatif Shahbaz 4


Insertion and Deletion
Insert(Q , 10 )

Front = -1 Front = 0
Rear = -1 Rear = 0

Insert(Q , 3) Insert(Q , 41 )

Front = 0 Front = 0
Rear = 1 Rear = 2

Insert(Q , 70 ) Data = Delete(Q )

Front = 0 Front = 1
Rear = 3 Rear = 3

Instructor: Aatif Shahbaz 5


Insertion and Deletion
Insert(Q , 11 ) Data = Delete(Q )

Front = 1 Front = 2
Rear = 4 Rear = 4

 Queue can be implemented in two ways:


• Using arrays (static)
• Using pointers (dynamic)

Underflow and Overflow :


 If we try to delete or remove an element from queue when it is empty, underflow occurs.
• It is not possible to delete (or take out) any element when there is no element in the queue.

 If we try to insert or add an element to queue when it is full, overflow occurs.


• It is naturally not possible to insert any more elements in queue.

Instructor: Aatif Shahbaz 6


Insertion and Deletion
 Insert ( Q , data )  data = Delete ( Q )
• Q = A linear Array • Q = A linear Array
• data = Element needs to be inserted • data = for containing removed element

Algorithm: Algorithm:
 [start]  [start]
1. If rear == size-1 then, 1. If front == –1 OR front > rear
a) Display “Queue overflow” a) Display “The queue is empty”
2. Else 2. Else
a) Rear = rear +1 a) Data = Q[front]
b) Q[rear] = data b) Front = front +1
 [stop] 3. Return data
 [stop]

Instructor: Aatif Shahbaz 7


Types of Queues
Circular Queue
 In circular queues the elements Q[0] , Q[1] , Q[2] .... Q[n – 1] is represented in a circular fashion with
Q[0] following Q[n-1].

 Suppose Q is a queue array of 6 elements

After inserting 18, 7, 42, 67 After deleting 18 and 7

Instructor: Aatif Shahbaz 9


Circular Queue
 After inserting an element at last location Q[5], the next element will be After inserting 30, 47,14
inserted at the very first location (i.e., Q[0])
 At any time the position of the element to be inserted will be
calculated by the relation
• Rear = (Rear + 1) % SIZE
 After deleting an element from circular queue the position of the front
end is calculated by the relation
• Front= (Front + 1) % SIZE

Applications of Queue:
• Round robin techniques for processor scheduling is implemented
using queue.
• Printer server routines (in drivers) are designed using queues.
• All types of customer service software (like Railway/Air ticket
reservation) are designed using queue to give proper service to the
customers.

Instructor: Aatif Shahbaz 10


Insertion
 Insert ( CQ , data)

Algorithm:
 [start]
1. If (FRONT == 0 && rear == SIZE -1) OR FRONT == REAR +1
Display “Queue is full”
Else
If FRONT == – 1 then, [for empty Q]
Set FRONT = 0 and REAR = 0
Else if REAR == SIZE -1 then , [when Q reached its last(largest) index]
Set REAR = 0
Else : [for indices other than last one]
Set REAR = REAR + 1 [end of inner if statement] [end of outer if statement]
2. CQ[REAR] = data
 [stop]
Instructor: Aatif Shahbaz 11
Deletetion
 DATA =Delete ( CQ )

Algorithm:
 [start]
1. If FRONT== – 1
Display “Queue is empty”
Else:
Set DATA = CQ[FRONT]
If (REAR == FRONT) [only one data item in queue]
Set FRONT = –1 and REAR = –1
Else if FRONT == SIZE -1 then, [item stored at last(largest) index]
Set FRONT = 0
Else : [for indices other than last one]
FRONT = FRONT +1 [end of inner if statement] [end of outer if statement]
2. Return DATA and [stop]
Instructor: Aatif Shahbaz 12
Assignment 10
Implement Insertion and Deletion Algorithm for Circular Queue in C
• Use help from Program 4.2 - Principles of Data Structures using C and C++ by Vinu V Das
• Submit printout of source code and output with your Name and SID at the top.
Lecture 14
Dqueus. Operations on Dqueus
DeQues
 A deque is a homogeneous circular list in which elements can be added or inserted and deleted or
removed from both the ends.
 New element can be at rear or front ends and also can be removed from both front and rear ends.
• Hence it is called Double Ended Queue.

 There are two types of deque depending upon the restriction to perform insertion or deletion
operations at the two ends.
• Input restricted deque
• Output restricted deque

Instructor: Aatif Shahbaz 15


DeQues
 An input restricted deque is a deque, which allows insertion at only 1 end, rear end, but allows
deletion at both ends, rear and front end of the lists.

 An output restricted deque is a deque, which allows deletion at only one end, front end, but allows
insertion at both ends, rear and front ends, of the lists.

 The possible operation performed on deque is


1. Add an element at the rear end (RIGHT)
2. Add an element at the front end (LEFT)
3. Delete an element from the front end (LEFT)
4. Delete an element from the rear end (RIGHT)

 Only 1st , 3rd and 4th operations are performed by input-restricted deque.
 Only 1st , 2nd and 3rd operations are performed by output-restricted deque.

Instructor: Aatif Shahbaz 16


Insertion at Right
 Insert ( DQ , data)

Algorithm:
 [start]
1. If (LEFT == 0 && RIGHT == SIZE -1) OR LEFT == RIGHT +1
Display “Queue is full”
Else:
If LEFT == – 1 then, [for empty Q]
Set LEFT = 0 and RIGHT = 0
Else if RIGHT == SIZE -1 then , [when Q reached its right most index]
Set RIGHT = 0
Else : [for indices other than last one]
Set RIGHT = RIGHT + 1 [end of inner if statement] [end of outer if statement]
2. DQ[RIGHT] = data
 [stop]
Instructor: Aatif Shahbaz 17
Insertion at Left
 Insert ( DQ , data)

Algorithm:
 [start]
1. If (LEFT == 0 && RIGHT == SIZE -1) OR LEFT == RIGHT +1
Display “Queue is full”
Else:
If LEFT == – 1 then, [for empty Q]
Set LEFT = 0 and RIGHT = 0
Else if LEFT == 0 then , [when Q reached its left most index]
Set LEFT = SIZE -1
Else : [for indices other than last one]
Set LEFT = LEFT - 1 [end of inner if statement] [end of outer if statement]
2. DQ[LEFT] = data
 [stop]
Instructor: Aatif Shahbaz 18
Deletetion from Left
 DATA = Delete ( DQ )

Algorithm:
 [start]
1. If LEFT == – 1
Display “Queue is empty”
Else:
Set DATA = DQ[LEFT]
If (LEFT == RIGHT) [only one data item in queue]
Set LEFT = –1 and RIGHT = –1
Else if LEFT == SIZE -1 then, [item stored at left most index]
Set LEFT = 0
Else : [for indices other than last one]
LEFT = LEFT +1 [end of inner if statement] [end of outer if statement]
2. Return DATA and [stop]
Instructor: Aatif Shahbaz 19
Deletetion from Right
 DATA = Delete ( DQ )

Algorithm:
 [start]
1. If LEFT == – 1
Display “Queue is empty”
Else:
Set DATA = DQ[RIGHT]
If (LEFT == RIGHT) [only one data item in queue]
Set LEFT = –1 and RIGHT = –1
Else if RIGHT == 0 then, [item stored at left most index]
Set RIGHT = SIZE -1
Else : [for indices other than last one]
RIGHT = RIGHT -1 [end of inner if statement] [end of outer if statement]
2. Return DATA and [stop]
Instructor: Aatif Shahbaz 20
Input Restricted Deque
Initially Delete from Right
M A T G M A T

Insert S Delete from Left


M A T S A T S

Insert O Insert M
A T S O A T S O M

Instructor: Aatif Shahbaz 21


Output Restricted Deque
Initially Delete
S M A T G M A T G

Insert O to Right Delete


O M A T G O A T G

Insert K to Right Insert A to Left


O K A T G O K A A T G

Instructor: Aatif Shahbaz 22


Assignment 11a
Apply following operations on Input Restricted DQ
 Trace LEFT and RIGHT pointers

• Delete from Right


• Insert G


Insert M
Delete from Left
H B A Z S H A
• Insert O
• Delete from Right LEFT= 6 RIGHT = 3
• Insert S
• Delete from Left
• Insert A
• Insert J
• Delete from Left
Assignment 11b
Apply following operations on Output Restricted DQ
 Trace LEFT and RIGHT pointers

• Delete
• Insert G to Left


Insert M to Right
Delete
H B A Z S H A
• Insert O to Left
• Delete LEFT= 6 RIGHT = 3
• Insert S to Right
• Delete
• Insert A to Left
• Insert J to Right
• Delete
Practice Problem
Write a complete program in C for Insertion and Deletion in Queue
• Use help from Program 4.1 - Principles of Data Structures using C and C++ by Vinu V Das

Write a complete program in C for Insertion and Deletion in DeQueue


from Lefr and Right
• Use help from Program 4.3 - Principles of Data Structures using C and C++ by Vinu V Das

You might also like