✅ Traversing in Linked List
✅ 1. Introduction
A Linked List is a linear data structure where elements are stored in the form of nodes connected by pointers.
To access or process the elements of a linked list, we perform an operation called Traversing.
✅ 2. Definition of Traversing
Traversing in a linked list means visiting each node of the list one by one, starting from the HEAD node and continuing until the last node is reached.
📌 Traversing is required for:
- Displaying elements
- Searching an element
- Counting nodes
- Performing operations on each node
✅ 3. Concept of Traversing
Since linked lists do not support random access, nodes cannot be accessed directly using index values.
So, traversal must start from the first node (HEAD).
📌 Basic idea:
HEAD → Node1 → Node2 → Node3 → NULL
Traversal order:
Node1 → Node2 → Node3
✅ 4. Traversing in Singly Linked List
✅ Steps Involved
- Create a temporary pointer
temp - Assign
temp = HEAD - Repeat until
temp == NULL:- Process (display)
temp->data - Move
tempto next node (temp = temp->next)
- Process (display)
✅ Algorithm for Traversing Singly Linked List
Algorithm: Traverse_SLL(HEAD)
- Start
- If
HEAD == NULL, print List is empty and Stop - Set
temp = HEAD - While
temp != NULL- Print
temp->data temp = temp->next
- Print
- Stop
✅ Example
Linked List:
HEAD → 10 → 20 → 30 → NULL
Traversal Output:
10 20 30
✅ Time Complexity
✅ O(n) where n is the number of nodes.
✅ 5. Traversing in Circular Linked List
In a Circular Linked List, the last node points back to the first node, so there is no NULL pointer.
✅ Traversing Logic
- Start from
HEAD - Visit nodes until you reach
HEADagain
📌 Representation:
HEAD → 10 → 20 → 30
↑ ↓
←←←←←←←←←←←←←←←←←←←
✅ Algorithm for Traversing Circular Linked List
Algorithm: Traverse_CLL(HEAD)
- Start
- If
HEAD == NULL, print List is empty and Stop - Set
temp = HEAD - Do
- Print
temp->data temp = temp->next
Whiletemp != HEAD
- Print
- Stop
✅ Time Complexity
✅ O(n)
✅ 6. Traversing in Doubly Linked List
In a Doubly Linked List, each node contains two pointers:
prevnext
✅ Forward Traversing
Start from the first node and move using next pointer.
📌 Example:
NULL ← 10 ↔ 20 ↔ 30 → NULL
Traversal:
10 20 30
✅ Backward Traversing
Start from the last node and move using prev pointer.
📌 Backward Traversal:
30 20 10
✅ Algorithm (Forward Traversing)
Algorithm: Traverse_DLL_Forward(HEAD)
- Start
- Set
temp = HEAD - While
temp != NULL- Print
temp->data temp = temp->next
- Print
- Stop
✅ Algorithm (Backward Traversing)
- Move to last node
- Traverse using
prevpointer
✅ 7. Important Points
✅ Key points to remember:
- Traversing always starts from HEAD
- Linked lists do not allow direct access
- Temporary pointer is used for traversal
- Traversing time complexity is O(n)
✅ 8. Applications of Traversing
Traversing is used for:
- Displaying list elements
- Searching data
- Counting number of nodes
- Updating node values
✅ Conclusion
Traversing in a linked list is the process of visiting each node sequentially from the first node to the last node. Since linked lists do not support direct access, traversal is the only way to access elements, and it takes O(n) time.
