Lecture 2.2.2 Circular Queue
Lecture 2.2.2 Circular Queue
2
Circular
Queues
• Circular Queue is also a linear
data structure, which follows the
principle of FIFO(First In First
Out), but instead of ending the
queue at the last position, it
again starts from the first
position after the last, hence
making the queue behave like a
circular data structure.
•
3
Basic features of Circular
Queue
• In case of a circular
queue, head pointer will always
point to the front of the queue,
and tail pointer will always point
to the end of the queue.
• Initially, the head and the tail
pointers will be pointing to the
same location, this would mean
that the queue is empty.
4
Basic features of Circular
•Queue
New data is always added to the location • In a circular queue, data not actually
pointed by the tail pointer, and once the data removed from is the queue. Only
is added, tail pointer is incremented to point the head pointer is incremented by
to the next available location. position when dequeueone is executed. As the
queue data is only the
between head and tail, hence the data left
data
outside is not a part of the queue anymore,
hence removed.
5
• The head and the tail pointer will • The head and the tail pointers can cross each
other. In other words, head pointer can be
get reinitialised to 0 every time greater than the tail. Sounds odd? This will
they reach the end of the queue. happen when we dequeue the queue a
couple of times and the tail pointer gets
reinitialised upon reaching the end of the
queue.
6
Application of Circular
Queue
real-world examples where circular queues are used:
• Computer controlled Traffic Signal System uses circular queue.
• CPU scheduling and Memory management.
7
Inserting an element in a circular
queue
• If front = 0 and rear = MAX – 1, then the
circular queue is full. Look at the queue given
in Fig. 8.16 which illustrates this point.
• If rear != MAX – 1, then rear will be
incremented and the value will be inserted as
illustrated in Fig. 8.17.
• If front != 0 and rear = MAX – 1, then it
means that the queue is not full. So, set rear =
0 and insert the new element there, as
shown in Fig. 8.18.
8
Algorithm to insert an element in a circular
queue
• In Step 1, we check for
overflow
the condition.
• In Step 2, we make two
First
checks.to see if the queue is
empty, and second to see if the
REAR end has already reached
the maximum capacity while
there are certain free locations
before the FRONT end.
• In Step 3, the value is stored in
the queue at the location
pointed by REAR.
9
Delete an element from a circular queue
11
Deques
• A deque (pronounced as ‘deck’ or
‘dequeue’) is a list in which the elements
can be inserted or deleted at either end.
• It is also known as a head-tail linked list
because elements can be added to or
removed from either the front (head) or
the back (tail) end.
• However, no element can be added and
deleted from the middle. In the
computer’s memory, a deque is
implemented using either a circular array
or a circular doubly linked list.
• In a deque, two pointers are
maintained, LEFT and RIGHT, which point
to either end of the deque.
• The elements in a deque extend from the
LEFT end to the RIGHT end and since it is
circular, Dequeue[N–1] is followed by
Dequeue[0].
12
Types of
Deque
1. Input restricted deque: In this dequeue, insertions can be done
only at one of the ends, while deletions can be done from both
ends.
2. Output restricted deque: In this dequeue, deletions can be done
only at one of the ends, while insertions can be done on both
ends.
13
Operations on a Deque
Operations, these steps are
followed.
1. Take an array (deque) of
size n.
2. Set two pointers at the first
position and set front = -
1 and rear = 0.
I
n
i
t
i
a 14
Insert at the Front
This operation adds an element at 2. If front < 1, reinitialize front = n-
the front. 1 (last index).
1. Check the position of front.
15
3. Else, decrease front by 1.
4. Add the new key 5 into array[front].
16
2. Insert at the Rear If the deque is full, reinitialize rear
This operation adds an element to = 0.
the rear. Else, increase rear by 1.
Check if the array is full.
17
Add the new key 5 into array[rear].
18
3. Delete from the
Front 2. If the deque is empty (i.e. front = -1),
deletion cannot be performed (underflow
The operation deletes an element
from the front. condition).
1. Check if the deque is empty 3. If the deque has only one element
(i.e. front = rear), set front = -1 and rear =
-1.
4.Else if front is at the end (i.e. front = n -
1), set go to the front front = 0.
5. Else, front = front + 1
Check if deque is empty
Increase the front
19
4. Delete from the
Rear 2. If the deque is empty (i.e. front = -1),
This operation deletes an element deletion cannot be performed (underflow
from the rear. condition).
1. Check if the deque is empty. 3. If the deque has only one element (i.e.
front
= rear), set front = -1 and rear = -1, else follow
the steps below.
4.If rear is at the front (i.e. rear = 0), set go to
the front rear = n - 1.
Check if deque is empty
5. Else, rear = rear - 1.
Decrease the rear
20
5. Check Empty
• This operation checks if the deque is empty. If front = -1, the deque is
empty.
6. Check Full
• This operation checks if the deque is full. If front = 0 and rear = n -
1 OR front = rear + 1, the deque is full.
21
Time Complexity
• The time complexity of all the above operations is constant i.e. O(1).
22
Priority
• A priority queue can be thought of as
Queues
• Apriority queue is a data structure in a modified queue in which when an
which each element is assigned a element has to be removed from the
priority. queue, the one with the highest-
• The priority of the element will be priority is retrieved first.
used to determine the order in which • The priority of the element can be set
the elements will be processed. based on various factors.
• The general rules of processing the • Priority queues are widely used in
elements of a priority queue are operating systems to execute the
1. An element with higher priority is highest priority process first.
processed before an element with a
lower priority. • The priority of the process may be set
2. Two elements with the same priority based on the CPU time it requires to
are processed on a first-come-first- get executed completely.
served (FCFS) basis
23
APPLICATIONs OF
QUEUES
• Queues are widely used as waiting • Queues are used in operating system
lists for a single shared resource like for handling interrupts. When
printer, disk, CPU. programming a real-time system that
• Queues are used to transfer data can be interrupted, for example, by a
asynchronously (data not necessarily mouse click, it is necessary to process
received at same rate as sent) the interrupts immediately, before
between two processes (IO buffers), proceeding with the current job. If the
e.g., pipes, file IO, sockets. interrupts have to be handled in the
order of arrival, then a FIFO queue is
• Queues are used as buffers on MP3 the appropriate data structure.
players and portable CD players, iPod
playlist.
• Queues are used in Playlist for jukebox
to add songs to the end, play from the
front of the list.
24
REFERENCES
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in C++”, Wiley Student
Edition.
• https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
• https://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecture2.html
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall of India
25
THANK
YOU