In: Computer Science
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List?
####THIS IS THE ENTIRE ASSIGNMENT####
ADD the Following feature:
Include a class attribute in the container class called name.
In the implementation - Pod:
You should ask the user to enter the name of the container and the program should add the container to the Pod repeatedly.
Once done, You should ask the user which container to remove from the Pod. When the user gives the name of the container,
the program should go through the Pod and remove that container alone from the Pod.
Clue: Remember if you want to remove one item from the Stack, you should use only the top to remove. So if you want to remove the fifth item from the top of the Stack, you should remove all the four items above it, remove the fifth item and then put back all the four items in the same order.
Write a method called removeNamedContainer in Pod program for implementing the above.
*********** PLEASE PLEASE USE THIS CODE TO HELP ME. THANK YOU!!!***********
class container:
def __init__(self, data):
self.data = data
self.next = None
self.previous = None
class Pod:
def __init__(self):
self.head = None
def addContainer(self, data):
if self.head == None:
self.head = container(data)
else:
newContainer = container(data)
newContainer.next = self.head
self.head = newContainer
print(data + " Stacked to the Ship")
def removeContainer(self):
if self.empty():
return None
else:
removedContainer = self.head
self.head = self.head.next
removedContainer.next = None
print(removedContainer.data + " Un-Stacked from the Ship")
def empty(self):
if self.head == None:
return True
else:
return False
MyPod = Pod()
MyPod.addContainer("Container 1")
MyPod.removeContainer()
MyPod.addContainer("Container 2")
MyPod.addContainer("Container 3")
MyPod.removeContainer()
MyPod.addContainer("Container 4")
MyPod.addContainer("Container 5")
MyPod.removeContainer()
MyPod.removeContainer()
MyPod.addContainer("Container 6")
#class for each node
class container:
def __init__(self,name):
self.name = name
self.next = None
self.previous = None
class Pod:
#initialize head as None
def __init__(self):
self.head = None
#method to add a node
def addContainer(self,name):
if(self.head == None): #if linked list is not created,
create it
self.head = container(name)
else:
#else put current node before head and make it
head
newContainer = container(name)
newContainer.next = self.head
newContainer.previous = None
self.head.previous = newContainer
self.head = newContainer
print(name,"stacked to the ship")
#method to remove a node
def removeNamedContainer(self,name):
#if head is not present, it means list is
empty
if(self.head == None):
print("Pod is empty")
return
temp = self.head
while(temp.name != name): #loop till node required is not
found
temp = temp.next
if(temp == None): #if entire list is traversed,
break
break
if(temp == None): #if entire list is traversed but node not
found, display it
print(name,"not found in Pod")
return
#connect previous node with next node
#given both exist
prev = temp.previous
nxt = temp.next
if(prev != None):
prev.next = nxt
if(nxt != None):
nxt.previous = prev
#if head is removed, make next node as
head
if(temp == self.head):
self.head = self.head.next
return
#method to print linked list
def printPod(self):
if self.head == None:
return
temp = self.head
while(temp != None):
print(temp.name,end=", ")
temp = temp.next
print("")
choice = 'y'
p = Pod() #create pod object
#loop till data insertion is not done
while(choice != 'n' and choice != 'N'):
choice = input("Enter another value in Pod? y/n\t")
if(choice == 'y' or choice == 'Y'):
name = input("Enter name of container:\t")
p.addContainer(name)
p.printPod()
choice = 'y'
#loop till data removal is not done
while(choice != 'n' and choice != 'N'):
choice = input("Remove another value from Pod? y/n\t")
if(choice == 'y' or choice == 'Y'):
name = input("Enter name of container to be removed:\t")
p.removeNamedContainer(name)
p.printPod()
Code screenshot for indentation help:
Sample execution:
In case of any doubt, drop a comment and I'll surely get back to you.
Please give a like if you're satisfied with the answer. Thank you.