Python Code:   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 Generate(self,num_nodes):              # Your code goes here      def printList(self):             # Your code goes here                  def swap(self):               # Your code goes here                   if __name__ == '__main__':           LL = LinkedList()     num_nodes = int(input())     LL.Generate(num_nodes)     LL.swap()     LL.printList()

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

Python Code:

 

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 Generate(self,num_nodes):     
        # Your code goes here 


    def printList(self):     
       # Your code goes here   
    
    
    def swap(self):      
        # Your code goes here
        
        
if __name__ == '__main__':      
    LL = LinkedList()
    num_nodes = int(input())
    LL.Generate(num_nodes)
    LL.swap()
    LL.printList()

 

In this lab you are provided with skeleton code and asked to complete the following linked list functions:
• Generate(num_nodes) where num_nodes is the number of nodes which are read from the user and used to form a linked list:
o if the number is negative the function displays "number of nodes can't be negative, please try again!", and lets the user try again"
• otherwise it accepts num_nodes nodes from the user and generates the singly linked list
• printList:
o if the number of nodes is more than zero it prints a singly linked list.
o if the number n is zero, then it displays: "Empty List!"
• swap: which swaps the data in the head and the tail nodes of the singly linked list, and prints all the nodes in the list
For example, for the following input linked list
d -> b -> c -> a
the resulting linked list, after executing the swap function, would be:
a -> b -> c -> d
The format for printing the list is to print each node separated by a space e.g. for the list after executing the swap function shown above the
output is:
a b c d
Transcribed Image Text:In this lab you are provided with skeleton code and asked to complete the following linked list functions: • Generate(num_nodes) where num_nodes is the number of nodes which are read from the user and used to form a linked list: o if the number is negative the function displays "number of nodes can't be negative, please try again!", and lets the user try again" • otherwise it accepts num_nodes nodes from the user and generates the singly linked list • printList: o if the number of nodes is more than zero it prints a singly linked list. o if the number n is zero, then it displays: "Empty List!" • swap: which swaps the data in the head and the tail nodes of the singly linked list, and prints all the nodes in the list For example, for the following input linked list d -> b -> c -> a the resulting linked list, after executing the swap function, would be: a -> b -> c -> d The format for printing the list is to print each node separated by a space e.g. for the list after executing the swap function shown above the output is: a b c d
Expert Solution
steps

Step by step

Solved in 5 steps with 3 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

I get the following errors for the code above

Traceback (most recent call last):
File "main.py", line 54, in <module>
LL. Generate (num_nodes)
File "main.py", line 32, in Generate
self.append (Node (x[i]))
IndexError: list index out of range
Output differs. See highlights below. Special character legend
Input
Your output
Expected output
-1
-2
3
1
2
3
number of nodes can't be negative, please try again!
number of nodes can't be negative, please try again!
number of nodes can't be negative, please try again!
number of nodes can't be negative, please try again!
3 2 1
Output is nearly correct, but whitespace differs. See highlights below. Special character legend
Input
1
2
24
Your output
Expected output 2
N
Transcribed Image Text:Traceback (most recent call last): File "main.py", line 54, in <module> LL. Generate (num_nodes) File "main.py", line 32, in Generate self.append (Node (x[i])) IndexError: list index out of range Output differs. See highlights below. Special character legend Input Your output Expected output -1 -2 3 1 2 3 number of nodes can't be negative, please try again! number of nodes can't be negative, please try again! number of nodes can't be negative, please try again! number of nodes can't be negative, please try again! 3 2 1 Output is nearly correct, but whitespace differs. See highlights below. Special character legend Input 1 2 24 Your output Expected output 2 N
Traceback (most recent call last):
File "main.py", line 54, in <module>
LL.Generate (num_nodes)
File "main.py", line 32, in Generate
self.append (Node (x[i]))
IndexError: list index out of range
Input
5
Morning
Noon
Input
Afternoon
Evening
Night
Your output
Your program produced no output
Expected output Night Noon Afternoon Evening Morning
Traceback (most recent call last):
File "main.py", line 54, in <module>
LL.Generate (num_nodes)
File "main.py", line 32, in Generate
self.append (Node (x[i]))
IndexError: list index out of range
5
a
b
e
Your output Your program produced no output
Expected output e b c da
Traceback (most recent call last):
File "main.py", line 54, in <module>
LL. Generate (num_nodes)
File "main.py", line 29, in Generate
value1=input()
EOFError: EOF when reading a line
Input 0
Your output Your program produced no output
Expected output Empty List!
Transcribed Image Text:Traceback (most recent call last): File "main.py", line 54, in <module> LL.Generate (num_nodes) File "main.py", line 32, in Generate self.append (Node (x[i])) IndexError: list index out of range Input 5 Morning Noon Input Afternoon Evening Night Your output Your program produced no output Expected output Night Noon Afternoon Evening Morning Traceback (most recent call last): File "main.py", line 54, in <module> LL.Generate (num_nodes) File "main.py", line 32, in Generate self.append (Node (x[i])) IndexError: list index out of range 5 a b e Your output Your program produced no output Expected output e b c da Traceback (most recent call last): File "main.py", line 54, in <module> LL. Generate (num_nodes) File "main.py", line 29, in Generate value1=input() EOFError: EOF when reading a line Input 0 Your output Your program produced no output Expected output Empty List!
Solution
Bartleby Expert
SEE SOLUTION
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