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

Circular Queue: Dept. of Computer Science Faculty of Science and Technology

The document discusses circular queues, which are more memory efficient than linear queues. Unlike linear queues, circular queues can reuse unused memory by circling back to the beginning of the queue. This allows elements to be added even when the queue appears full. The document provides examples of adding and removing elements from a circular queue to demonstrate how it works. It also outlines simulating operations like enqueue, dequeue, checking if empty or full on a circular queue.

Uploaded by

Rehaan Razib
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Circular Queue: Dept. of Computer Science Faculty of Science and Technology

The document discusses circular queues, which are more memory efficient than linear queues. Unlike linear queues, circular queues can reuse unused memory by circling back to the beginning of the queue. This allows elements to be added even when the queue appears full. The document provides examples of adding and removing elements from a circular queue to demonstrate how it works. It also outlines simulating operations like enqueue, dequeue, checking if empty or full on a circular queue.

Uploaded by

Rehaan Razib
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Circular Queue

Course Code: CSC 2106 Course Title: Data Structure (Theory)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 4.1 Week No: 4 Semester: Fall 20_21


Lecturer: Kaniz Fatema, Assistant Professor, [email protected]
Lecture Outline

1. Queue
i. Definition
ii. Simulation of Operations
iii. Pseudo code of Circular Queue Implementation
2. Books
3. References
Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.


Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.

maxSize = 4 front

Can we enqueue another element in this?


0 1 2 3 4
3 6 2 5
NO

rear Recall, the queue is full if(rear==(maxSize-1))


fig 1: A Linear Queue
Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.


Assume,This
Queue starts
here

fig 2.1: A Circular Queue


Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.

fig 2.1: A Circular Queue


Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.

7 9

fig 2.1: A Circular Queue


Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.

7 9

fig 2.1: A Circular Queue


Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.

If we Dequeue, which item


7 9 will be removed from the
queue?

4 8

fig 2.1: A Circular Queue


Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.

Then?
7 9

4 8

fig 2.1: A Circular Queue


Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.

Then?
7 9

4 8

fig 2.1: A Circular Queue


Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.

Can you guess the position of


7 9 front and rear?

4 8

fig 2.1: A Circular Queue


Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.

7 9
front

4 8
rear
fig 2.1: A Circular Queue
Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.


front

9 Can we enqueue
7
front an element 2 now? 0 1 2 3 4
Linear Representation 7 9 8 4
4 8
rear rear
fig 2.1: A Circular Queue
Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.


front

rear 9 Enqueue Possible.


2
Reason: Circular 0 1 2 3 4
Linear Representation 2 9 8 4
front 4 8
rear
fig 2.1: A Circular Queue
Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.


front

rear 2 9
Enqueue again?
0 1 2 3 4
Linear Representation 2 9 8 4
front 4 8
rear
fig 2.1: A Circular Queue
Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.


rear front

2 1
Enqueue again?
0 1 2 3 4
Linear Representation 2 1 8 4
front 4 8
rear
fig 2.1: A Circular Queue
Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.


front

2 1
Enqueue again?
0 1 2 3 4
Linear Representation 2 1 5 4
front 4 5
rear
rear
fig 2.1: A Circular Queue
Circular Queue
Definition

 A Circular Queue is different than a regular Linear Queue.

 Unlike a Linear Queue, a Circular one can reuse an unused memory in the queue by
circling back to it.

 Hence, Circular Queue is more memory efficient than Linear Queue.


front
Not possible anymore.
2 1
No unused memory.
0 1 2 3 4
Linear Representation 2 1 5 4
front 4 5
rear
rear
fig 2.1: A Circular Queue
Simulation of Operations

front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue
Initialize( )  front=rear=-1
 Check IsEmpty
 Check IsFull rear

 EnQueue (add element


to back i.e. at the rear)
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
 Check IsFull rear

 EnQueue (add element


to back i.e. at the rear)
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Circular Queue: Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
EnQueue( 6 )  rear = (rear + 1)%MaxSize;
 Check IsFull rear

 EnQueue (add element


to back i.e. at the rear)
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Circular Queue: Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
EnQueue( 6 )  rear = 1;
 Check IsFull rear
EnQueue( 2 )  rear = (rear + 1)%MaxSize;
 EnQueue (add element
to back i.e. at the rear)
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Circular Queue: Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
EnQueue( 6 )  rear = 1;
 Check IsFull rear
EnQueue( 2 )  rear = 2;
 EnQueue (add element DeQueue( )  front = (front+1)%MaxSize;
to back i.e. at the rear)
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Circular Queue: Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
EnQueue( 6 )  rear = 1;
 Check IsFull rear
EnQueue( 2 )  rear = 2;
 EnQueue (add element DeQueue( )  front = 1;
to back i.e. at the rear) DeQueue( )  front = (front+1)%MaxSize;
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Circular Queue: Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 3 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
EnQueue( 6 )  rear = 1;
 Check IsFull rear
EnQueue( 2 )  rear = 2;
 EnQueue (add element DeQueue( )  front = 1;
to back i.e. at the rear) DeQueue( )  front = 2;
EnQueue( 5 )  rear = (rear + 1)%MaxSize;
 DeQueue (remove
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Circular Queue: Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 9 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
EnQueue( 6 )  rear = 1;
 Check IsFull rear
EnQueue( 2 )  rear = 2;
 EnQueue (add element DeQueue( )  front = 1;
to back i.e. at the rear) DeQueue( )  front = 2;
EnQueue( 5 )  rear = 3;
 DeQueue (remove EnQueue( 9 )  rear = (rear + 1)%MaxSize;
element from the front)
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Circular Queue: Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 9 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
EnQueue( 6 )  rear = 1;
 Check IsFull rear
EnQueue( 2 )  rear = 2;
 EnQueue (add element DeQueue( )  front = 1;
to back i.e. at the rear) DeQueue( )  front = 2;
EnQueue( 5 )  rear = 3;
 DeQueue (remove EnQueue( 9 )  rear = 0;
element from the front) DeQueue( )  front = (front+1)%MaxSize;
 FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Circular Queue: Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 9 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
EnQueue( 6 )  rear = 1;
 Check IsFull rear
EnQueue( 2 )  rear = 2;
 EnQueue (add element DeQueue( )  front = 1;
to back i.e. at the rear) DeQueue( )  front = 2;
EnQueue( 5 )  rear = 3;
 DeQueue (remove EnQueue( 9 )  rear = 0;
element from the front) DeQueue( )  front = 3;
 DeQueue( )  front = (front+1)%MaxSize;
FrontValue (retrieve
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Circular Queue: Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 9 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
EnQueue( 6 )  rear = 1;
 Check IsFull rear
EnQueue( 2 )  rear = 2;
 EnQueue (add element DeQueue( )  front = 1;
to back i.e. at the rear) DeQueue( )  front = 2;
EnQueue( 5 )  rear = 3;
 DeQueue (remove EnQueue( 9 )  rear = 0;
element from the front) DeQueue( )  front = 3;
 FrontValue (retrieve DeQueue( )  front = 0;
DeQueue( )  front=rear=-1;
value of element from
front)
 ShowQueue (print all
the values of queue
from front to rear)
Circular Queue: Simulation of Operations
front

Queue[4],MaxSize=4; 0 1 2 3 4
Queue 9 6 2 5
Initialize( )  front=rear=-1
 Check IsEmpty EnQueue( 3 )  front=rear=0;
EnQueue( 6 )  rear = 1;
 Check IsFull rear
EnQueue( 2 )  rear = 2;
 EnQueue (add element DeQueue( )  front = 1;
to back i.e. at the rear) DeQueue( )  front = 2;
EnQueue( 5 )  rear = 3;
 DeQueue (remove EnQueue( 9 )  rear = 0;
element from the front) DeQueue( )  front = 3;
 FrontValue (retrieve DeQueue( )  front = 0;
DeQueue( )  front=rear=-1;
value of element from DeQueue( )  Queue Empty, if ((front==-1) && (rear==-1));
front)
 ShowQueue (print all
the values of queue
from front to rear)
Pseudo code of Circular Queue Implementation

Initialize queue[maxSize]; front = rear = -1 , numberOfElement=0;

isEmpty(){
if ((front==-1) and (rear==-1)):
then return true;
}

isFull(){
if ((numberOfElement == maxSize):
then return true;
}

enqueue(x){
if(queue full): {error: “queue full!”;}
otherwise if(queue is empty): {front=rear=0; insert x in queue[rear];
numberOfElement++; }
otherwise: rear=(rear+1)%maxSize; insert x in queue[rear];
numberOfElement++;
}
Pseudo code of Circular Queue Implementation

dequeue(){
if(queue empty): {error: “queue is empty! dequeue not possible”}
otherwise if (front and rear are equal):
{front=rear=-1;
numberOfElement --;}
otherwise:
{front=(front+1)%maxSize;
numberOfElement--;}
}

frontElement(){
return queue[front];
}
Pseudo code of Circular Queue Implementation

showQueue(){
if (queue is empty)
error: “cannot show queue because it is empty!”;
otherwise:{
j= front;
for: i=0; i<numberOfEllement; i++
output: {queue[j];
j= (j+1) % maxSize;
}
}
Books

 “Schaum's Outline of Data Structures with C++”. By John R. Hubbard


 “Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
 “Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
 “Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
 “C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
 “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,
References

1. https://en.wikipedia.org/wiki/Circular_buffer

You might also like