Circular Queue Implementation in Python

 

 

This program implements a circular queue using a fixed-size list. The circular queue overcomes the limitation of wasting spaces in normal queue operations by reconnecting the end of the queue back to its beginning.

Program Structure

The circular queue uses an array to store elements and two pointers, front and rear, to track the start and end of the queue respectively. Here’s how the basic operations work:

  • Enqueue: Adds an element to the rear end of the queue. If the queue is full, it will return an overflow condition.
  • Dequeue: Removes an element from the front of the queue. If the queue is empty, it will return an underflow condition.
  • Is Full/Is Empty: Checks whether the queue is full or empty to prevent overflow and underflow.

Python Code

class CircularQueue:
    def __init__(self, size):
        self.size = size
        self.queue = [None] * size
        self.front = self.rear = -1

    def enqueue(self, item):
        if (self.rear + 1) % self.size == self.front:
            print("Queue is Full")
        elif self.front == -1:
            self.front = self.rear = 0
            self.queue[self.rear] = item
        else:
            self.rear = (self.rear + 1) % self.size
            self.queue[self.rear] = item

    def dequeue(self):
        if self.front == -1:
            print("Queue is Empty")
        elif self.front == self.rear:
            self.queue[self.front] = None
            self.front = self.rear = -1
        else:
            self.queue[self.front] = None
            self.front = (self.front + 1) % self.size

    def display(self):
        if self.front == -1:
            print("Queue is Empty")
        else:
            start = self.front
            while True:
                print(self.queue[start], end=" ")
                if start == self.rear:
                    break
                start = (start + 1) % self.size
            print()

# Example Usage
cq = CircularQueue(5)
cq.enqueue(1)
cq.enqueue(2)
cq.enqueue(3)
cq.enqueue(4)
cq.enqueue(5)
cq.enqueue(6)  # Should show "Queue is Full"
cq.display()  # Should display 1 2 3 4 5
cq.dequeue()
cq.dequeue()
cq.enqueue(7)
cq.enqueue(8)
cq.display()  # Should display 3 4 5 7 8

Explanation of the Code

  • __init__: Initializes the queue with a given size, setting both front and rear to -1, indicating an empty queue.
  • enqueue: Adds an item at the rear and handles circular index movement.
  • dequeue: Removes an item from the front and handles circular index movement.
  • display: Outputs elements from the front to the rear, following the circular nature of the queue.

Usage

This circular queue implementation can be used in scenarios where constant memory usage is critical, such as in embedded systems or when implementing resource-shared environments.

 

One Reply to “Circular Queue Implementation in Python”

Leave a Reply to Cheap Backlinks Cancel reply

Your email address will not be published. Required fields are marked *