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

Spread the post

Get element at specific index of linked list

Suppose given linked list contain following nodes [1,2,3,4,5,6,7,8]. Our goal is to Get given index element of linked list.

root_ptr87654321

View insertion process.

Try it Yourself

Get Element at index [4].

root_ptr8765Index [4] is [5]4321

Accepted Output


Algorithm:


Time complexity of this program O(n).

C program to Get element at index.


Output


Code execution

Stack Areamainroot(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer)= NULL

Get element at index [3].

Stack Areamainroot(pointer)index_valuecount (int) =3index (int) =3temp(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer)= NULL

Note given all execution process.

Try it Yourself

Spread the post

Recommended Posts: