write an algorithm to count total number of nodes in a linked list
Answers
Answered by
19
Answer:
Algorithm to Count Number of Node in Linked List
Step 1: Count = 0
SAVE = FIRST
Step 2: Repeat step 3 while SAVE ≠ NULL
Step 3: Count= Count + 1
SAVE=SAVE->LINK
Step 4: Return Count
Answered by
1
In an effort to find the total count of number of nodes present in any single linked list we can use iterative method as well as recursive method.
Firstly, the algorithm used for iterative method to find the total count of nodes present in the single linked list is as follows:
- Initialize count_node as 0.
- Initialize a node pointer, current_ptr = head.
- Do following while current_ptr is not NULL
- current_ptr = current_ptr -> next
- count_node++;
- return count_node;
Now, the algorithm used for recursive method to find the total count of nodes present in the single linked list is as follows:
- int find_count(head)
- If head is NULL
return 0;
2. Else
return 1 + find_count(head->next);
Similar questions