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()
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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F45d19586-ebc3-4f1a-8702-47e93c6193b1%2Ff36f8456-3f6f-4f14-9c4d-134276d10632%2Fsx12cxx_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)