Write a program to create a queue that permits insertion at any vacant location at the rear end.
# Linked list node
# To store a queue entry
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Class to represent a queue
# The queue, front stores the front node
# of linked list and rear stores the last node of linked list
class Queue:
def __init__(self):
self.front = self.rear = None
def isEmpty(self):
return self.front == None
# Method to add an item to the queue
def EnQueue(self, item):
Temp = Node(item)
if self.rear == None:
self.front = self.rear = Temp
return
self.rear.next = Temp
self.rear = Temp
# Method to remove an item from queue
def DeQueue(self):
if self.isEmpty():
return
Temp = self.front
self.front = Temp.next
if(self.front == None):
self.rear = None
# Driver Code
if __name__== '__main__':
Q = Queue()
Q.EnQueue(15)
Q.EnQueue(25)
Q.DeQueue()
Q.DeQueue()
Q.EnQueue(35)
Q.EnQueue(45)
Q.EnQueue(55)
Q.DeQueue()
print("Queue Front " + str(Q.front.data))
print("Queue Rear " + str(Q.rear.data))
Comments
Leave a comment