Spread the post

Sort a linked list of 0s 1s and 2s

Sort a linked list of 0s 1s and 2s by changing links.

For example given linked list are contain following nodes.

root_ptr0121120102

Function: by changing links.

View pointers and nodes of Linked list.

Stack Areamainroot(pointer) Heap Areastruct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 0next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 0next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 0next (pointer)= NULL

After sort.

root_ptr0121120102 Stack Areamainroot(pointer) Heap Areastruct Nodedata (int)= 2next (pointer)= NULLstruct Nodedata (int)= 0next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 0next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 0next (pointer)

Try it Yourself

Program for sort a linked list which contain number of 0 to 2. without recursion.


Output

View process

Stack Areamainroot(pointer)sort_0s_1s_2sauxilary(pointer)one_ptr(pointer) NULLroot(pointer)temp(pointer) NULLtwo_ptr(pointer) NULLzero_ptr(pointer) Heap Areastruct Nodedata (int)= 2next (pointer)= NULLstruct Nodedata (int)= 0next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 0next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 0next (pointer)

Try it Yourself

Spread the post

Recommended Posts: