In: Computer Science
Please write in Python(Python3)
Stack: The following was done already in the Lab in the Stacks Module with Doubly Linked List.(***Code Below***)
Create an application to help you stack and un-stack containers in the ship.
Create a class called container which will have the object (data), the link (next)
Create a class called Pod which is Stack. Include methods addContainer and removeContainer
Implement these classes by creating multiple containers to go inside the pod.
ADD the Following feature:(*This is what I need to ADD to the code.)
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.
# DSTACK #
#****************************************************
# the container
class Container:
# initialise the container object
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# The pod
class Pod:
stack=[]
# push item to the stack
def addContainer(self, data):
c = Container(data)
c.next = None
self.stack.append(c)
# set the next item and prevous
length = len(self.stack)
# print(str(length))
if length>1:
self.stack[length-2].next=self.stack[length-1]
self.stack[length-1].prev=self.stack[length-2]
# pop the stack
def removeContainer(self):
self.stack.pop()
# reset the next link
length = len(self.stack)
self.stack[length-1].next=None
# to print items
def printList(self):
print("Traversal in forward direction")
temp = self.stack[0]
while (temp):
print(temp.data)
temp = temp.next
print("Traversal in reverse direction")
temp = self.stack[len(self.stack)-1]
while (temp):
print(temp.data)
temp = temp.prev
break
# Testing the stack
print("Adding containers...")
pod = Pod()
pod.addContainer(1)
pod.addContainer(2)
pod.addContainer(3)
pod.printList()
# poping the stack
print("Removing containers...")
pod.removeContainer()
pod.printList()
# container class containing data and next attributes
class container():
def _init__(self,data):
self.data=data
self.next=None
#pod class containing addcontainer and removecontainer methods
class pod():
# addcontainer
def addContainer(self,head,containerObj):
# check if intial container exists to stack contianers
if(head):
temp = head
while(temp.next):
temp=temp.next
temp.next=containerObj
return head
else:
return containerObj
# removecotainer
def removeContainer(self,head):
# removing container in stack manner such that the container which is on top
# removed first
if(head.next):
temp = head
prev = None
while(temp.next):
prev=temp
temp=temp.next
prev.next=None
return head
else:
return None
# method to list all the available containers and ships
def helper(head):
while(head):
print(head.data)
head=head.next
print("working with container and pod")
# containers
container_one = container("container_1")
container_two = container("container_2")
container_three = container("container_3")
container_four = container("container_4")
# pod object
podObj = pod()
# adding containers to pod
head = pod Obj.add Container(Non,container one)
head = podObj.addContainer(head,container_two)
head = podobj.addContainer(head, container_three)
head = podobj.addContainer(head,container_four)
# listing all the containeer availabl to cross check
print("ALI the added containeers")
helper(head)
#removind containers from stack so it results in removing
#container_4 and container_3
head = podObj.removeContainer(head)
head = podObj.removeContainer(head)
# listing all the containeer availabl to cross check
print("left over containeers after removing few in stack manner(LIFO)")
helper(head)
# ship class containing data and next attributes
class ship():
def __init__(self,data):
self.data=data
self.next=None
class Port():
# adding ships to dock
def dock(self,head, ship Obj):
if(head):
temp=head
while(temp.next)
temp=temp.next
temp.next=shipObj
return head
else:
return shipObj
# undocking ships in first come first out manner
def undock(self,head):
if(head):
temp = head.next
head.next=None
head=temp
return head
else:
return None
print("working with ship and Port")
# ships
ship_one = ship("ship_1")
ship_two = ship("ship_2")
ship_three = ship("ship_3")
ship four = ship("ship_4")
portObj = Port()
# dock ships
head = portObj.dock(None,ship_one)
head = portObj.dock(head, ship_two)
head = portObj.dock(head, ship_three)
head = portObj.dock(head, ship_four)
# listing all the ships available to cross check
print("All the ships available in port")
helper(head)
# undock ships from the port it results in removing
#ship_1 and ship_2 as the first ship in queue is served first
head = portObj.undock(head)
head = portObj.undock(head)
# listing all the ships available to cross check
print("All the ships available in port after undocking few in Queue manner (FIFO)")
helper(head)