0% found this document useful (0 votes)
4 views

04.Types of queue

The document provides class notes on types of queues, specifically focusing on Circular Queue, Double-Ended Queue (Deque), and Priority Queue, as part of a Data Structures course. It includes definitions, operations, and algorithms for each type of queue, along with learning outcomes and practice questions for students. The material is prepared by B. Rama, an Assistant Professor in the Department of IT at M.A.M. College of Engineering and Technology.

Uploaded by

Jaya Raja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

04.Types of queue

The document provides class notes on types of queues, specifically focusing on Circular Queue, Double-Ended Queue (Deque), and Priority Queue, as part of a Data Structures course. It includes definitions, operations, and algorithms for each type of queue, along with learning outcomes and practice questions for students. The material is prepared by B. Rama, an Assistant Professor in the Department of IT at M.A.M. College of Engineering and Technology.

Uploaded by

Jaya Raja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

M.A.M.

COLLEGE OF ENGINEERING AND


TECHNOLOGY
Siruganur, Tiruchirappalli – 621105.

TOPIC WISE CLASS NOTES


Unit Number: 2 Topic Number: 4 Lecture Number: 15,16&17/46

Name of the Faculty / Designation : B.Rama / ASP


Course : CS8391-Data Structures
Program : B. Tech – IT
Topic : Types of Queue

Resources required to handle the class:( VAK - Visuals, Audio, Kinesthetic.)

Visual PPT

Audio Recorded video with audio

Kinesthetic Workbook

STEP 1: LEARNING OUTCOMES:


Types of Queue
After the session, the students should be able to:
1. Understand the basic concepts of Queue ADT
2. Identify the strength and weakness of linear data structures to solve a problem
3. Use the appropriate data structure in context of solution for a given problem

STEP 2: ACQUISITION
2.4 CIRCULAR QUEUE
In Circular Queue, the insertion of a new element is performed at the very first location of
the queue if the last location of the queue is full, in which the first element comes just after
the last element.

Prepared By: B.Rama, ASP, Dept. of IT


9
A circular queue is an abstract data type that contains a collection of data which allows
addition of data at the end of the queue and removal of data at the beginning of the queue.
Circular queues have a fixed size. Circular queue follows FIFO principle. Queue items are
added at the rear end and the items are deleted at front end of the circular queue. Here the
Queue space is utilized fully by inserting the element at the Front end if the rear end is
full.

Fig 2.17 Representation of circular queue


2.4.1 Operations on Circular Queue
Fundamental operations performed on the Circular Queue are
 Circular Queue Enqueue
 Circular Queue Dequeue
Formula to be used in Circular Queue
For Enqueue Rear = ( Rear + 1) % ArraySize
For Dequeue Front = ( Front + 1) % ArraySize
Circular Queue Enqueue
Operation
It is same as Linear Queue EnQueue Operation (i.e) Inserting the element at the Rear end.
First check for full Queue. If the circular queue is full, then insertion is not possible.
Otherwise check for the rear end. If the Rear end is full, the elements start getting
inserted from the Front end.
Routine to Enqueue an element in circular queue
void Enqueue ( int X, CircularQueue CQ )
{
if( Front = = ( Rear + 1 ) % ArraySize)
Error( “Queue is full!!Insertion not possible” );
else if( Rear = = -1 )
{
Rear = Rear + 1;
CQ[ Rear ] = X;
Rear = ( Rear + 1 ) % Arraysize; CQ[ Rear ] = X;
}
else
{

Prepared By: B.Rama, ASP, Dept. of IT


9
Front = Front + 1;
Rear = Rear + 1;
CQ[ Rear ] = X;
Rear = ( Rear + 1 ) % Arraysize; CQ[ Rear ] = X;
}
}

Circular Queue DeQueue Operation


It is same as Linear Queue DeQueue operation (i.e) deleting the front element. First
check for Empty Queue. If the Circular Queue is empty, then deletion is not possible. If
the Circular Queue has only one element, then the element is deleted and Front and Rear
pointer is initialized to - 1 to represent Empty Queue. Otherwise, Front element is
deleted and the Front pointer is made to point to next element in the Circular Queue.

Fig 2.18 Insertion in circular queue

Routine to DeQueue An Element In Circular Queue


void DeQueue (CircularQueue CQ)
{
if(Front== - 1)
Empty(“Empty Queue!”);
else if(Front==rear)
{
X=CQ[Front];
Front=-1; Rear=-1;
}
else
{
X=CQ[Front];
Front=(Front+1)%Arraysize;
}
}

DOUBLE-ENDED QUEUE (DEQUE)


In DEQUE, insertion and deletion operations are performed at both ends of the
Queue.
Fig 2.18 Insertion in circular queue
Prepared By: B.Rama, ASP, Dept. of IT
9
Fig 2.19 Representation of DEQUE

2.5.1 Operations on DEQUE


Four cases for inserting and deleting the elements in DEQUE are
1. Insertion At Rear End [ same as Linear Queue ]
2. Insertion At Front End
3. Deletion At Front End [ same as Linear Queue ]
4. Deletion At Rear End
Insertion at the rear end
1. Check for the overflow condition
2. If it is true, display that the queue is full
3. Otherwise, if the rear and front pointers are at the initial values(-1). Increment
both the pointers. Goto step 5
4. Increment the rear pointer
5. Assign the value to Q[rear]

Case 1: Routine to insert an element at Rear end


void Insert_Rear (int X, DEQUE DQ)
{
if( Rear = = Arraysize - 1)
Error(“Full Queue!!!! Insertion not possible”);
else if( Rear = = -1)
{
Front = Front + 1;
Rear = Rear + 1;
DQ[ Rear ] = X;
}
else
{
Rear = Rear + 1;
DQ[ Rear ] = X;
}
}
Insertion at front end
1. Check the front pointer, if it is in the first position(0) then display an error message
Prepared By: B.Rama, ASP, Dept. of IT
9
that the value cannot be inserted at the front end
2. Otherwise, decrement the front pointer
3. Assign the value to Q[front]

Case 2: Routine to insert an element at Front end


void Insert_Front ( int X, DEQUE DQ )
{
if( Front = = 0 )
Error(“Element present in Front!!!!! Insertion not possible”);
else if(Front = = -1)
{
Front = Front + 1;
Rear = Rear + 1;
DQ[Front] = X;
}
else
{
Front = Front - 1;
DQ[Front] = X;
}
}

Case 3: Routine to delete an element from Front end


void Delete_Front(DEQUE DQ)
{
if(Front = = - 1)
Error(“Empty queue!!!! Deletion not possible”);
else if( Front = = Rear )
{
X = DQ[ Front];
Front = - 1;
Rear = - 1;
}
else
{
X = DQ [ Front ];
Front = Front + 1;
}
}
Deletion from front end:
1. Check for the underflow condition. If it is true display that the queue is empty
2. Otherwise, delete the elemenr at the front position, by assigning X as Q[front]
3. If the rear and front pointer points to the same position (ie) only one value is
present, then reinitialize both the pointers

Prepared By: B.Rama, ASP, Dept. of IT


9
4. Otherwise, increment the front pointer

Case 4: Routine to delete an element from Rear end


void Delete_Rear(DEQUE DQ)
{
if( Rear = = - 1)
Error(“Empty queue!!!! Deletion not possible”);
else if( Front = = Rear )
{
X = DQ[ Rear ];
Front = - 1;
Rear = - 1;
}
else
{
X = DQ[ Rear ];
Rear = Rear - 1;
}
}

Deletion from rear end


1. Check the rear pointer. If it is in the initial value then display that the value cannot be
deleted
2. Otherwise, delete element at the rear position
3. If the rear and front pointers are at the same position, reinitialize both the pointers
4. Otherwise, decrement the rear pointer

2.6 PRIORITY QUEUE


A priority queue is an abstract data type similar to regular queue or stack data
structure in which each element additionally has a "priority" associated with it. In a
priority queue, an element with high priority is served before an element with low
priority. In some implementations, if two elements have the same priority, they are served
according to the order in which they were enqueued, while in other implementations,
ordering of elements with the same priority is undefined.

Operations on Priority Queue :


 push(): This function is used to insert a new data into the queue.
 pop(): This function removes the element with the highest priority form the queue.
 peek() / top(): This function is used to get the highest priority element in the queue
without removing it from the queue.
Function to insert value into priority queue */
void insert_by_priority(int data)
{
if (rear >= MAX - 1)

Prepared By: B.Rama, ASP, Dept. of IT


9
{
printf("\nQueue overflow no more elements can be inserted");
return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pri_que[rear] = data;
return;
}
else
check(data);
rear++;
}
void delete_by_priority(int data)
{
int i;

if ((front==-1) && (rear==-1))


{
printf("\nQueue is empty no elements to delete");
return;
}
for (i = 0; i <= rear; i++)
{
if (data == pri_que[i])
{
for (; i < rear; i++)
{
pri_que[i] = pri_que[i + 1];
}

pri_que[i] = -99;
rear--;

if (rear == -1)
front = -1;
return;
}
}
printf("\n%d not found in queue to delete", data);
}

Prepared By: B.Rama, ASP, Dept. of IT


9
STEP 3: PRACTICE/TESTING
3. a. Short answer questions to check learning in the class
1. Circular Queue is also known as ________
2. Which of the following is not the type of queue?
a) Ordinary queue
b) Single ended queue
c) Circular queue
d) Priority queue
3. Which of the following is not an advantage of priority queue?
a) Easy to implement
b) Processes with different priority can be efficiently handled
c) Applications with differing requirements
d) Easy to delete elements in any case
4. A data structure in which elements can be inserted or deleted at/from both the ends
but not in the middle is?
a) Queue
b) Circular queue
c) Dequeue
d) Priority queue
5. What is a dequeue?
a) A queue with insert/delete defined for both front and rear ends of the queue
b) A queue implemented with a doubly linked list
c) A queue implemented with both singly and doubly linked lists
d) A queue with insert/delete defined for front side of the queue

3. b. Questions to use in tests and examinations with CO and K level:


S.No Objective type Questions RBT Course
Level Outcomes
1 Circular queue is better than standard linear queue, Why? K4 CO303.6
2 Differentiate between double ended queue and circular
K2 CO303.5
queue.
3 Develop an algorithm for deleting an element in a double
K3 CO303.2
ended queue.
4 Classify the different types of queues. K2 CO303.1
5 What is priority queue? What are the ways to implement
K2 CO303.2
priority queue?
Descriptive type questions
1 A circular queue has a size of 5 and has 3 elements
10,20 and 40 where F=2 and R=4.After inserting 50 and
60,what is the value of F and R.Trying to insert 30 at
K4 CO303.4
this stage what happens? Delete 2 elements from the
queue and insert 70, 80 & 90.Assess the sequence of
steps with necessary diagrams with the value of F & R.
Prepared By: B.Rama, ASP, Dept. of IT
9
2 Analyze the implementation of priority queue. K4 CO303.2
3 Describe about implementation of dequeue ADT using
array. Give relevant examples and diagrammatic K3 CO303.3
representations.

Prepared By: B.Rama, ASP, Dept. of IT


9

You might also like