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

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning