C Program to Swap Two Elements of the List without Key Field

This C Program interchanges the two elements of the list without touching the key field. The nodes are exchanged in the address space.

Here is a source code of the C program to interchange the two elements of the list without touching the key field. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Interchange two Elements of the List without
  3.  * touching the Key Field  
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct node
  9. {
  10.     int num;
  11.     struct node *next;
  12. };
  13.  
  14. void create(struct node **);
  15. void release(struct node **);
  16. void change(struct node **, int, int);
  17. void display(struct node *);
  18.  
  19. int main()
  20. {
  21.     struct node *p = NULL;
  22.     int num1, num2;
  23.  
  24.     printf("Enter data into the list\n");
  25.     create(&p);
  26.     printf("Circular list generated\n");
  27.     display(p);
  28.     printf("Enter node position: ");
  29.     scanf("%d", &num1);
  30.     printf("Enter node position to exchange with: ");
  31.     scanf("%d", &num2);
  32.     change(&p, num1 - 2, num2 - 2);
  33.     printf("After interchanging, ");
  34.     display(p);
  35.     release (&p);
  36.  
  37.     return 0;
  38. }
  39.  
  40. void change(struct node **head, int num1, int num2)
  41. {
  42.     struct node *p1, *q1, *r1;
  43.     struct node *p2, *q2, *r2;
  44.  
  45.     p1 = q1 = r1 = *head;
  46.     p2 = q2 = r2 = *head;
  47.     if (num1 == num2)
  48.     {
  49.         return;
  50.     }
  51.     else if ((p1->next == NULL && num1 > 0) || (p1->next->next == NULL && num1 > 1))
  52.     {
  53.         printf("List smaller than entered node position.\n");
  54.     }
  55.     else if ((p2->next == NULL && num2 > 0) || (p2->next->next == NULL && num2 > 1))
  56.     {
  57.         printf("List smaller than entered node position.\n");
  58.     }
  59.     else
  60.     {
  61.         if (num1 >=0 && num2 >= 0)
  62.         {
  63.             p1 = p1->next->next;
  64.             q1 = q1->next;
  65.             while (num1 > 0)
  66.             {
  67.                 r1 = q1;
  68.                 q1 = p1;
  69.                 p1 = p1->next;
  70.                 num1--;
  71.             }
  72.             p2 = p2->next->next;
  73.             q2 = q2->next;
  74.             while (num2 > 0)
  75.             {
  76.                 r2 = q2;
  77.                 q2 = p2;
  78.                 p2 = p2->next;
  79.                 num2--;
  80.             }
  81.             r2->next = q1;
  82.             q2->next = p1;
  83.             r1->next = q2;
  84.             q1->next = p2;
  85.         }
  86.         else if (num1 == -1)
  87.         {
  88.             p2 = p2->next->next;
  89.             q2 = q2->next;
  90.             while (num2 > 0)
  91.             {
  92.                 r2 = q2;
  93.                 q2 = p2;
  94.                 p2 = p2->next;
  95.                 num2--;
  96.             }
  97.             if (p1->next != q2)
  98.             {
  99.                 q2->next = p1->next;
  100.                 p1->next = p2;
  101.                 r2->next = p1;
  102.             }
  103.             else
  104.             {
  105.                 p1->next = q2->next;
  106.                 q2->next = p1;
  107.             }
  108.             *head = q2;
  109.         }
  110.         else if (num2 == -1)
  111.         {
  112.             p1 = p1->next->next;
  113.             q1 = q1->next;
  114.             while (num1 > 0)
  115.             {
  116.                 r1 = q1;
  117.                 q1 = p1;
  118.                 p1 = p1->next;
  119.                 num1--;
  120.             }
  121.             if (p2->next != q1)
  122.             {
  123.                 q1->next = p2->next;
  124.                 p2->next = p1;
  125.                 r1->next = p2;
  126.             }
  127.             else
  128.             {
  129.                 p2->next = q1->next;
  130.                 q1->next = p2;
  131.             }
  132.             *head = q1;
  133.         }
  134.     }
  135. }
  136.  
  137. void create(struct node **head)
  138. {
  139.     int c, ch;
  140.     struct node *temp, *rear;
  141.  
  142.     do
  143.     {
  144.         printf("Enter number: ");
  145.         scanf("%d", &c);
  146.         temp = (struct node *)malloc(sizeof(struct node));
  147.         temp->num = c;
  148.         temp->next = NULL;
  149.         if (*head == NULL)
  150.         {
  151.             *head = temp;
  152.         }
  153.         else
  154.         {
  155.             rear->next = temp;
  156.         }
  157.         rear = temp;
  158.         printf("Do you wish to continue [1/0]: ");
  159.         scanf("%d", &ch);
  160.     } while (ch != 0);
  161.     printf("\n");
  162. }
  163.  
  164. void display(struct node *head)
  165. {
  166.     struct node *temp = head;
  167.     printf("Displaying the list elements\n");
  168.     while (temp != NULL)
  169.     {
  170.         printf("%d\t", temp->num);
  171.         temp = temp->next;
  172.     }
  173.     printf("\n");
  174. }
  175.  
  176. void release(struct node **head)
  177. {
  178.     struct node *temp = *head;
  179.     *head = (*head)->next;
  180.     while ((*head) != NULL)
  181.     {
  182.         free(temp);
  183.         temp = *head;
  184.         (*head) = (*head)->next;
  185.     }
  186. }

$ gcc exchangenodelist.c 
$ ./a.out
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 0
 
Circular list generated
Displaying the list elements
1	2	3	4	5	
Enter node position: 2
Enter node position to exchange with: 4
After interchanging, Displaying the list elements
1	4	3	2	5

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.