In this lab you are asked to complete the provided code so that it: Accepts integers as input from the user and appends them to a singly linked list (until the user enters -1) Then shifts all the elements in the singly-linked list to the right, so the tail becomes the head, while the previous ** head** shifts to the position of head.next Then print the modified singly-linked-list print "Empty!" if the linked list is empty
In this lab you are asked to complete the provided code so that it:
- Accepts integers as input from the user and appends them to a singly linked list (until the user enters -1)
- Then shifts all the elements in the singly-linked list to the right, so the tail becomes the head, while the previous ** head** shifts to the position of head.next
- Then print the modified singly-linked-list
- print "Empty!" if the linked list is empty
class Node:
def __init__(self, initial_data):
self.data = initial_data
self.next = None
def __str__(self):
return str(self.data)
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 remove_after(self, current_node):
# Special case, remove head
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 printList(self):
if(self.head == None):
print('Empty!')
else:
node = self.head
while(node is not None):
print(node.data),
node = node.next
def shift(self):
#** your code goes here **
return self
if __name__ == '__main__':
#** Your code goes here **
![20.12 LAB: Singly linked list
In this lab you are asked to complete the provided code so that it:
Accepts integers as input from the user and appends them to a singly linked list (until the user enters -1)
• Then shifts all the elements in the singly-linked list to the right, so the tail becomes the head, while the previous ** head** shifts to the
position of head.next
Then print the modified singly-linked-list
o print "Empty!" if the linked list is empty
As an example the following linked list:
1->2->7->12->8
would become
8->1->2->7->12
377344.2021528.qx3zqy7
LAB
20.12.1: LAB: Singly linked list
0/4
ACTIVITY
main.py
Load default template...
1 class Node:
2
def
init_(self, initial_data):
self.data = initial data
4
self.next = None
6
def
str_(self):
7
return str(self.data)
8
9 class LinkedList:
def
init_(self):
self.head = None
self.tail = None
10
11
12
13
def append (self, new_node):
if self.head == None:
self.head = new_node
self.tail = new_node
14
15
16
17
18
else:
Run your program as often as you'd like, before submitting for grading. Below, type any needed
input values in the first box, then click Run program and observe the program's output in the
Develop mode
Submit mode](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F4ced4582-c25a-460b-84fc-e6b65d359cb8%2F8f2a7eb8-b56a-4d48-a21a-b49007024b99%2F6uwc2rm_processed.png&w=3840&q=75)
![main.py
Load default template...
1 class Node:
def _init_(self, initial_data):
self.data = initial_data
2
3
self.next = None
def _str_(self):
return str(self.data)
7
8
9 class LinkedList:
def_init_(self):
10
11
self.head = None
12
self.tail = None
13
def append (self, new_node):
if self.head == None:
self.head = new_node
self.tail = new_node
14
15
16
17
else:
self.tail.next = new_node
self.tail = new_node
18
19
20
21
22
def prepend(self, new_node):
23
if self.head == None:
self.head = new_node
self.tail = new_node
24
25
26
else:
new_node.next = self.head
self.head = new_node
27
28
29
30
def remove_after(self, current_node):
# Special case, remove head
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
31
32
33
34
35
36
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_nodel
37
38
39
40
41
42
43
44
def printlist(self):
if(self.head == None):
print ('Empty!')
else:
node = self.head
while(node is not None):
print (node.data),
45
46
47
48
49
50
51
node = node.next
52
53
def shift(self):
54
55
#** your code goes here **
56
57
return self
58
== '_main_':
#** Your code goes here **
59 if
name
60
61
62](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F4ced4582-c25a-460b-84fc-e6b65d359cb8%2F8f2a7eb8-b56a-4d48-a21a-b49007024b99%2F8lrimmw_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)