Question

In: Computer Science

Python Stack: The following was done already in the Lab in the Stacks Module with Doubly...

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.

Solutions

Expert Solution

# 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)


Related Solutions

This is the code what I have for doubly linked list for STACK. This is Python...
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...
In C language using stacks: Activity: Using Stack, check whether the following is balanced or not...
In C language using stacks: Activity: Using Stack, check whether the following is balanced or not : “{ ( ) } ) ” -Assume a stack of Max_Size = 6 -Draw the stack for each step and show the value of “top” -If any decision is made (valid/invalid), then write the reason 11
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
Suppose that two stacks of positive integers are needed for a project. Each stack is to...
Suppose that two stacks of positive integers are needed for a project. Each stack is to contain integers that are less than or equal to 500. One stack is to contain even integers; the other stack is to contain odd integers. Also, the total number of values in the combined stacks at any given time will not exceed 200. ▪ Design a way to implement both stacks in only one(pay attention!!!) 1-D array. ▪ Write a Python class definition(s) for...
In the Stack Module I gave you a project that shows how to create a Stack...
In the Stack Module I gave you a project that shows how to create a Stack using doubly linked nodes. StackWithDoublyLinkedNodes.zip It is a template class that needs the Node class. You must use this same Node class (no alterations) to create a Queue class . public class Queue <T>{ } Use the NetBeans project above as an example. I have put a driver program (q1.java) in the module. Also here:  q1.java This driver program should then run with your Queue...
This lab problem demonstrates the use of import module. The Python programming language has many strengths,...
This lab problem demonstrates the use of import module. The Python programming language has many strengths, but one of its best is the availability to use many existing modules for various tasks, and you do not need to be an experienced computer programmer to start using these modules. We have given you some incomplete code; note that the very first line of that code contains an import statement as follows: import math This statement enables your program to use a...
Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
Create a binary search tree of stacks where each node contains a key and a stack.
IN C++   Create a binary search tree of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key, and a empty...
5 How Many Stacks? An NFA has no stack. It recognizes regular languages. A PDA is...
5 How Many Stacks? An NFA has no stack. It recognizes regular languages. A PDA is defined as an NFA with one stack. It recognizes context-free languages. Prove that a PDA with two stacks recognizes Turing-recognizable languages
As a lab instructor in machining, you are required to prepare a lab module for a...
As a lab instructor in machining, you are required to prepare a lab module for a laboratory session with your students. In this session, the students are required to produce aT-Slot. Prepare the methodology to produce the T -Slot with illustrations for the lab module.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT