Spread the post

Pairwise element swap of given linked list

Suppose given linked list contain following nodes.

n group of node. n=4 swap every four node group.

input: 1-->2-->3-->4-->5-->6-->7-->8-->9-->10

Output: 4-->3-->2-->1-->8-->7-->6-->5-->9-->10

Other example:

n group of node. n=3 swap every three node group.

Input: 1-->2-->3-->4-->5-->6-->7-->8-->9-->10

Output: 3-->2-->1-->6-->5-->4-->9-->8-->7-->10

Given example:

Paorwise swap nodes

View insertion process.

Try it yourself

Suppose swap group of three node.

root_ptr10 98765432 1

Code execution:

Stack Areamainroot(pointer)swap_nodecurrent(pointer)help(pointer) NULLlast_node(pointer)root(pointer)start(pointer)status (int) =1swap_number (int) =2temp(pointer)vaildator_node(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) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 10next (pointer)= NULL

Note that not given all step of execution process here.View How to insert linked list element and so on.

Try it yourself

C program to swap every group of n nodes.


Spread the post