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...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
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)
Using the singly linked list code as a base, create a class that implements a doubly...
Using the singly linked list code as a base, create a class that implements a doubly linked list. A doubly linked list has a Previous link so you can move backwards in the list. Be sure the class is a template class so the user can create a list with any data type. Be sure to test all the member functions in your test program. c++
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...
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...
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:...
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   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT