EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Python Lists Tutorial Python Doubly Linked List

Python Doubly Linked List

Updated April 7, 2023

Python Doubly Linked List

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 :

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:

Python Doubly Linked List

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 –

  1. Shell sort in Python
  2. Insertion sort in Python
  3. Sort string in Python
  4. Python String Contains
GOLANG Course Bundle - 6 Courses in 1
23+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
iOS DEVELOPER Course Bundle - 61 Courses in 1
147+ Hours of HD Videos
61 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests
56+ Hours of HD Videos
18 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
RED HAT LINUX Course Bundle - 5 Courses in 1
28+ Hours of HD Videos
5 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
PYTHON for Machine Learning Course Bundle - 39 Courses in 1 | 6 Mock Tests
 125+ Hour of HD Videos
39 Courses
6 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.8
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more