Circular Queue using Linked List in Java
Implementation of Circular Queue using linked list
Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position.
Why we need circular queue ??
When we work with normal Queue, we can insert elements till queue is full. But when queue is full, we are not allowed to insert the next element this is the problem with normal queue . To overcome this problem circular queue is used .
A circular queue overcomes the problem of unused space in normal queues, and it can be implemented on both arrays and linked list.
Operation In Circular Queue:
The following are the operations that can be performed on a circular queue:
- Front: It is used to get the front item from the Queue.
- Rear: It is used to get the last element from the Queue.
- enQueue() This operation adds a new node after rear and moves rear to the next node.
- deQueue() This operation removes the front node and moves front to the next node.
Algorithm for Insertion:
static void enQueue(Queue q, int d)
Algorithm for Deletion:
Java code :
AFTER DELETING ELEMENT ELEMENT LEFT
AFTER INSERTING NEW VALUE