CircularQ

 avatar
shinta0x01
python
2 months ago
1.7 kB
7
Indexable
Never
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularQ:
    def __init__(self):
        self.front = None
        self.rear = None

    def enqueue(self, data):
        node = Node(data)
        if self.rear is None:
            self.front = self.rear = node
            self.rear.next = self.front
        else:
            self.rear.next = node
            self.rear = node
            self.rear.next = self.front

    def dequeue(self):
        if self.rear is None:
            self.is_empty()
        else:
            if self.front == self.rear:
                self.front = self.rear = None
            else:
                self.front = self.front.next
                self.rear.next = self.front

    def front_q(self):
        if self.rear is None:
            self.is_empty()
        else:
            print(self.front.data)

    def rear_q(self):
        if self.rear is None:
            self.is_empty()
        else:
            print(self.rear.data)

    def is_empty(self):
        if self.rear is None:
            print('Queue is empty')

    def display(self):
        current = self.front
        while True:
            if self.rear is None:
                break
            print(current.data, '', end='')
            current = current.next
            if current == self.front:
                break



queue = CircularQ()

queue.enqueue(10)
queue.enqueue(9)
queue.enqueue(4)
queue.enqueue(1)
# queue.dequeue()
# queue.dequeue()
# queue.dequeue()
# queue.dequeue()
queue.display()
print()
queue.front_q()
queue.rear_q()

Leave a Comment