In: Computer Science
Python
Stack: The following was done already in the Lab in the Stacks Module with Doubly Linked List.
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:
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.
# 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 = podObj.addContainer(None,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("ALl 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,shipObj):
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)