
In this section, we will see how we can create a very simple doubly linked list in Python.

Implementing the Doubly Linked List with Python A few additional steps are required to be performed in order to perform insertion and deletion operations.One of the major drawbacks of the doubly linked list is that you need more memory space to store one extra reference for each node.Rather, in a doubly linked list the reference of the predecessor node can be retrieved from the node that we want to delete. Basic operations such as insertion and deletion are easier to implement in the doubly linked lists since, unlike single linked lists, we do not need to traverse to the predecessor node and store its reference.The reference to the next node helps in traversing the node in the forward direction while the references to the previous nodes allow traversal in the backward direction. Unlike a single linked list, the doubly linked list can be traversed and searched in both directions.Pros and Cons of a Doubly Linked Listįollowing are some of the pros and cons of a doubly linked list: Pros Similarly, for the last node in the doubly linked list, the reference to next node is null. For the start node of the doubly linked list, the reference to the previous node is null. In the doubly linked list, each node has three components: the value of the node, the reference to the previous node, and the reference to the next node. In single linked list each node of the list has two components, the actual value of the node and the reference to the next node in the linked list. In this article, we will start our discussion about doubly linked list, which is actually an extension of single linked list.
#DOUBLY LINKED LIST STACK JAVA SERIES#
In Part 1 and Part 2 of the series we studied single linked list in detail. If the list is empty, add the node to the list and point the head to it.This is the third article in the series of articles on implementing linked list with Python. The head will be pointed to the new node, if the list is empty. In this insertion operation, the new input node is added at the end of the doubly linked list if the list is not empty. Printf("\nList after deleting first record: ") If the list is not empty, the head pointer is shifted to the next node. If the list is empty, deletion is not possibleĤ. Check the status of the doubly linked listģ. The head is shifted to the next node and the link is removed. This deletion operation deletes the existing first nodes in the doubly linked list. Otherwise, link the address of the existing first node to the next variable of the new node, and assign null to the prev variable.įollowing are the implementations of this operation in various programming languages −

If the list is empty, make the new node as head.ĥ. Store the new data in the data variableĤ. Create a new node with three variables: prev, data, next.ģ. This new node is inserted at the beginning of the list. In this operation, we create a new node with three compartments, one containing the data, the others containing the address of its previous and next nodes in the list. Insert After − Adds an element after an item of the list.ĭelete − Deletes an element from the list using the key.ĭisplay forward − Displays the complete list in a forward manner.ĭisplay backward − Displays the complete list in a backward manner. Insert Last − Adds an element at the end of the list.ĭelete Last − Deletes an element from the end of the list. Insertion − Adds an element at the beginning of the list.ĭeletion − Deletes an element at the beginning of the list. The last link carries a link as null to mark the end of the list.įollowing are the basic operations supported by a list. Linked List − A Linked List contains the connection link to the first link called First and to the last link called Last.Īs per the above illustration, following are the important points to be considered.ĭoubly Linked List contains a link element called first and last.Įach link carries a data field(s) and a link field called next.Įach link is linked with its next link using its next link.Įach link is linked with its previous link using its previous link.


Prev − Each link of a linked list contains a link to the previous link called Prev. Next − Each link of a linked list contains a link to the next link called Next. Link − Each link of a linked list can store a data called an element. Following are the important terms to understand the concept of doubly linked list. Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List.
