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


When it is required to count the number of nodes in a doubly linked list, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.

In a doubly linked list, the nodes have pointers. The current node would have a pointer to the next node as well as the previous node. The last value in the list will have ‘NULL’ value in the next pointer. It can be traversed in both the directions.

Below is a demonstration for the same −

Example

 Live Demo

class Node:
   def __init__(self, my_data):
      self.prev = None
      self.data = my_data
      self.next = None
class count_val:
   def __init__(self):
      self.head = None
      self.tail = None
   def add_data(self, my_data):
      new_node = Node(my_data)
      if(self.head == None):
         self.head = self.tail = new_node;
         self.head.previous = None;
         self.tail.next = None;
      else:
         self.tail.next = new_node;
         new_node.previous = self.tail;
         self.tail = new_node;
         self.tail.next = None;
   def count_node(self):
      my_counter = 0;
      curr = self.head;
      while(curr != None):
         my_counter = my_counter + 1;
         curr = curr.next;
      return my_counter;
   def print_it(self):
      curr = self.head
      if (self.head == None):
         print("The list is empty")
         return
      print("The nodes are :")
      while curr != None:
         print(curr.data)
         curr = curr.next
my_instance = count_val()
print("Elements are being added to the list")
my_instance.add_data(10)
my_instance.add_data(14)
my_instance.add_data(24)
my_instance.add_data(17)
my_instance.add_data(22)
my_instance.print_it()
print("The nodes in the doubly linked list are : ")
print(my_instance.count_node())

Output

Elements are being added to the list
The nodes are :
10
14
24
17
22
The nodes in the doubly linked list are :
5

Explanation

  • The ‘Node’ class is created.
  • Another class with required attributes is created.
  • A method named ‘add_data’ is defined, that is used to add data to the doubly linked list.
  • Another method named ‘count_node’ is defined, that helps in fetching the number of nodes in the doubly linked list.
  • Another method named ‘print_it’ is defined, that displays the nodes of the circular linked list.
  • An object of the ‘count_val’ class is created, and the methods are called on it to convert the doubly linked list to a ternary tree.
  • An ‘init’ method is defined, that the root, head, and tail nodes of the doubly linked list to None.
  • The ‘count_node’ method is called.
  • It iterates through the doubly linked list, and gets the number of nodes in the list.
  • This is displayed on the console using the ‘print_it’ method.

Updated on: 11-Mar-2021

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements