Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to create a doubly linked list of n nodes and count the number of nodes
A doubly linked list is a data structure where each node contains three components: data, a pointer to the next node, and a pointer to the previous node. This allows traversal in both directions. To count the nodes, we traverse the list from head to tail and increment a counter.
In a doubly linked list, the first node's previous pointer is None, and the last node's next pointer is None. This bidirectional linking makes it more flexible than a singly linked list.
Node Class Structure
First, we create a Node class to represent individual nodes ?
class Node:
def __init__(self, data):
self.prev = None
self.data = data
self.next = None
Doubly Linked List Implementation
Here's a complete implementation that creates nodes and counts them ?
class Node:
def __init__(self, data):
self.prev = None
self.data = data
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def add_node(self, data):
new_node = Node(data)
if self.head is None:
self.head = self.tail = new_node
else:
self.tail.next = new_node
new_node.prev = self.tail
self.tail = new_node
def count_nodes(self):
counter = 0
current = self.head
while current is not None:
counter += 1
current = current.next
return counter
def display_list(self):
current = self.head
if self.head is None:
print("The list is empty")
return
print("The nodes are:")
while current is not None:
print(current.data)
current = current.next
# Create doubly linked list and add nodes
dll = DoublyLinkedList()
print("Adding elements to the doubly linked list")
dll.add_node(10)
dll.add_node(14)
dll.add_node(24)
dll.add_node(17)
dll.add_node(22)
dll.display_list()
print(f"Total number of nodes: {dll.count_nodes()}")
Adding elements to the doubly linked list The nodes are: 10 14 24 17 22 Total number of nodes: 5
How Node Counting Works
The count_nodes() method traverses the list from head to tail ?
def count_nodes(self):
counter = 0
current = self.head
# Traverse until we reach the end (None)
while current is not None:
counter += 1 # Increment count for each node
current = current.next # Move to next node
return counter
Alternative Counting Methods
You can also count nodes by maintaining a size attribute ?
class OptimizedDoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0 # Track size for O(1) counting
def add_node(self, data):
new_node = Node(data)
if self.head is None:
self.head = self.tail = new_node
else:
self.tail.next = new_node
new_node.prev = self.tail
self.tail = new_node
self.size += 1 # Increment size when adding
def count_nodes(self):
return self.size # O(1) time complexity
# Test the optimized version
opt_dll = OptimizedDoublyLinkedList()
opt_dll.add_node(100)
opt_dll.add_node(200)
opt_dll.add_node(300)
print(f"Node count (optimized): {opt_dll.count_nodes()}")
Node count (optimized): 3
Time Complexity Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Traversal Counting | O(n) | O(1) | Simple implementation |
| Size Attribute | O(1) | O(1) | Frequent counting operations |
Conclusion
Counting nodes in a doubly linked list can be done by traversing from head to tail. For frequent counting operations, maintaining a size attribute provides O(1) time complexity instead of O(n).
