In the previous post, I introduced Data structure: Singly Linked List and how to implement it in Python.
Move on, I would like to show you about Data structure: Doubly Linked List
This part is mentioned in the previous post. Don't worry about it. I will repeat it below.
A linked list is a fundamental data structure in computer science. It consists of nodes where each node contains data and a reference (link) to the next node in the sequence. This allows for dynamic memory allocation and efficient insertion and deletion operations compared to arrays.
A doubly linked list (DLL) is a special type of linked list in which each node contains a pointer to the previous node as well as the next node of the linked list.
You can clone the source code here to follow it easier.
mkdir doubly_linked_list cd doubly_linked_list vi doubly_linked_list.py
class Node: def __init__(self, data: int) -> None: self.data = data self.prev = None self.next = None
class DoublyLinkedList: def __init__(self) -> None: self.head = None
There are three types:
Insert a node at the beginning
Step to insert:
def insert_at_head(self, data: int): new_node = Node(data) new_node.prev = None new_node.next = self.head if (self.head): self.head.prev = new_node self.head = new_node
Insert a node at the end
Step to insert:
def insert_at_tail(self, data: int): if (self.head is None): return self.insert_at_head(data) node_len = self.get_len() prev_node = self.get_node_at_index(node_len - 1) new_node = Node(data) new_node.prev = prev_node new_node.next = None prev_node.next = new_node
Insert a node at a given position
Step to insert:
def insert_at_index(self, data: int, index: int): node_len = self.get_len() if (index < 0 or index > node_len): return None if (index == 0): return self.insert_at_head(data) if (index == node_len - 1): return self.insert_at_tail(data) prev_node = self.get_node_at_index(index - 1) new_node = Node(data) new_node.prev = prev_node new_node.next = prev_node.next prev_node.next = new_node
There are three types:
Delete a node at the beginning
Step to delete:
def delete_first_node(self): node_len = self.get_len() if (node_len == 0): return None if (node_len == 1): self.head = None return self.head next_node = self.head.next next_node.prev = None self.head = next_node
Delete a node at the end
Step to delete:
def delete_last_node(self): node_len = self.get_len() if (node_len == 0 or node_len == 1): return self.delete_first_node() prev_node = self.get_node_at_index(node_len - 2) prev_node.next = None
Delete a node at a given position
Step to delete:
def delete_node_at_index(self, index: int): node_len = self.get_len() if (index < 0 or index >= node_len): return None if (index == 0): return self.delete_first_node() if (index == node_len - 1): return self.delete_last_node() node_delete = self.get_node_at_index(index) node_delete.prev.next = node_delete.next node_delete.next.prev = node_delete.prev
Both the singly linked list and the doubly linked list are the data structures to store a sequence of pointers.
But they still have differences like below:
Singly-linked list:
Doubly-linked list:
In this article, I showed you how to set up doubly linked lists in Python, and the difference between a singly linked list and a doubly linked list.
Hope this article will be useful to you.
My door is always open if you have any comments.
Document references: