
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
![In this lab you are asked to complete the provided code so that it generates a list of singly-linked-lists (note: this will be a python list, where
each element of the list is a singly linked list). Your code should do the following:
• For the linked list in the index position supplied by the user, your code should find the node with the smallest value and delete it
• Your code should display "Empty!", if there is no nodes left in the linked list after removing the smallest node!
• Finally, it should print the modified linked list
For example, when the input is:
2
1
7
• To generate a list of singly linked lists, your code should accept the number of linked lists as an input
• Then, for each linked list, it should accept integers as nodes till the user enters -1
• It should next display all the linked lists in the lists entered by the user
• Next, it should accept a number - this is an index position for the list (a linked list)
3
7
1
The output should be:
linked list [ 0 ]:
1 3 5 7
linked list [ 1 ]:
34729
the smallest node is:
2
List 1 after removing the smallest node:
3479
Note Make sure to check for the following possible user's mistakes
• if the user inputs a negative number as the number of linked lists in the list, your code should display: "Wrong input! try again:"; then,
let the user to input another number
• Same for the Empty linked lists](https://content.bartleby.com/qna-images/question/45d19586-ebc3-4f1a-8702-47e93c6193b1/7a1161d8-312f-4638-a78d-efd6dde33588/zdl0oi_thumbnail.png)

Trending nowThis is a popular solution!
Step by stepSolved 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()
![Input
Your output
Expected output
1
#
linked list [ 0 ]:
1 3 5 7
linked list [ 1 ]:
34729
the smallest node is:
2
List 1 after removing smallest node:
3479
linked list [ 0 ]:
1 3 5 7
linked list [ 1 ]:
34729
the smallest node is:
2
List 1 after removing smallest node:
3479
error 1
Output differs. See highlights below. Special character legend
Input
Your output
Expected output
-
9
0
List 1 after removing smallest node:
234
linked list [ 0 ]:
2340
Input
Wrong input! try again
Wrong input! try again
the smallest node is:
0
List after removing smallest node:
234
Your output
A
linked list [0]:
2340
Wrong input! try again: Wrong input! try again: the smallest node is:
0
Expected output
error 4
1
0
3
4
1
5
-1
0
e
error 2
linked list [0]:
03415
the smallest node is:
0
List 1 after removing smallest node:
3 4 15
linked list [ 0 ]:
0 3 4 15
the smallest node is:
0
List 0 after removing smallest node:
3 4 15
Traceback (most recent call last):
File "main.py", line 108, in <module>
while x >= n or LLL [x].head is None:
IndexError: list index out of range
Output differs. See highlights below.
Expected output
Input
Your output
Your output
Expected output
Input
1
-1
1
-1
0
0
اله
2
3
4
1
5
0
d
Wrong input! try again: Wrong input! try again: Wrong input! try again: Wrc
Wrong input! try again:
linke
[0]:4
2 3 4154
Traceback (most recent call last):
File "main.py", line 112, in <module>
x = int (input ("Wrong input! try again:"))
EOFError: EOF when reading a line
Output differs. See highlights below. Special character legend
the
14
Special character legend
smallest node is:
List 0 after removing smallest node: <
2 3 4 5
linked list [ 0 ]:
Empty!
Wrong input! try again:
linked list [ 0 ]:
1
the smallest node is:<
14
error 3
Wrong input! try again: Wrong input! try again: Wrong input! try again:
List 0 after removing smallest node: <
Empty!
error 5](https://content.bartleby.com/qna-images/question/45d19586-ebc3-4f1a-8702-47e93c6193b1/67399859-5db2-4071-81ea-f7043fdec075/iz0sjfd_thumbnail.png)
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()
![Input
Your output
Expected output
1
#
linked list [ 0 ]:
1 3 5 7
linked list [ 1 ]:
34729
the smallest node is:
2
List 1 after removing smallest node:
3479
linked list [ 0 ]:
1 3 5 7
linked list [ 1 ]:
34729
the smallest node is:
2
List 1 after removing smallest node:
3479
error 1
Output differs. See highlights below. Special character legend
Input
Your output
Expected output
-
9
0
List 1 after removing smallest node:
234
linked list [ 0 ]:
2340
Input
Wrong input! try again
Wrong input! try again
the smallest node is:
0
List after removing smallest node:
234
Your output
A
linked list [0]:
2340
Wrong input! try again: Wrong input! try again: the smallest node is:
0
Expected output
error 4
1
0
3
4
1
5
-1
0
e
error 2
linked list [0]:
03415
the smallest node is:
0
List 1 after removing smallest node:
3 4 15
linked list [ 0 ]:
0 3 4 15
the smallest node is:
0
List 0 after removing smallest node:
3 4 15
Traceback (most recent call last):
File "main.py", line 108, in <module>
while x >= n or LLL [x].head is None:
IndexError: list index out of range
Output differs. See highlights below.
Expected output
Input
Your output
Your output
Expected output
Input
1
-1
1
-1
0
0
اله
2
3
4
1
5
0
d
Wrong input! try again: Wrong input! try again: Wrong input! try again: Wrc
Wrong input! try again:
linke
[0]:4
2 3 4154
Traceback (most recent call last):
File "main.py", line 112, in <module>
x = int (input ("Wrong input! try again:"))
EOFError: EOF when reading a line
Output differs. See highlights below. Special character legend
the
14
Special character legend
smallest node is:
List 0 after removing smallest node: <
2 3 4 5
linked list [ 0 ]:
Empty!
Wrong input! try again:
linked list [ 0 ]:
1
the smallest node is:<
14
error 3
Wrong input! try again: Wrong input! try again: Wrong input! try again:
List 0 after removing smallest node: <
Empty!
error 5](https://content.bartleby.com/qna-images/question/45d19586-ebc3-4f1a-8702-47e93c6193b1/67399859-5db2-4071-81ea-f7043fdec075/iz0sjfd_thumbnail.png)
- #include <iostream> usingnamespace std; class Queue { int size; int* queue; public: Queue(){ size = 0; queue = new int[100]; } void add(int data){ queue[size]= data; size++; } void remove(){ if(size ==0){ cout <<"Queue is empty"<<endl; return; } else{ for(int i =0; i < size -1; i++){ queue[i]= queue[i +1]; } size--; } } void print(){ if(size ==0){ cout <<"Queue is empty"<<endl; return; } for(int i =0; i < size; i++){ cout<<queue[i]<<" <- "; } cout << endl; } //your code goes here }; int main(){ Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5); Queue q3 = q1+q2; q3.print();…arrow_forwardclass BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)…arrow_forward1 Assume some Node class with info & link fields. Complete this method in class List that returns a reference to the node containing the data item in the argument find This, Assume that find this is in the list public class List { protected Node head; protected int size; fublic Public Node find (char find This)arrow_forward
- class BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)…arrow_forwardRedesign LaptopList class from previous project public class LaptopList { private class LaptopNode //inner class { public String brand; public double price; public LaptopNode next; public LaptopNode(String brand, double price) { // add your code } public String toString() { // add your code } } private LaptopNode head; // head of the linked list public LaptopList(String fname) throws IOException { File file = new File(fname); Scanner scan = new Scanner(file); head = null; while(scan.hasNextLine()) { // scan data // create LaptopNode // call addToHead and addToTail alternatively } } private void addToHead(LaptopNode node) { // add your code } private void addToTail(LaptopNode node) { // add your code } private…arrow_forwardclass Queue { private static int front, rear, capacity; private static int queue[]; Queue(int c) { front = rear = 0; capacity = c; queue = new int[capacity]; } static void queueEnqueue(int data) { if (capacity == rear) { System.out.printf("\nQueue is full\n"); return; } else { queue[rear] = data; rear++; } return; } static void queueDequeue() { if (front == rear) { System.out.printf("\nQueue is empty\n"); return; } else { for (int i = 0; i < rear - 1; i++) { queue[i] = queue[i + 1]; } if (rear < capacity) queue[rear] = 0; rear--; } return; } static void queueDisplay() { int i; if (front == rear) { System.out.printf("\nQueue is Empty\n"); return; } for (i = front; i < rear; i++) { System.out.printf(" %d <-- ", queue[i]); } return; } static void queueFront() { if (front == rear) { System.out.printf("\nQueue is Empty\n"); return; } System.out.printf("\nFront Element is: %d", queue[front]);…arrow_forward
- Help pls: Write the definitions of the following functions: getRemainingTransactionTime setCurrentCustomer getCurrentCustomerNumber getCurrentCustomerArrivalTime getCurrentCustomerWaitingTime getCurrentCustomerTransactionTime of the class serverType defined in the section Application of Queues: Simulation. serverType::serverType() { status = "free"; transactionTime = 0; } bool serverType::isFree() const { return (status == "free"); } void serverType::setBusy() { status = "busy"; } void serverType::setFree() { status = "free"; } void serverType::setTransactionTime(int t) { transactionTime = t; } void serverType::setTransactionTime() { //TODO: uncomment once getTransactionTime is defined /* int time; time = currentCustomer.getTransactionTime(); transactionTime = time; */ } void serverType::decreaseTransactionTime() { transactionTime--; }arrow_forwardclass BSTNode { int key; BSTNode left, right; public BSTNode(int item) { key = item; left = right = null; } } class BST_Tree { BSTNode root; BST_Tree() { // Constructor root = null; } boolean search(int key){ return (searchRec(root, key) != null); } public BSTNode searchRec(BSTNode root, int key) { if (root==null || root.key==key) return root; if (root.key > key) return searchRec(root.left, key); return searchRec (root.right, key); } void deleteKey(int key) { root = deleteRec(root, key); } /* A recursive function to insert a new key in BST */ BSTNode deleteRec(BSTNode root, int key) { /* Base Case: If the tree is empty */ if (root == null) return root; /* Otherwise, recur down the tree */ if (key < root.key)…arrow_forward1.Assume some Node class with info link fields . Complete this method in class List that returns a reference to the node containing the data item in the argument findThis, Assume that findThis is in the list public class list { protected Node head ; Protected int size ; Public Node find (char find this) { Node curr = head; while(curr != null) { if(curr.info == findThis) return curr; curr = curr.link; } return -1; } }arrow_forward
- Draw a UML class diagram for the following code: class Node: def __init__(self, value): self.value = value self.next = None class Stack: def __init__(self): self.top = None def isEmpty(self): return self.top is None def push(self, item): new_node = Node(item) new_node.next = self.top self.top = new_node def pop(self): if self.isEmpty(): raise Exception("Stack is empty") popped_item = self.top.value self.top = self.top.next return popped_item def popAll(self): items = [] while not self.isEmpty(): items.append(self.pop()) return items[::-1] def peek(self): if self.isEmpty(): raise Exception("Stack is empty") return self.top.value # Verificationstack = Stack()print("Is stack empty?", stack.isEmpty()) stack.push(10)stack.push(20)stack.push(30)print("Top item:", stack.peek()) print("Popped item:", stack.pop())print("Popped…arrow_forwardBelow you're given a Node class and a LinkedList class. You will implement a method for the LinkedList class named "delete48in148". That is, whenever there is a sequence of nodes with values 1, 4, 8, we delete the 4 and 8. For exCample, Before: 1 -> 4 -> 8 LAfter: 1 Before: 7 -> 1 -> 4 -> 8 -> 9 -> 4 -> 8 After: 7 -> 1 -> 9 -> 4 -> 8 Before: 7 -> 1 -> 4 -> 8 -> 4 -> 8 -> 4 -> 8 -> 9 After: 7 -> 1 -> 9 Note from the above example that, after deleting one instance of 48, there may be new instances of 148 formed. You must delete ALL of them. Requirement: Your implementation must be ITERATIVE (i.e., using a loop). You must NOT use recursion. Recursive solutions will NOT be given marks. import ... # No other import is allowedarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





