Doubly_LinkedList
Doubly_LinkedList
Design and implement a Python program that defines two classes: Node and
DoublyLinkedList.
When initializing a new instance of the DoublyLinkedList class, the user should
provide a value for the first node.
The constructor should create a new instance of the Node class using the
provided value and assign it as both the head and tail of the list.
The length attribute should be initialized to 1, as the list contains one node at the
beginning.
Algorithm
class Node:
def __init__(self,value):
self.value=value
self.next=None
self.prev=None
# class DoublyLinkedList:
class DoublyLinkedList:
def __init__(self,value):
new_node=Node(value)
self.head=new_node
self.tail=new_node
self.length=1
my_doubly_linked_list = DoublyLinkedList(7)
print('Head:', my_doubly_linked_list.head.value)
print('Tail:', my_doubly_linked_list.tail.value)
print('Length:', my_doubly_linked_list.length)
DoublyLinkedLlist: Print the List
Extend the previously implemented DoublyLinkedList class by adding a
print_list method that prints all the values of the nodes in the list from head to tail.
def print_list(self):
temp = self.head
print(temp.value)
temp = temp.next
DoublyLinkedList: Append
1. Create a new instance of the Node class with the provided value.
2. If the list is empty (i.e., the head is None), set the head and tail of the list to the
newly created node.
3. If the list is not empty, perform the following steps:
a. Set the next attribute of the current tail node to the new node.
b. Set the prev attribute of the new node to the current tail node.
c. Update the tail of the list to the new node.
4. Increment the length attribute of the list by 1.
5. Return True to indicate that the operation was successful.
Algorithm for append Method
new_node=Node(value)
if self.head is None:
self.head=new_node
self.tail=new_node
else:
self.tail.next=new_node
new_node.prev=self.tail
self.tail=new_node
self.length+=1
return True
DoublyLinkedList: Pop
Further extend the DoublyLinkedList class by adding a pop method that removes the
last node from the list and returns it.
if self.length==0:
return None
temp=self.tail
self.tail=self.tail.prev
self.tail.next=None
temp.prev=None
if self.length==0:
self.head=None
self.tail=None
self.length-=1
return temp
DLL: Prepend
Implement the prepend method that inserts a new node with a given
value at the beginning of the list.
1. Create a new instance of the Node class with the provided value.
2. If the list is empty (i.e., the length is 0), set the head and tail of
the list to the newly created node.
3. If the list is not empty, perform the following steps:
a. Set the next attribute of the new node to the current head
node.
b. Set the prev attribute of the current head node to the new
node.
c. Update the head of the list to the new node.
4. Increment the length attribute of the list by 1.
5. Return True to indicate that the operation was successful.
Algorithm: Prepend Method in Doubly Linked List
Objective: Insert a new node with a given value at the beginning of the list.
Input:
Output:
Steps:
new_node=Node(value)
if self.length==0:
self.head=new_node
self.tail=new_node
else:
new_node.next=self.head
self.head.prev=new_node
self.head=new_node
self.length+=1
return True
DLL: Pop First
Implement the pop_first method that removes the first node from the list
and returns it.
Objective: Remove and return the first node from the list.
Input:
Output:
● Returns the value of the removed node if the list is not empty; returns None if the list is
empty.
Steps:
Objective: Retrieve the node at a specific index in the doubly linked list.
Input:
Output:
● Returns the node at the given index if it exists; otherwise, returns None.
Steps:
1. Call the get method with the given index to retrieve the node at
the specified index. Store the result in a temporary variable,
temp.
2. If the temp variable contains a valid node (i.e., it is not None),
update the node's value attribute with the provided value and
return True.
3. If the temp variable does not contain a valid node (i.e., it is
None), return False to indicate that the operation was
unsuccessful.
Algorithm: Set Value Method in Doubly Linked List
Objective: Update the value of the node at a specific index in the list.
Input:
Output:
Steps:
Objective: Insert a new node with a given value at a specified index in the
list.
Input:
● index: The position in the list where the new node should be
inserted.
● value: The value to be added to the new node.
Output:
Steps:
def insert(self,index,value):
return False
if index==0:
return self.prepend(value)
if index==self.length:
return self.append(value)
new_node=Node(value)
before=self.get(index-1)
after=before.next
new_node.prev=before
new_node.next=after
before.next=new_node
after.prev=new_node
self.length+=1
return True
DLL: Remove
Implement the remove method for the DoublyLinkedList class
that removes a node at a specific index in the list and returns it.
def remove(self,index):
return None
if index==0:
return self.pop_first()
if index==self.length-1:
return self.pop()
temp=self.get(index)
before=temp.prev
after=temp.next
self.length-=1
return temp