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

Spread the post

Move a given occurrences at beginning of linked list

Given a linked list and Move a given occurrences at beginning of linked list. suppose linked list contain following nodes[1, 2, 1, 4, 3, 3, 7, 1, 9].

root_ptr917334121

Try it Yourself

View pointers and nodes.

Stack Areamainoccurrences (int) =1root(pointer)show_datatemp(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 9next (pointer)= NULL

Try it Yourself

Example : Move all occurrence 1 to beginning.

root_ptr973342111

visualize execution process.

Stack Areamainoccurrences (int) =1root(pointer)move_occurrenceshold(pointer) NULLprev(pointer)root(pointer)status (int) =1temp(pointer) NULLvalue (int) =1show_datatemp(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 9next (pointer)= NULL

Try it Yourself

Accepted Output

C program to Move a given occurrence to beginning of linked list . Iterative approach.


Output

Spread the post

Recommended Posts: