You may find a doubly-linked list implementation below. Our first class is Node which we can make a new node with a given element. Its constructor also includes previous node reference prev and next node reference next. We have another class called DoublyLinkedList which has start_node attribute in its constructor as well as the methods such as: 1. insert_to_empty_list() 2. insert_to_end() 3. insert_at_index() Hints: Make a node object for the new element. Check if the index >= 0. If index is 0, make new node as head; else, make a temp node and iterate to the node previous to the index. If the previous node is not null, adjust the prev and next references. Print a message when the previous node is null. 4. delete_at_start() 5. delete_at_end() . 6. display() the task is to implement these 3 methods: insert_at_index(), delete_at_end(), display(). hint on how to start thee code # Initialize the Node class Node: def __init__(self, data): self.item = data self.next = None self.prev = None # Class for doubly Linked List class DoublyLinkedList: def __init__(self): self.start_node = None # Insert Element to Empty list def insert_to_empty_list(self, data): if self.start_node is None: new_node = Node(data) self.start_node = new_node else: print("The list is empty") # Insert element at the end def insert_to_end(self, data): # Check if the list is empty if self.start_node is None: new_node = Node(data) self.start_node = new_node return n = self.start_node # Iterate till the next reaches NULL while n.next is not None: n = n.next new_node = Node(data) n.next = new_node new_node.prev = n # Delete the elements from the start def delete_at_start(self): if self.start_node is None: print("The Linked list is empty, no element to delete") return if self.start_node.next is None: self.start_node = None return self.start_node = self.start_node.next self.start_node.prev = None let the OUTPUT be: The list is empty Element is: 3 Element is: 10 Element is: 20 Element is: 30 Element is: 35 Element is: 38 Element is: 40 Element is: 50 Element is: 60 Element is: 10 Element is: 20 Element is: 30 Element is: 35 Element is: 38 Element is: 40 Element is: 50
You may find a doubly-linked list implementation below. Our first class is Node which we can make a new node with a given element. Its constructor also includes previous node reference prev and next node reference next.
We have another class called DoublyLinkedList which has start_node attribute in its constructor as well as the methods such as:
1. insert_to_empty_list() 2. insert_to_end() 3. insert_at_index()
Hints: Make a node object for the new element. Check if the index >= 0.
If index is 0, make new node as head; else, make a temp node and iterate to the node previous to the index.
If the previous node is not null, adjust the prev and next references. Print a message when the previous node is null.
4. delete_at_start()
5. delete_at_end() .
6. display()
the task is to implement these 3 methods: insert_at_index(), delete_at_end(), display().
hint on how to start thee code
# Initialize the Node
class Node:
def __init__(self, data):
self.item = data
self.next = None
self.prev = None
# Class for doubly Linked List
class DoublyLinkedList:
def __init__(self):
self.start_node = None
# Insert Element to Empty list
def insert_to_empty_list(self, data):
if self.start_node is None:
new_node = Node(data)
self.start_node = new_node
else:
print("The list is empty")
# Insert element at the end
def insert_to_end(self, data):
# Check if the list is empty
if self.start_node is None:
new_node = Node(data)
self.start_node = new_node
return
n = self.start_node
# Iterate till the next reaches NULL
while n.next is not None:
n = n.next
new_node = Node(data)
n.next = new_node
new_node.prev = n
# Delete the elements from the start
def delete_at_start(self):
if self.start_node is None:
print("The Linked list is empty, no element to delete") return
if self.start_node.next is None:
self.start_node = None
return
self.start_node = self.start_node.next
self.start_node.prev = None
let the OUTPUT be:
The list is empty
Element is: 3
Element is: 10
Element is: 20
Element is: 30
Element is: 35
Element is: 38
Element is: 40
Element is: 50
Element is: 60
Element is: 10
Element is: 20
Element is: 30
Element is: 35
Element is: 38
Element is: 40
Element is: 50
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 5 images