Python program to create a doubly linked list of n nodes and count the number of nodes.

Created with Sketch.

Python program to create a doubly linked list of n nodes and count the number of nodes.

In this program, we will create a doubly linked list and count the number of nodes present in the list. To count the node, we traverse through the list by incrementing the counter by 1.

Python program to create a doubly linked list of n nodes and count the number of nodes

What count of nodes presents above doubly linked list is 5.

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. countNodes() will count the number of nodes present in the list.

  • Define a variable counter and new node current which will point to the head node.
  • Traverse through the list to count the nodes by making the current node to point to next node in the list till current point to null.
  • Increment the counter by 1.

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 CountList:
  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.     #countNodes() will count the nodes present in the list  
  34.     def countNodes(self):
  35.         counter = 0;
  36.         #Node current will point to head  
  37.         current = self.head;
  38.         while(current != None):
  39.             #Increment the counter by 1 for each node  
  40.             counter = counter + 1;
  41.             current = current.next;
  42.         return counter;
  43.     #display() will print out the nodes of the list  
  44.     def display(self):
  45.         #Node current will point to head  
  46.         current = self.head;
  47.         if(self.head == None):
  48.             print(“List is empty”);
  49.             return;
  50.         print(“Nodes of doubly linked list: “);
  51.         while(current != None):
  52.             #Prints each node by incrementing pointer.  
  53.             print(current.data),
  54.             current = current.next;
  55. dList = CountList();
  56. #Add nodes to the list  
  57. dList.addNode(1);
  58. dList.addNode(2);
  59. dList.addNode(3);
  60. dList.addNode(4);
  61. dList.addNode(5);
  62. #Displays the nodes present in the list  
  63. dList.display();
  64. #Counts the nodes present in the given list  
  65. print(“\nCount of nodes present in the list: “ + str(dList.countNodes()));

Output:

Nodes of doubly linked list: 
1 2 3 4 5 
Count of nodes present in the list: 5

Leave a Reply

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