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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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
Transcribed Image Text: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
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
Transcribed Image Text: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
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY