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

Spread the post

How to find nth element from the end of linked list using Stack data structure?

In this post We are discussing about how to find nth last node of single linked list. Let given an example

Example :Suppose following data are exist in linked list [144, 325, 343, 44, 23, 36, 66, 125, 343, 612].

View and Edit Linked list Click Here.

root_ptr 61234312566362344343325144

Accepted Result:

Find [3]-nth Last Node Is : 125
Find [6]-nth Last Node Is : 23 
Find [11]-nth Node Not Exist 
Find [2]-nth Node Node Is : 343

Code Executrion Process: Below in show. And more execution process Click Here

Stack Areamainroot(pointer)stack_root(pointer)find_nth_nodecounter (int) =5find_node (int) =11temp(pointer) Heap Areastruct Nodedata (int)= 144next (pointer) struct Nodedata (int)= 325next (pointer) struct Nodedata (int)= 343next (pointer) struct Nodedata (int)= 44next (pointer) struct Nodedata (int)= 23next (pointer) struct Nodedata (int)= 36next (pointer) struct Nodedata (int)= 66next (pointer) struct Nodedata (int)= 125next (pointer) struct Nodedata (int)= 343next (pointer) struct Nodedata (int)= 612next (pointer)= NULLstruct StacklnkedNode (pointer) next (pointer)= NULLstruct StacklnkedNode (pointer) next (pointer) struct StacklnkedNode (pointer) next (pointer) struct StacklnkedNode (pointer) next (pointer) struct StacklnkedNode (pointer) next (pointer) struct StacklnkedNode (pointer) next (pointer) struct StacklnkedNode (pointer) next (pointer) struct StacklnkedNode (pointer) next (pointer) struct StacklnkedNode (pointer) next (pointer) struct StacklnkedNode (pointer) next (pointer)

Code implementation: Below in C programn and Time complexity of this problem O(n).

Output
 Case 1: Empty Linked list 
 	 Output Empty linked List

 case 2: When linked list are not empty 
 	 Output Linked list data is : 144  325  343  44  23  36  66  125  343  612  
 Find [3]-nth Last Node Is : 125
 Find [6]-nth Last Node Is : 23
 Find [11]-nth Node Not Exist
	 Free linked list Node element  

Spread the post

Recommended Posts: