DS – Adjecent Node Swapping in Linked List

If we swap first 2 nodes in the list:

If we swap any other 2 nodes in this list:

#include<stdio.h>
#include<stdlib.h>
struct Node
{
            int data;
            struct Node* link;
};
struct Node* root=NULL;
 
struct Node* insert(struct Node*, int);
struct Node* newNode(int);
void print_forward(struct Node*);
void print(struct Node*);
int length(void);
void swap_adj(void);
int main()
{
            int ch, ele;
            while(1)
            {
                        printf(“\n1. Insert \n”);
                        printf(“2. Print \n”);
                        printf(“3. Swap adjecent \n”);
                        printf(“4. Quit \n”);
                        printf(“Enter choice : “);
                        scanf(“%d”, &ch);
                        switch(ch)
                        {
                                    case 1  :           printf(“Enter element : “);
                                                                        scanf(“%d”, &ele);
                                                                        root = insert(root, ele);
                                                                        break ;
                                   
                                    case 2  :           print_forward(root);
                                                                        break;
                                   
                                    case 3  :           swap_adj();
                                                                        break;
                                   
                                    case 4  :           exit(1);
                                    default :           printf(“Invalid choice \n\n”);
                        }
            }          
            return 0;          
}
 
struct Node* insert(struct Node* node, int ele)
{
            if(node==NULL)
            {
                        return newNode(ele);  
            }
            else
            {
                        node->link = insert(node->link, ele);
            }
            return node;
}
 
struct Node* newNode(int data)
{
            struct Node* node;
            node = (struct Node*)malloc(sizeof(struct Node));
            node->data = data;
            node->link = NULL;
            return node;
}
 
void print_forward(struct Node* node)
{
            if(node==NULL)
            {
                        printf(“List is empty \n\n”);
            }
            else
            {
                        print(node);
            }
}
 
void print(struct Node* node)
{
            if(node == NULL)
            {
                        return;
            }
            printf(“%d \t”, node->data);
            print(node->link);
}
 
int length(void)
{
            struct Node *node = root;
            int count=0;
            while(node)
            {
                        count++;
                        node=node->link;
            }
            return count;
}
void swap_adj(void)
{
            int len, loc, i=1;
            struct Node *p, *q, *r;
            len = length();
            if(len>1)
            {
                        printf(“Enter location to swap with adjecent node : “);
                        scanf(“%d”, &loc);
                        if(loc >= len)
                        {
                                    printf(“Invalid location to swap \n”);
                                    printf(“List length : %d \n”, len);
                                    return;
                        }
                        p=root;
                        if(loc==1)
                        {
                                    q = p->link;
                                    p->link = q->link;
                                    q->link = p;
                                    root = q;
                        }
                        else
                        {
                                    while(i<loc-1)
                                    {
                                                p = p->link;
                                                i++;
                                    }
                                    q = p->link;
                                    r = q->link;
                                   
                                    q->link = r->link;
                                    r->link = q;
                                    p->link = r;
                        }
            }
            else
            {
                        printf(“Minimum 2 nodes required to swap \n”);
            }
}
Scroll to Top