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()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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()

 

 

*****NOTE*******

Consider the following example to get an idea of how the output of the above program should be

Example:

Input

5 Morning Noon Afternoon Evening Night

Program's Output

Night Noon Afternoon Evening Morning 

Singly Linked List
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"
o 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:Singly Linked List 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" o 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 3 steps with 1 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education