A Circular Queue is a kind of queue that can insert elements inside it dynamically. Suppose, in a given array there is not any space left at the rear but we have space at the front in the normal queue it is not possible to insert elements at the front but in the case of a circular queue we can do that. So in this case, once the array is full till the last space then we insert the further elements at the front if there is any space left. That's why it is known as a circular queue.
Operations on Circular Queue:
- Front: Get the front item from the queue.
- Rear: Get the last item from the queue.
- enQueue(value) This function is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at the rear position.
- Check whether the queue is full – [i.e., the rear end is in just before the front end in a circular manner].
- If it is full then display Queue is full.
- If the queue is not full then, insert an element at the end of the queue.
- deQueue() This function is used to delete an element from the circular queue. In a circular queue, the element is always deleted from the front position.
- Check whether the queue is Empty.
- If it is empty then display Queue is empty.
- If the queue is not empty, then get the last element and remove it from the queue.
Implementation of Circular Queue in Python
In a normal queue, we keep on adding the elements at the rear of the array and once we reach the end of the array we will not be able to insert the elements further. To get rid of this problem we can use the spaces available at the front which is created while removing elements from the queue. We will use the following method to implement the circular queue.
- When we insert the element in the queue we will increment the index by (index+1)%size instead of just adding one to it.
- This will ensure that when we reach the end of the array the index will move again to the start of the array.
- Now check if the space is empty or not. If the space is empty we can insert the elements further in it.
- In this method we are inserting elements circularly that's why this method is called circular queue.
Code:
Python
class CircularQueue:
def __init__(self, size):
self.maxSize = size
self.queueArray = [0] * size
self.front = -1
self.rear = -1
def enqueue(self, item):
if self.isEmpty():
self.front = 0
self.rear = 0
self.queueArray[self.rear] = item
else:
self.rear = (self.rear + 1) % self.maxSize
if self.rear == self.front:
print("Queue is full. Cannot enqueue.")
self.rear = (self.rear - 1 + self.maxSize) % self.maxSize
else:
self.queueArray[self.rear] = item
def dequeue(self):
item = -1 # Assuming -1 represents an empty value
if not self.isEmpty():
item = self.queueArray[self.front]
if self.front == self.rear:
self.front = -1
self.rear = -1
else:
self.front = (self.front + 1) % self.maxSize
else:
print("Queue is empty. Cannot dequeue.")
return item
def peek(self):
if not self.isEmpty():
return self.queueArray[self.front]
else:
print("Queue is empty. No peek value.")
return -1 # Assuming -1 represents an empty value
def isEmpty(self):
return self.front == -1 and self.rear == -1
if __name__ == "__main__":
circularQueue = CircularQueue(5)
circularQueue.enqueue(1)
circularQueue.enqueue(2)
circularQueue.enqueue(3)
# Should print 1
print("Peek:", circularQueue.peek())
# Should print 1
print("Dequeue:", circularQueue.dequeue())
# Should print 2
print("Peek after dequeue:", circularQueue.peek())
Output('Peek:', 1)
('Dequeue:', 1)
('Peek after dequeue:', 2)
Time complexity Circular Queue in Python:
- Enqueue: O(1)
- Dequque: O(1)
- Peek: O(1)
- isEmpty: O(1)
Auxiliary Space complexity Circular Queue in Python:
Similar Reads
Circular Bar Plot in Python In this guide, we'll dive into the world of circular bar plots in Python. We'll explore what they are, why they're useful, and how you can create them to add a stylish touch to your data visualization projects. What is a Circular Bar Plot?Circular bar plots, also known as radial bar charts or circul
4 min read
Circular Linked List in Python A Circular Linked List is a variation of the standard linked list. In a standard linked list, the last element points to null, indicating the end of the list. However, in a circular linked list, the last element points back to the first element, forming a loop. In this article, we will discuss the c
13 min read
Calculate Pi with Python Pi is an irrational number having non-recurring decimal values. We commonly know Pi=3.14 or Pi=22/7, but it is just an approximation for our ease. In this article, we will see how to calculate PI with Python and also how to use PI in Python. Calculate and Use PI in PythonBelow are the ways by which
3 min read
Circular Linked List in C++ A circular linked list is a linear data structure similar to a singly linked list where each node consists of two data members data and a pointer next, that stores the address of the next node in the sequence but in a circular linked list, the last node of the list points to the first node instead o
10 min read
Cropping an Image in a circular way using Python In this article, we will learn to crop an image circularly using a pillow library. Cropping an image circularly means selecting a circular region inside an image and removing everything outside the circle. Approach: If you have an L mode image, the image becomes grayscale. So we create a new image
2 min read