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...
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...
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...
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.
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack • When an operand is read, push it on statck • When an operator is read i.e +, *. /, - – Pop two from the top of the stack and apply the operator and push the result on stack if there is one value instead of two display an error message • Keep repeating until an equal sign, = is read; pop from...
A doubly drained specimen, 2.54 cm in height, is consolidated in the lab under an applied...
A doubly drained specimen, 2.54 cm in height, is consolidated in the lab under an applied stress. The time for 50% overall (or average) consolidation is 12 min. (a) Compute the cv value for the lab specimen. (b) How long will it take for the specimen to consolidate to an average consolidation of 50%? (c) If the final consolidation settlement of the specimen is expected to be 0.43 cm, how long will it take for 0.18 cm of settlement to...
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write...
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT