Skip to content

Python Doubly Linked List with Examples Gottumukkala Sravan Kumar Spark By {Examples}

  • by

In this article, we will discuss how to implement the Doubly Linked list using dstructure module in python. Double Linked list is a one-dimensional data structure that contains nodes. Each node has one data/element and two links. The first link points to the previous node in the linked list and the second link points to the next node in the linked list.

After going through this Python guide, you will know how to create, insert, delete, and display the data present in the Double Linked list.

Create a Doubly Linked List using DLL()

Insert elements into Double Linked List using insert()

Delete the first node from the Doubly Linked List using delete_f() method.

Delete the last node from the Doubly Linked List using delete_l() method.

Delete a particular node from the Doubly Linked List using delete() method.

Return all nodes of the Doubly Linked List using the getnodes() method.

Print all nodes of the Doubly Linked List using the print() function.

It is important to install dstructure module before importing it. Before import, make sure you have installed it by using the below command.

# Install dstructure
pip install dstructure

1. Quick Examples of Doubly Linked List

Following are quick examples of operations on a doubly linked list

# Quick Examples of Doubly Linked List

# Import
from dstructure.DLL import DLL

# Create Doubly Linkedlist.
double_ll=DLL()

# Insert element
double_ll.insert(‘China’)
double_ll.insert(‘Japan’)
double_ll.print()

# Delete first node using delete_f()
double_ll.delete_f()
double_ll.print()

# Delete last node using delete_l()
double_ll.delete_l()
double_ll.print()

# Delete specific node using delete()
double_ll.delete(‘China’)
double_ll.print()

# Return all nodes in a list
print(double_ll.getnodes())

2. Create Python Doubly Linked List using DLL()

After importing the DLL module from dstructure package in Python, Double Linked list can be created using DLL().

# Import DLL
from dstructure.DLL import DLL

# Create Doubly Linkedlist.
double_ll=DLL()

3. Insert an element into Doubly Linked List in Python

The insert() method is used to insert a single item into Double Linked list in Python. It takes an element to be inserted as a parameter and insert an element to the double linked list.

If we print the Linkedlist using the print() function, all the elements from the Linkedlist are displayed separated by “<->”. Let’s insert 3 countries (string type) one after another to the above created double linked list.

# Import DLL
from dstructure.DLL import DLL

# Create empty DLL
double_ll=DLL()

# Insert 3 elements using insert()
double_ll.insert(‘China’)
double_ll.insert(‘Japan’)
double_ll.insert(‘India’)

double_ll.print()

Yields below output.

Three countries are inserted into the doubly linked list.

4. Delete First Node from Doubly Linked List

The first node from Python doubly linked list is deleted using delete_f() method, It takes no parameter and returns True after deleting the node from the first. If the doubly linked list is empty, False is returned.

Let’s consider the above Doubly linked list and delete the first node.

from dstructure.DLL import DLL

# Delete first node using delete_f()
double_ll.delete_f()

# Output:
# True

Since the Doubly linkedlist is not empty, First node – ‘China’ is deleted by returning True.

Example 2: Let’s display the Doubly Linkedlist after deleting the first node.

# Print the doubly linked list
double_ll.print()

# Output:
# Japan India

5. Delete Last Node from Doubly Linked List

The last node from doubly linked list is deleted using delete_l() method. This returns true after deleting node from the end. If the doubly linked list is empty, False is returned. It takes no parameter.

Let’s consider the Doubly linked list with no elements and try to delete the last node.

from dstructure.DLL import DLL
double_ll=DLL()

# Delete last node using delete_l()
double_ll.delete_l()

# Output:
# False

Since the Doubly linked list is empty, False is returned.

Example 2: Let’s consider the Doubly linked list with some elements and display it after deleting the last node.

from dstructure.DLL import DLL
double_ll=DLL()

# Insert 3 elements using insert()
double_ll.insert(‘China’)
double_ll.insert(‘Japan’)
double_ll.insert(‘India’)

# Delete last node using delete_l()
double_ll.delete_l()

double_ll.print()

# Output:
# China Japan

6. Delete a particular node from Python Doubly Linked List

If you want to delete particular element from the Doubly Linkedlist, you can use delete() method. It will take the element as a parameter that you wanted to delete.

Let’s consider the Doubly linked list with 3 elements and delete ‘Japan’.

from dstructure.DLL import DLL
double_ll=DLL()

# Insert 3 elements using insert()
double_ll.insert(‘China’)
double_ll.insert(‘Japan’)
double_ll.insert(‘India’)

# Delete specific node using delete()
double_ll.delete(‘Japan’)
double_ll.print()

# Output:
# China India

We can see ‘Japan‘ is deleted from the Double linked list.

7. Get All nodes from Python Doubly Linked List

Python getnodes() method will return all the nodes from the doubly linked list. Let’s consider the Doubly linked list with 3 elements and display them.

from dstructure.DLL import DLL
double_ll=DLL()

# Insert 3 elements using insert()
double_ll.insert(‘China’)
double_ll.insert(‘Japan’)
double_ll.insert(‘India’)

# Return all nodes in a list
double_ll.getnodes()

# Output:
# [‘China’, ‘Japan’, ‘India’]

8. Conclusion

First, we have seen how to create Python Doubly Linked list using DLL() and then we used insert() method to put elements into it. First node of the linked list is deleted using delete_f() method and last node of the linked list is deleted using delete_l() method. If you want to delete particular node, you can use the delete() method. getnodes() method is used to return all the nodes from the Doubly Linked list in a list.

 In this article, we will discuss how to implement the Doubly Linked list using dstructure module in python. Double Linked list is a one-dimensional data structure that contains nodes. Each node has one data/element and two links. The first link points to the previous node in the linked list and the second link points to  Read More Python, Python Tutorial, Python Doubly Linked List 

Leave a Reply

Your email address will not be published. Required fields are marked *