Question

In: Computer Science

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

Solutions

Expert Solution

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


Related Solutions

I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
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...
In python I have a linked list. I want to create one function that takes in...
In python I have a linked list. I want to create one function that takes in one parameter, head. In the function, cur = head and next_cur = head.next. I want to return head and next_cur, except at the end of the function they will return alternating values from head. For example, if the these are the values in the linked list: 2, 3, 5, 7, 11 after the function head should return: 2, 5, 11 and next_cur should return:...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
I need an example of how to swap and index within a doubly linked list with...
I need an example of how to swap and index within a doubly linked list with index + 1. This is written in java. public class A3DoubleLL<E> {    /*    * Grading:    * Swapped nodes without modifying values - 2pt    * Works for all special cases - 1pt    */    public void swap(int index) {        //swap the nodes at index and index+1        //change the next/prev connections, do not modify the values   ...
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...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT