Implement a simple linked list in Python (Write source code and show output) with basic linked list operations like: (a) create a sequence of nodes and construct a linear linked list. (b) insert a new node in the linked list. (b) delete a particular node in the linked list. (c) modify the linear linked list into a circular linked list. Use this template: class Node: def __init__(self, val=None): self.val = val self.next = None class LinkedList: """ TODO: Remove the "pass" statements and implement each method Add any methods if necessary DON'T use a builtin list to keep all your nodes. """ def __init__(self): self.head = None # The head of your list, don't change its name. It should be "None" when the list is empty. def append(self, num): # append num to the tail of the list pass def insert(self, index, num): # insert num into the given index pass def delete(self, index): # remove the node at the given index and return the deleted value as an integer pass def circularize(self): # Make your list circular. This method can only be the last call of the autograder. pass if __name__ == "__main__": my_list = LinkedList() # [] my_list.insert(0, 32) # [32] my_list.append(-5) # [32, -5] my_list.append(19) # [32, -5, 19] my_list.insert(1, 6) # [32, 6, -5, 19] my_list.delete(2) # [32, 6, 19] my_list.circularize()

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

Implement a simple linked list in Python (Write source code and show output) with basic linked list operations like:

(a) create a sequence of nodes and construct a linear linked list.

(b) insert a new node in the linked list.

(b) delete a particular node in the linked list.

(c) modify the linear linked list into a circular linked list.

Use this template:

class Node:
def __init__(self, val=None):
self.val = val
self.next = None
class LinkedList:
"""
TODO: Remove the "pass" statements and implement each method
Add any methods if necessary
DON'T use a builtin list to keep all your nodes.
"""
def __init__(self):
self.head = None # The head of your list, don't change its name. It should
be "None" when the list is empty.
def append(self, num): # append num to the tail of the list
pass
def insert(self, index, num): # insert num into the given index
pass
def delete(self, index): # remove the node at the given index and return the
deleted value as an integer
pass
def circularize(self): # Make your list circular. This method can only be the
last call of the autograder.
pass
if __name__ == "__main__":
my_list = LinkedList() # []
my_list.insert(0, 32) # [32]
my_list.append(-5) # [32, -5]
my_list.append(19) # [32, -5, 19]
my_list.insert(1, 6) # [32, 6, -5, 19]
my_list.delete(2) # [32, 6, 19]
my_list.circularize()

Expert Solution
Step 1 Algorithm to implement Linked list and perform some operations

Algorithm to implement Linked list and perform some operations

  • __init__(self): Initializes a new instance of the LinkedList class with an empty head node.
    • Set the head node to None
  • append(self, num): Adds a new node with the given num to the end of the linked list.
    • Create a new node with the given num.
    • If the head node is None, set the new node as the head node and return
    • Traverse the linked list until the last node is reached 4. Set the next pointer of the last node to the new node
  • insert(self, index, num): Inserts a new node with the given num at the specified index in the linked list.
    • Create a new node with the given num
    • If the index is 0, set the new node as the head node and return
    • Traverse the linked list until the node at the (index - 1) position is reached
    • Set the next pointer of the new node to the next node of the (index - 1) node
    • Set the next pointer of the (index - 1) node to the new node
  • delete(self, index): Removes the node at the specified index from the linked list and returns its value.
    • If the index is 0, set the head node to the next node and return the value of the current head node
    • Traverse the linked list until the node at the (index - 1) position is reached 
    • Set the next pointer of the (index - 1) node to the next node of the (index) node 
    • Return the value of the (index) node
  • circularize(self): Changes the linked list into a circular linked list by setting the next pointer of the last node to the head node.
    • If the head node is None, return 
    • Traverse the linked list until the last node is reached
    • Set the next pointer of the last node to the head node
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Arrays
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
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