Python program to create and display a doubly linked list.

Created with Sketch.

Python program to create and display a doubly linked list.

In this program, we will create a doubly linked list and print all the nodes present in the list.

Doubly Linked List:

Doubly Linked List is a variation of the linked list. The linked list is a linear data structure which can be described as the collection of nodes. Nodes are connected through pointers. Each node contains two fields: data and pointer to the next field. The first node of the linked list is called the head, and the last node of the list is called the tail of the list.

One of the limitations of the singly linked list is that it can be traversed in only one direction that is forward. The doubly linked list has overcome this limitation by providing an additional pointer that points to the previous node. With the help of the previous pointer, the doubly linked list can be traversed in a backward direction thus making insertion and deletion operation easier to perform. So, a typical node in the doubly linked list consists of three fields:

  • Data represents the data value stored in the node.
  • Previous represents a pointer that points to the previous node.
  • Next represents a pointer that points to the next node in the list.

Python program to create and display a doubly linked list

The above picture represents a doubly linked list in which each node has two pointers to point to previous and next node respectively. Here, node 1 represents the head of the list. The previous pointer of the head node will always point to NULL. Next pointer of node one will point to node 2. Node 5 represents the tail of the list whose previous pointer will point to node 4, and the next will point to NULL.

ALGORITHM:

  1. Define a Node class which represents a node in the list. It will have three properties: data, previous which will point to the previous node and next which will point to the next node.
  2. Define another class for creating a doubly linked list, and it has two nodes: head and tail. Initially, head and tail will point to null.
  3. addNode() will add node to the list:
  • It first checks whether the head is null, then it will insert the node as the head.
  • Both head and tail will point to a newly added node.
  • Head’s previous pointer will point to null and tail’s next pointer will point to null.
  • If the head is not null, the new node will be inserted at the end of the list such that new node’s previous pointer will point to tail.
  • The new node will become the new tail. Tail’s next pointer will point to null.

a. display() will show all the nodes present in the list.

  • Define a new node ‘current’ that will point to the head.
  • Print current.data till current points to null.
  • Current will point to the next node in the list in each iteration.

PROGRAM:

  1. #Represent a node of doubly linked list  
  2. class Node:
  3.     def __init__(self,data):
  4.         self.data = data;
  5.         self.previous = None;
  6.         self.next = None;
  7. class DoublyLinkedList:
  8.     #Represent the head and tail of the doubly linked list  
  9.     def __init__(self):
  10.         self.head = None;
  11.         self.tail = None;
  12.     #addNode() will add a node to the list  
  13.     def addNode(self, data):
  14.         #Create a new node  
  15.         newNode = Node(data);
  16.         #If list is empty  
  17.         if(self.head == None):
  18.             #Both head and tail will point to newNode  
  19.             self.head = self.tail = newNode;
  20.             #head’s previous will point to None  
  21.             self.head.previous = None;
  22.             #tail’s next will point to None, as it is the last node of the list  
  23.             self.tail.next = None;
  24.         else:
  25.             #newNode will be added after tail such that tail’s next will point to newNode  
  26.             self.tail.next = newNode;
  27.             #newNode’s previous will point to tail  
  28.             newNode.previous = self.tail;
  29.             #newNode will become new tail  
  30.             self.tail = newNode;
  31.             #As it is last node, tail’s next will point to None  
  32.             self.tail.next = None;
  33.     #display() will print out the nodes of the list  
  34.     def display(self):
  35.         #Node current will point to head  
  36.         current = self.head;
  37.         if(self.head == None):
  38.             print(“List is empty”);
  39.             return;
  40.         print(“Nodes of doubly linked list: “);
  41.         while(current != None):
  42.             #Prints each node by incrementing pointer.  
  43.             print(current.data),;
  44.             current = current.next;
  45. dList = DoublyLinkedList();
  46. #Add nodes to the list  
  47. dList.addNode(1);
  48. dList.addNode(2);
  49. dList.addNode(3);
  50. dList.addNode(4);
  51. dList.addNode(5);
  52. #Displays the nodes present in the list  
  53. dList.display();

Output:

Nodes of doubly linked list: 
1 2 3 4 5

Leave a Reply

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