Question
Programming assignment: Let's think about doubly-linked lists . Define a class ListNode2 , with three attributes: item , leftL , and rightL . Left link
Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item, leftL, and rightL. Left link points to the previous node in the list, right link points to the next node in the list. You can also add the display method to this class (like we did it in class for the ListNode class). Then test your class. For example, create a linked list of 5 values: 34, 1, 23, 7, and 10. Display it. Then insert new value, say 8 between 34 and 1 (this time you will have to take care of links pointing to the previous and next node), display the resulting list. Then delete and element, update the links and display the new list. Use this draft: ListNode2_forStudents.py
# ListNode2.py class ListNode2: def __init__(self, item = None, leftL = None, rightL = None): '''creates a ListNode with the specified data value and two links: to the previous node and to the next node post: creates a ListNode with the specified data value and links''' # put the code here def __str__(self): ''' for printing the node ''' return str(self.item) def printLR(headNode): """ prints all elements following right links, starting with the headNode """ node = headNode while node is not None: print(node.item, end = "\t") node = node.rightL print("end of linked list") def printRL(tailNode): """ generates a list all elements following left links, starting with the tailNode """ node = tailNode listV = [] while node is not None: listV.append(node.item) node = node.leftL listV = listV[::-1] print("here is the list of elements, following left links:",listV) return listV # Testing # create a linked list of 5 values: 34, 1, 23, 7, and 10. # Displays it. # put the code here, make n1 to be 34, and n5 be 10 n5 = ListNode2(10) ... n1 = ListNode2(34,...... # printing all the nodes, one by one, following right links, then left links printLR(n1) printRL(n5) # Then insert new value, say 8 between 34 and 1. Display the updated list print("Inserting 8...") # put the code here printLR(n1) printRL(n5) # Then delete node with value 7, update the links and display the new list. print("Deleting 7...") # put the code here printLR(n1) printRL(n5)
Step 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