Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Count linked list node element

Count how many number of nodes in given linked list. suppose linked list contain following nodes.

root_ptr2166235153216323245Length of linked list is [9]

View insertion process.

Try it Yourself

This function count linked list nodes.


Time complexity of this program O(n).

Accepted Output



Output

Accepted Output

Global Variableroot(pointer) Stack Areamaincount_nodecounter (int) =9temp(pointer) Heap Areastruct Nodedata (int)= 45next (pointer) struct Nodedata (int)= 235next (pointer) struct Nodedata (int)= 63next (pointer) struct Nodedata (int)= 21next (pointer) struct Nodedata (int)= 52next (pointer) struct Nodedata (int)= 51next (pointer) struct Nodedata (int)= 23next (pointer) struct Nodedata (int)= 66next (pointer) struct Nodedata (int)= 21next (pointer)= NULL

Try it Yourself

Recursive approach to count linked list node element

Help of this program we are learning how to count linked list node element

Time complexity of this program O(n)

Below in C implementation of this problem. Recursive approach.


Output
 Case  Empty Linked list 
   Output Empty linked List

 
 linked list data is : 45  235  63  21  52  51  23  66  21  
 Total linked list node is : [ 9 ]

 Free linked list Node element  
Global Variableroot(pointer) Stack Areamaincount_nodetemp(pointer)count_nodetemp(pointer)count_nodetemp(pointer)count_nodetemp(pointer)count_nodetemp(pointer)count_nodetemp(pointer)count_nodetemp(pointer)count_nodetemp(pointer)count_nodetemp(pointer) Heap Areastruct Nodedata (int)= 45next (pointer) struct Nodedata (int)= 235next (pointer) struct Nodedata (int)= 63next (pointer) struct Nodedata (int)= 21next (pointer) struct Nodedata (int)= 52next (pointer) struct Nodedata (int)= 51next (pointer) struct Nodedata (int)= 23next (pointer) struct Nodedata (int)= 66next (pointer) struct Nodedata (int)= 21next (pointer)= NULL

Try it Yourself

Spread the post

Recommended Posts: