Question
PYTHON 3 PLEASE COMPLETE CODE #COMMENTS APPRECIATED Problem Given the singly LinkedList and Node implementation we saw in class, modify the code for each to
PYTHON 3
PLEASE COMPLETE CODE
#COMMENTS APPRECIATED
Problem
Given the singly LinkedList and Node implementation we saw in class, modify the code for each to be a Doubly LinkedList.
Remember, a doubly linked list is made of nodes that have pointers to the next element as well as the previous element.
Then complete the function printInReverse() to print the list in reverse order.
CODE FROM PIC:
class Node: def __init__(self, value): self.value = value self.next = None
class LinkedList: def __init__(self): self.first = None self.last = None def printInReverse(self): #TODO def append(self, value): # you will have to modify this function to support the # "doubly" feature of the list new_node = Node(value) if self.last is not None: self.last.next = new_node
self.last = new_node if self.first is None: self.first = new_node my_list = LinkedList() my_list.append("a") my_list.append("b") my_list.append("c") my_list.printInReverse() # should print b c a
class Node: Problem 2 def init (self, value): 4 6 class LinkedList: self.value value self.next = None Given the singly LinkedList and Node implementation we saw in class, modify the code for each to be a Doubly LinkedList 7 def _init (self): Remember, a doubly linked list is made of nodes that have pointers to the next element as well as the previous element. self.first None self.last None Then complete the function printInReverse() to print the list in reverse order 10 11 def printInReverse(self): 12 13 14 def append(self, value): 15 16 17 18 19 20 21 # you will have to modify this function to support the # "doubly" feature of the list new-node = Node(value) if self.last is not None: self. Last.next = new.node self.last new_node if self.first is None: self.first - new_node 23 24 25 my-list= LinkedList() 26 my list.append("a) 27 my_list.append("b) 28 my list.append("c) 29 my-list.printinReverse() # should print b c aStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started