This is a python programming question Python Code: class Node: def __init__(self, initial_data): self.data = initial_data self.next = None class LinkedList: def __init__(self):
This is a python programming question
Python Code:
class Node:
def __init__(self, initial_data):
self.data = initial_data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def append(self, new_node):
if self.head == None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
def prepend(self, new_node):
if self.head == None:
self.head = new_node
self.tail = new_node
else:
new_node.next = self.head
self.head = new_node
def insert_after(self, current_node, new_node):
if self.head == None:
self.head = new_node
self.tail = new_node
elif current_node is self.tail:
self.tail.next = new_node
self.tail = new_node
else:
new_node.next = current_node.next
current_node.next = new_node
def remove_after(self, current_node):
if (current_node == None) and (self.head != None):
succeeding_node = self.head.next
self.head = succeeding_node
if succeeding_node == None: # Remove last item
self.tail = None
elif current_node.next != None:
succeeding_node = current_node.next.next
current_node.next = succeeding_node
if succeeding_node == None: # Remove tail
self.tail = current_node
def display(self):
node = self.head
if (node == None):
print('Empty!')
while node != None:
print(node.data, end=' ')
node = node.next
def remove_smallest_node(self):
#*** Your code goes here!***
# NOTE: this function will print out the information about the smallest node
# before removing it
def Generate_List_of_LinkedLists(n):
LLL = []
#*** Your code goes here!***
# NOTE: this function will read the input from the user for each of the lists and
# generate the list
return LLL
if __name__ == '__main__':
#*** Your code goes here!***
****Note****
This is how the output should be, given example input.
Example -
For Input
2 1 3 5 7 -1 3 4 7 2 9 -1 1
Expected Output
linked list [ 0 ]:
1 3 5 7
linked list [1]:
3 4 7 2 9
the smallest node is:
2
List 1 after removing smallest node:
3 4 7 9
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 3 images
I'm getting the errors in this image for the below code:
class Node:
def __init__(self, initial_data):
self.data = initial_data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def append(self, new_node):
if self.head == None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node
def prepend(self, new_node):
if self.head is None:
self.head = new_node
self.tail = new_node
else:
new_node.next = self.head
self.head = new_node
def insert_after(self, current_node, new_node):
if self.head is None:
self.head = new_node
self.tail = new_node
elif current_node is self.tail:
self.tail.next = new_node
self.tail = new_node
else:
new_node.next = current_node.next
current_node.next = new_node
def remove_after(self, current_node):
if (current_node is None) and (self.head is not None):
succeeding_node = self.head.next
self.head = succeeding_node
if succeeding_node is None: # Remove last item
self.tail = None
elif current_node.next is not None:
succeeding_node = current_node.next.next
current_node.next = succeeding_node
if succeeding_node is None: # Remove tail
self.tail = current_node
def display(self):
node = self.head
if node == None:
print('Empty!')
while node is not None:
print(node.data, end=' ')
node = node.next
def remove_smallest_node(self):
# *** Your code goes here!***
temp_prev = None
temp = self.head
smallest = self.head
prev = None
while temp is not None:
if temp.data < smallest.data:
smallest = temp
prev = temp_prev
temp_prev = temp
temp = temp.next
if smallest == self.head and smallest == self.tail:
self.head = self.tail = None
elif smallest == self.head:
self.head = self.head.next
elif smallest == self.tail:
self.tail = prev
prev.next = None
else:
prev.next = smallest.next
return smallest.data
def Generate_List_of_LinkedLists(n):
LLL = []
# *** Your code goes here!***
for i in range(n):
LLL.append(LinkedList())
return LLL
if __name__ == '__main__':
# *** Your code goes here!***
n = int(input())
LLL = Generate_List_of_LinkedLists(n)
for i in range(n):
x = int(input())
while x != -1:
node = Node(x)
LLL[i].append(node)
x = int(input())
for i in range(n):
print("\nlinked list [", i, "]:")
LLL[i].display()
print("")
x = int(input())
while x >= n or LLL[x].head is None:
if x >= n:
x = int(input("Wrong input! try again:"))
else:
x = int(input("Wrong input! try again:"))
smallest = LLL[x].remove_smallest_node()
print("the smallest node is:")
print(smallest)
print("List 1 after removing smallest node: ")
LLL[x].display()