Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python programming question. Given the following code: here is the code from the screenshot above, if it makes it easier to copy. class Node: def

Python programming question.

image text in transcribedimage text in transcribed

Given the following code:

image text in transcribedimage text in transcribed

image text in transcribedhere is the code from the screenshot above, if it makes it easier to copy.

class Node: def __init__(self, value): self.value = value self.next = None self.prev = None

def getValue(self): return self.value

def getNext(self): return self.next

def setValue(self,new_value): self.value = new_value

def setNext(self,new_next): self.next = new_next

def getPrevious(self): return self.prev

def setPrevious(self,new_prev): self.prev = new_prev

def __str__(self): return ("{}".format(self.value))

__repr__ = __str__

class DoublyLinkedList: # Do NOT modify the constructor def __init__(self): self.head = None def addFirst(self, value): # write your code here def addLast(self, value): # write your code here def addBefore(self, pnode_value, value): # write your code here def addAfter(self, pnode_value, value): # write your code here

def printDLL(self): temp=self.head print(" Traversal Head to Tail") while temp: print(temp.getValue(), end=' ') last = temp temp=temp.getNext() print(" Traversal Tail to Head") while(last is not None): print(last.getValue(), end=' ') last = last.prev

def getNode(self,value): current=self.head found=False while current!=None and not found: if current.getValue()==value: found=True return current else: current=current.getNext() return

Instructions: In class, we discussed the Doubly Linked List Abstract Data Type (section 1.1.4 of your Lecture Notes). A Doubly Linked List contains an extra pointer (known as previous pointer) allowing the traversal of the list in both forward and backward direction. When implementing the LinkedList data structure, we observed the importance of updating the pointers in order to get access to the entire list. Implement the data structure DoublyLinkedList with the following characteristics: . DoublyLinkedList) creates a new doubly linked list that is empty. It needs no parameters e addFirt(item) adds a new Node with value-item at the beginning of the list. It needs the addLast(item) adds a new Node with value-item at the end of the list. It needs the item and and returns nothing. Assume the items in the list are unique tem and returns nothing. 20 pts returns nothing. [20 pts] * addBefore(pnode value, item) adds a new Node with value-item before the Node with value=pnode value. It needs the value of the reference Node and the item to be added returns nothing. You can assume the reference node is in the list. [30 pts] e addAfter(pnode value, item) adds a new Node with value-item after the Node with value=pnode-value. It needs the value of the reference Node and the item to be added returns nothing. You can assume the reference node is in the list. [30 pts] NOTE: There is no partial credit for methods that do not workproperly. Code will be tested calling all methods and comparing the final list EXAMPLE >>> dll-DoublyLinkedList () >>>dl1.addFirst (5) >>>d11.addFirst(9) >>>d11.addFirst (4) >>>d11.addFirst (3) >>>dll.head >>>d11.addLast (8) >>> d11.addLast (12) Instructions: In class, we discussed the Doubly Linked List Abstract Data Type (section 1.1.4 of your Lecture Notes). A Doubly Linked List contains an extra pointer (known as previous pointer) allowing the traversal of the list in both forward and backward direction. When implementing the LinkedList data structure, we observed the importance of updating the pointers in order to get access to the entire list. Implement the data structure DoublyLinkedList with the following characteristics: . DoublyLinkedList) creates a new doubly linked list that is empty. It needs no parameters e addFirt(item) adds a new Node with value-item at the beginning of the list. It needs the addLast(item) adds a new Node with value-item at the end of the list. It needs the item and and returns nothing. Assume the items in the list are unique tem and returns nothing. 20 pts returns nothing. [20 pts] * addBefore(pnode value, item) adds a new Node with value-item before the Node with value=pnode value. It needs the value of the reference Node and the item to be added returns nothing. You can assume the reference node is in the list. [30 pts] e addAfter(pnode value, item) adds a new Node with value-item after the Node with value=pnode-value. It needs the value of the reference Node and the item to be added returns nothing. You can assume the reference node is in the list. [30 pts] NOTE: There is no partial credit for methods that do not workproperly. Code will be tested calling all methods and comparing the final list EXAMPLE >>> dll-DoublyLinkedList () >>>dl1.addFirst (5) >>>d11.addFirst(9) >>>d11.addFirst (4) >>>d11.addFirst (3) >>>dll.head >>>d11.addLast (8) >>> d11.addLast (12)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions

Question

What is human nature?

Answered: 1 week ago