Queues: Chapter 6 - Principles of Data Structures Using C by Vinu V Das
Queues: Chapter 6 - Principles of Data Structures Using C by Vinu V Das
Queues
Deques
Circular Operations
Queue on
Deques
Operations
on Queue Types of
Queues
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
Front = -1 Front = 0
Rear = -1 Rear = 0
Insert(Q , 3) Insert(Q , 41 )
Front = 0 Front = 0
Rear = 1 Rear = 2
Front = 0 Front = 1
Rear = 3 Rear = 3
Front = 1 Front = 2
Rear = 4 Rear = 4
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]
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.
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
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.
Only 1st , 3rd and 4th operations are performed by input-restricted deque.
Only 1st , 2nd and 3rd operations are performed by output-restricted deque.
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 O Insert M
A T S O A T S O M
• 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