0% found this document useful (0 votes)
38 views3 pages

Lab Task#11

This document defines a Queue class that implements a queue data structure using linked nodes. The Queue class initializes with an empty front and rear. It contains methods to add nodes to the rear of the queue, remove nodes from the front of the queue, and update a node in the queue. The driver code demonstrates adding, removing, and updating nodes in a queue.

Uploaded by

Sheikh Alyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Lab Task#11

This document defines a Queue class that implements a queue data structure using linked nodes. The Queue class initializes with an empty front and rear. It contains methods to add nodes to the rear of the queue, remove nodes from the front of the queue, and update a node in the queue. The driver code demonstrates adding, removing, and updating nodes in a queue.

Uploaded by

Sheikh Alyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

--------------------------------------------------------

Q1--------------------------------------------------------

class Node:

def __init__(self, data):


self.data = data
self.next = None

class Queue:

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

def isEmpty(self):
return self.front == None

def AddInQueue(self, item):


temp = Node(item)

if self.rear == None:
self.front = self.rear = temp
return
self.rear.next = temp
self.rear = temp

def RemoveFromQueue(self):

if self.isEmpty():
return
temp = self.front
self.front = temp.next

if (self.front == None):
self.rear = None

def UpdateOfQueue(self,item):
temp = Node(item)

if self.rear == None:
self.front = self.rear = temp
return
self.rear.next = temp
self.rear = temp
print(f"The element updated now is:{item}")

# Driver Part
if __name__ == '__main__':
q = Queue()
q.AddInQueue(15)
q.AddInQueue(90)
q.AddInQueue(10)
q.AddInQueue(20)
q.RemoveFromQueue()
q.RemoveFromQueue() #10 and 20 removed each time Deque is called
q.AddInQueue(30)
q.AddInQueue(40)
q.AddInQueue(50)
q.AddInQueue(45)
q.RemoveFromQueue() #30 dequed now front would be 40 and rear side of the
queue whould have 50
q.UpdateOfQueue(12) # Updating (Injecting from the rear part of queue hence
only allowed)
print("Front of queue is: " + str(q.front.data))
print("Rear part of queue is: " + str(q.rear.data))
--------------------------------------------------------
Q2--------------------------------------------------------
class Queue: #MUHAMMAD UMAR KHAN 10619
def __init__(self): #DSA LAB 105079
self.elementONE = []
self.elementTWO = []

def AddInQueue(self, x):

while len(self.elementONE) != 0:
self.elementTWO.append(self.elementONE[-1])
self.elementONE.pop()

self.elementONE.append(x)

while len(self.elementTWO) != 0:
self.elementONE.append(self.elementTWO[-1])
self.elementTWO.pop()

def RemoveFromQueue(self):

if len(self.elementONE) == 0:
print("First stack is Empty")

x = self.elementONE[-1]
self.elementONE.pop()
return x

# Driver code
if __name__ == '__main__':
q = Queue()

#Adding elements to the stack


q.AddInQueue(1)
q.AddInQueue(2)
q.AddInQueue(3)
q.AddInQueue(4)
q.AddInQueue(5)

print("Removing elements from the stack")


print(q.RemoveFromQueue())
print(q.RemoveFromQueue())
print(q.RemoveFromQueue())
print(q.RemoveFromQueue())
print(q.RemoveFromQueue())
print("All elements removed from the stack!")

You might also like