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

## 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.

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.
• 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:
9.     def __init__(self):
11.         self.tail = None;
14.         #Create a new node
15.         newNode = Node(data);
16.         #If list is empty
18.             #Both head and tail will point to newNode
19.             self.head = self.tail = newNode;
20.             #head’s previous will point to 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
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
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
```Nodes of doubly linked list: