class Node(object):
def init(self, data):
self.data = data
self.nextNode = None
class Queue(object):
def init(self):
self.head = None
self.tail = None
self.size = 0
def enqueue(self,data):
self.size += 1
newNode = Node(data)
if self.head is None:
self.head = self.tail = newNode
else:
newNode.nextNode = self.head
self.head = newNode
def dequeue(self):
self.size -= 1
actualNode = self.head
if self.size == 0:
data = self.head.data
del self.head
self.head = self.tail = None
else:
while actualNode.nextNode is not self.tail:
actualNode = actualNode.nextNode
data = self.tail.data
del self.tail
self.tail = actualNode
return data
def queue_size(self):
return self.size
def isEmpty(self):
return self.head == self.tail == None
def print_queue(self):
if self.head is None:
print("The queue is empty")
return
print("****")
actualNode = self.head
while actualNode is not None:
print(actualNode.data)
actualNode = actualNode.nextNode
print("****")
q = Queue()
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
q.enqueue(40)
q.print_queue()
print("The size of the queue is " + str(q.queue_size()))
print("Popped : " + str(q.dequeue()))
print("Popped : " + str(q.dequeue()))
print("Popped : " + str(q.dequeue()))
print("Popped : " + str(q.dequeue()))
print("The size of the queue is " + str(q.queue_size()))
q.print_queue()