Updated April 7, 2023
Definition of Python Doubly linked List
In python also we have doubly linked list, which allows us to traverse in both the direction because by using the single linked list we can only traverse into the forward direction, but in the case of doubly linked list, we can move in both directions like forward and backward. If we talked about a doubly-linked list in general it always contains three components inside it, in is the data, reference to the previous node and the last is the reference to the next node. But this is not in the case of a single-linked list. Also by the use of it, we can traverse and search elements in both directions. In the coming section of the tutorial, we will see how we can implement the linked data structure into python for beginners to understand it better.
Syntax:
As we already know that it is used to store the elements inside it, but we do not have any specific syntax for this we need to follow the algorithm to create it, for better understanding see the structure of the node class in linked see below;
class Node:
def __init__(self, data):
self.item = data
self.nref = None
self.pref = None
As you can see we are creating one node here, which contains the next and previous reference inside the node with actual data. In the coming section, we will see the basic insertion operation, also it is a generic class that anyone can use.
How Doubly linked list works in Python?
As now we already know that doubly linked allows us to travers in both the direction like forwarding or backward. But by the use of a singly linked list, we are only allowed to traverse into the single direction that is forward because a singly linked list does not contain the previous pointer inside it. But in the case of a doubly-linked list, we have two references inside the node which will maintain the address of the next and previous node as well. In this section we will the basic flow of the doubly linked list, with the help of one flow chart after that what are the steps needed to implement the linked list in python, let’s get started;
1) The data inside the linked list is maintained in the form of a node, and it is a data structure that is used to store the elements inside it.
2) We have used the term node, which again contains three components inside it, which are as follows:
a) data: this will represent the element that we want to store inside the linked data structure. This will act as the actual value of the element for us.
b) next reference: This will contain the address to the next node, for we can say reference of the next node. This is similar like we have in the case of a single-linked list, so with the help of this, we will be able to move in the forward direction of the linked like.
c) the previous reference: this will contain the address of the previous node, which means the reference of the previous node. Which will help us to search and travers the element in the backward direction also. This is the advantage of the doubly linked list we can say.
3) Now let’s take a look at the flow chart, which will help us to understand it better see below;
flow chart :
If you can see in the above chart, we have different nodes which in turn contain the inside variable to cerate and access the doubly linked list in Python.
Advantages: We have so many advantages of using doubly linked list in python, we have already seen the working let’ take a loser look at some of the advantages of doubly linked list in python to see below;
1) By the use of it we can travers in both the direction, that is forward ad backward. because it maintains two references inside the node.
2) By traversing to both direction insertion and searching become easy now.
The disadvantage of using a doubly-linked list: For a doubly linked list we have to maintain the extra pointer or we can say the reference to the memory which will hold the address of the previous node. So this is the one disadvantage or more work we can say in the case of doubly linked list in python.
points to remember while using doubly linked list in python see below;
- While creating it we need to have three things inside the node class, which are data, previous reference, and next reference.
- We can add, remove elements from the linked list from any end. In the below example, we are just adding the values to it nothing more.
Examples
In the below program we are trying to create the doubly linked list in python, here we have a defined function to add the elements inside the doubly linked list and trying to show them using the print method we have. This is just a basic example to show the working and implementation of a doubly-linked list in python for beginners.
Example #1
# cerate a class
class Node:
def __init__(self, actualdata):
self.actualdata = actualdata
self.nextRefrence = None
self.prevRefrence = None
class doubly_linked_list_demo:
def __init__(self):
self.head = None
def addElement(self, NewVal):
NewNode = Node(NewVal)
NewNode.nextRefrence = self.head
if self.head is not None:
self.head.prevRefrence = NewNode
self.head = NewNode
def showListElement(self, node):
while (node is not None):
print(node.actualdata),
last = node
node = node.nextRefrence
myList = doubly_linked_list_demo()
myList.addElement(10)
myList.addElement(20)
myList.addElement(30)
myList.addElement(40)
myList.addElement(50)
myList.addElement(60)
myList.showListElement(myList.head)
Output:
Conclusion
As we have seen that how the doubly linked list works in python in this tutorial, we have one advantage in the case of searching for elements as well. Also, this is very easy to use and implement just need to understand its internal working first, which can be easily handled by the developers.
Recommended Articles
This is a guide to Python Doubly Linked List. Here we discuss definition, syntax, How Doubly linked list works in Python? examples with code implementation. You may also have a look at the following articles to learn more –