### Circular Queue Implementation in Python

In this source code example, we will write a code to implement the Circular Queue data structure in Python.

A circular queue is the extended version of a regular queue where the last element is connected to the first element. Thus forming a circle-like structure.

The circular queue solves the major limitation of the normal queue. In a normal queue, after a bit of insertion and deletion, there will be non-usable empty space.

# Circular Queue Implementation in Python

``````class CircularQueue(object):
def __init__(self, limit = 10):
self.limit = limit
self.queue = [None for i in range(limit)]
self.front = self.rear = -1

# for printing the queue
def __str__(self):
if (self.rear >= self.front):
return ' '.join([str(self.queue[i]) for i in range(self.front, self.rear + 1)])

else:
q1 = ' '.join([str(self.queue[i]) for i in range(self.front, self.limit)])
q2 = ' '.join([str(self.queue[i]) for i in range(0, self.rear + 1)])
return q1 + ' ' + q2

# for checking if queue is empty
def isEmpty(self):
return self.front == -1

# for checking if the queue is full
def isFull(self):
return (self.rear + 1) % self.limit == self.front

# for adding an element to the queue
def enqueue(self, data):
if self.isFull():
print('Queue is Full!')
elif self.isEmpty():
self.front = 0
self.rear = 0
self.queue[self.rear] = data
else:
self.rear = (self.rear + 1) % self.limit
self.queue[self.rear] = data

# for removing an element from the queue
def dequeue(self):
if self.isEmpty():
print('Queue is Empty!')
elif (self.front == self.rear):
self.front = -1
self.rear = -1
else:
self.front = (self.front + 1) % self.limit

if __name__ == '__main__':
myCQ = CircularQueue(5)
myCQ.enqueue(14)
myCQ.enqueue(22)
myCQ.enqueue(13)
myCQ.enqueue(16)
print('Queue:',myCQ)
myCQ.dequeue()
myCQ.dequeue()
print('Queue:',myCQ)
myCQ.enqueue(9)
myCQ.enqueue(20)
myCQ.enqueue(5)
myCQ.enqueue(5)
print('Queue:',myCQ)
``````

Output:

``````Queue: 14 22 13 16
Queue: 13 16
Queue is Full!
Queue: 13 16 9 20 5``````

# Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course