Write a python class to implement circular queue with methods enqueue, dequeue


Topic: Write a python class to implement circular queue with methods enqueue, dequeue

Solution

class CircularQueue(object):
    def __init__(self, limit = 10):
        self.limit = limit
        self.queue = [None for i in range(limit)]  
        self.front = self.rear = -1
    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
    def isEmpty(self):
        return self.front == -1
    def isFull(self):
        return (self.rear + 1) % self.limit == self.front
    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 
    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 
			
			



List all Python Programs