i want to be able to insert 8 in between 34 and 1 (being nodes n1 and n2) displa
ID: 3756194 • Letter: I
Question
i want to be able to insert 8 in between 34 and 1 (being nodes n1 and n2) display the updated list and delete 7 from the nodes and display the updated list.
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'''
self.item=item
self.leftL=leftL
self.rightL=rightL
# 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 = " ")
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)
n4= ListNode2(7,n5)
n3 = ListNode2(23,n4)
n2= ListNode2(1,n3)
n1 = ListNode2(34,n2)
# printing all the nodes, one by one, following right links, then left links
printLR(n1)
printLR(n2)
printLR(n3)
printLR(n4)
printLR(n5)
printRL(n1)
##printRL(n2)
##printRL(n3)
##printRL(n4)
##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)
printLR(n2)
printLR(n3)
printLR(n4)
printLR(n5)
printRL(n1)
##printRL(n2)
##printRL(n3)
##printRL(n4)
##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)
printLR(n2)
printLR(n3)
printLR(n4)
printLR(n5)
printRL(n1)
##printRL(n2)
##printRL(n3)
##printRL(n4)
##printRL(n5)
Explanation / Answer
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'''
self.item=item
self.leftL=leftL
self.rightL=rightL
# 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 = " ")
node = node.rightL
print("end of linked list")
def insertMiddle(n,leftNode,rightNode):
print("Insertion In Middle is success")
leftNode.rightL = n
rightNode.leftL = n
def deleteNode(n,headNode):
node = headNode
pre = None
while node is not None:
#start of while
if(node==n):
#start of if
break
#end of if
pre = node
node = node.leftL
#end of while
if(pre==None):
#start of if
headNode = headNode.leftL
#end of if
else:
#start of else
pre.leftL = n.leftL
#end of else
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)
n4= ListNode2(7,n5)
n3 = ListNode2(23,n4)
n2= ListNode2(1,n3)
n1 = ListNode2(34,n2)
# printing all the nodes, one by one, following right links, then left links
printLR(n1)
printLR(n2)
printLR(n3)
printLR(n4)
printLR(n5)
printRL(n1)
##printRL(n2)
##printRL(n3)
##printRL(n4)
##printRL(n5)
# Then insert new value, say 8 between 34 and 1. Display the updated list
print("Inserting 8...")
n6 = ListNode2(8,n2,n1)
insertMiddle(n6,n2,n1)
printLR(n1)
printLR(n2)
printLR(n3)
printLR(n4)
printLR(n5)
printRL(n1)
##printRL(n2)
##printRL(n3)
##printRL(n4)
##printRL(n5)
### Then delete node with value 7, update the links and display the new list.
print("Deleting 7...")
deleteNode(n4,n1)
printRL(n1)
printLR(n1)
printLR(n2)
printLR(n3)
printLR(n4)
printLR(n5)
printRL(n1)
##printRL(n2)
##printRL(n3)
##printRL(n4)
##printRL(n5)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.