Circular Queue Dsa
Circular Queue Dsa
Data Structures
Circular Queue
1
Circular Queue
There was one limitation in the array
implementation of Queue.
If the rear reaches to the end position of the
Queue then there might be possibility that
some vacant spaces are left in the
beginning which cannot be utilized.
So, to overcome such limitations, the
concept of the circular queue was
introduced.
2
Circular Queue
3
What is a Circular Queue?
A circular queue is similar to a linear queue as
it is also based on the FIFO (First In First Out)
principle except that the last position is
connected to the first position in a circular
queue that forms a circle.
It is also known as a Ring Buffer.
4
Operations on Circular
Queue
Front: It is used to get the front element from
the Queue.
Rear: It is used to get the rear element from
the Queue.
enQueue(value): This function is used to insert
the new value in the Queue. The new element
is always inserted from the rear end.
deQueue(): This function deletes an element
from the Queue. The deletion in a Queue
always takes place from the front end.
5
Algorithm to insert an element in a
circular queue
6
Algorithm to delete an element from the
circular queue
7
diagrammatic representation.
8
More operations
9
Implementation of circular queue using
linked list
void enqueue(int x) {
if(rear==-1) // checking whether the Queue is
empty or not. {
front=rear=newnode;
rear->next=front;
}
else {
rear->next=newnode;
rear=newnode;
rear->next=front;
}
}
10
Implementation of circular queue using
linked list
void dequeue() {
temp=front;
if((front==-1)&&(rear==-1)) // checking whether the queue is empty or
not {
print(Queue is empty) }
else if(front==rear) // checking whether the single element is left in the
queue{
front=rear=-1;
free(temp);
}
else {
front=front->next;
rear->next=front;
free(temp);
} }
11
Applications of Queue
Scheduling queues refers to queues of processes or devices. When
the process enters into the system, then this process is put into a job
queue. This queue consists of all processes in the system. The
operating system also maintains other queues such as device queue.
Device queue is a queue for which multiple processes are waiting for a
particular I/O device. Each device has its own device queue.
12
Applications of Queue
Message queues provide an asynchronous communications protocol,
a system that puts a message onto a message queue does not require
an immediate response to continue processing. Email is probably the
best example of asynchronous messaging. When an email is sent can
the sender continue processing other things without an immediate
response from the receiver. This way of handling messages decouples
the producer from the consumer. The producer and the consumer of
the message do not need to interact with the message queue at the
same time.
13