DEQUE

 avatar
shinta0x01
python
a month ago
1.5 kB
3
Indexable
Never
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

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

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

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

    def dequeue_rear(self):
        current = self.front
        if self.rear is None:
            self.is_empty()
        else:
            while current.next != self.rear:
                current = current.next
            current.next = None
            self.rear = current

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

    def display(self):
        current = self.front
        while current != None:
            print(current.data, '', end='')
            current = current.next

queue = DoubleQ()

queue.enqueue(10)
queue.enqueue(2)
queue.enqueue(5)
queue.enqueue(7)
queue.enqueue_rear(121)
queue.dequeue()
queue.dequeue_rear()
queue.display()
Leave a Comment