DS – Swapping Node values in Single Linked List

Swapping Node values in List:

  • Reading the locations to swap (interchange) the values of nodes.
  • If the locations are not in the list, we return error message.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
            int data;
            struct node *link;
};
struct node* root = NULL ;
void create(void);
void swap(void);
void swapAdjecent(void);
void reverse(void);
void display(void);
int count=0;
void main()
{
            int ch , len ;
            while(1)
            {
                        printf(“\n/**Single linked list Operations**/\n”);
                        printf(“1.Create \n”);
                        printf(“2.Swap Adjecent Node data \n”);
                        printf(“3.Swap Node data \n”);
                        printf(“4.Display \n”);
                        printf(“5.Quit \n”);
                        printf(“Enter Choice : “);
                        scanf(“%d”, &ch);
                        switch(ch)
                        {
                                    case 1  :           create();
                                                            break;
                                    case 2  :           swapAdjecent();
                                                            break;
                                    case 3  :           swap();
                                                            break;
                                    case 4  :           display();
                                                            break;
                                    case 5  :           exit(1);
                                    default :           printf(“Invalid choice..\n\n”);
                        }
            }
}
void create()
{
            struct node *temp,*p;
            temp = root ;
            while(1)
            {
                        p=(struct node *)malloc(sizeof(struct node));
                        printf(“Enter data :”);
                        scanf(“%d”, &p->data);
                        count++;
                        if(root==NULL)
                        {
                                    root = p;
                                    p->link = NULL;
                                    temp = p;
                        }
                        else
                        {
                                    temp->link=p;
                                    temp=p;
                        }
                        printf(“Do you want to stop(y/n) : “);
                        if(getch()==’y’)
                        {
                                    break;
                        }
            }
            p->link=NULL;
}
 
void display()
{
            if(root == NULL)
            {
                        printf(“List is empty\n\n”);
            }
            else
            {
                        struct node* temp = root ;
                        while(temp != NULL)
                        {
                                    printf(“%d \n”, temp->data);
                                    temp = temp->link ;
                        }
            }
}
void swapAdjecent()
{
            int loc , len , i=1 , temp ;
            struct node *p , *q;
            printf(“Enter location to Swap with Adjecency : “);
            scanf(“%d”, &loc);
            len = count ;
            if(loc >= len)
            {
                        printf(“Invalid Swap location\n”);
                        printf(“List contains only %d nodes\n\n”, len);
            }
            else
            {
                        p = root ;
                        while(i<loc)
                        {
                                    p = p->link ;
                                    i++;
                        }
                        q = p->link ;
                        temp = p->data ;
                        p->data = q->data ;
                        q->data = temp ;       
            }
}
void swap()
{
            int loc1, loc2 , len ;
            printf(“Enter loc1 : “);
            scanf(“%d”, &loc1);     
            printf(“Enter loc2 : “);
            scanf(“%d”, &loc2);
            len = count;
            if(loc1>len || loc2>len)
            {
                        printf(“Invalid locations to swap \n”);
            }
            else
            {
                        struct node *p, *q ;
                        int i=1, j=1 , temp ;
                        p = root ;
                        q = root ;
                        while(i<loc1)
                        {
                                    p = p->link;
                                    i++;
                        }
                        while(j<loc2)
                        {
                                    q = q->link;
                                    j++;
                        }
                        temp = p->data ;
                        p->data = q->data ;
                        q->data = temp ;
            }                      
}
Scroll to Top