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

Spread the post

Move first node at given position in linked list

Suppose linked list contain following nodes [ 10, 20, 30, 40, 50, 60, 70, 80 ]. Our goal is to Move first node at given position of linked list.

root_ptr8070605040302010

Try it Yourself

view pointers and nodes.

Stack Areamainposition (int) ? root(pointer) Heap Areastruct Nodedata (int)= 10next (pointer) struct Nodedata (int)= 20next (pointer) struct Nodedata (int)= 30next (pointer) struct Nodedata (int)= 40next (pointer) struct Nodedata (int)= 50next (pointer) struct Nodedata (int)= 60next (pointer) struct Nodedata (int)= 70next (pointer) struct Nodedata (int)= 80next (pointer)= NULL

Try it Yourself

Example 1: Move first node at [6].

root_ptr8070605040302010Before Move First node at position 6

After move

root_ptr8070605040302010

Visualize process.

Stack Areamainposition (int) =6root(pointer)move_first_nodehelper (int) =6move(pointer)position (int) =0root(pointer)status (int) =1temp(pointer) Heap Areastruct Nodedata (int)= 10next (pointer) struct Nodedata (int)= 20next (pointer) struct Nodedata (int)= 30next (pointer) struct Nodedata (int)= 40next (pointer) struct Nodedata (int)= 50next (pointer) struct Nodedata (int)= 60next (pointer) struct Nodedata (int)= 70next (pointer) struct Nodedata (int)= 80next (pointer)= NULL

Try it Yourself

C program to Move First node at given position. Iterative approach.


Output

Spread the post

Recommended Posts: