C program to Swap Two Nodes in a Circular Linked List

This C Program interchanges the two adjacent nodes given a circular linked list. The nodes exchanged are as per user’s
entered node position.

Here is a source code of the C program to interchange the two adjacent nodes given a circular linked list. 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 the two Adjacent Nodes given a circular
  3.  * Linked List 
  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 tocircular(struct node **);
  16. void release(struct node **);
  17. void change(struct node **, int);
  18. void display(struct node *);
  19.  
  20. int main()
  21. {
  22.     struct node *p = NULL;
  23.     int num;
  24.  
  25.     printf("Enter data into the list\n");
  26.     create(&p);
  27.     tocircular(&p);
  28.     printf("Circular list generated\n");
  29.     display(p);
  30.     printf("Enter node position to interchange with it's adjacent: ");
  31.     scanf("%d", &num);
  32.     change(&p, num - 2);
  33.     printf("After interchanging, ");
  34.     display(p);
  35.     release (&p);
  36.  
  37.     return 0;
  38. }
  39.  
  40. void tocircular(struct node **p)
  41. {
  42.     struct node *rear;
  43.  
  44.     rear = *p;
  45.     while (rear->next != NULL)
  46.     {
  47.         rear = rear->next;
  48.     }
  49.     rear->next = *p;
  50.     /*After this the singly linked list is now circular*/
  51. }
  52.  
  53. void change(struct node **head, int num)
  54. {
  55.     struct node *p, *q, *r;
  56.  
  57.     p = q = r = *head;
  58.     p = p->next->next;
  59.     q = q->next;
  60.     while (num != 0)
  61.     {
  62.         r = q;
  63.         q = p;
  64.         p = p->next;
  65.         num--;
  66.     }
  67.     r->next = p;
  68.     q->next = p->next;
  69.     p->next = q;   
  70. }
  71.  
  72. void create(struct node **head)
  73. {
  74.     int c, ch;
  75.     struct node *temp, *rear;
  76.  
  77.     do
  78.     {
  79.         printf("Enter number: ");
  80.         scanf("%d", &c);
  81.         temp = (struct node *)malloc(sizeof(struct node));
  82.         temp->num = c;
  83.         temp->next = NULL;
  84.         if (*head == NULL)
  85.         {
  86.             *head = temp;
  87.         }
  88.         else
  89.         {
  90.             rear->next = temp;
  91.         }
  92.         rear = temp;
  93.         printf("Do you wish to continue [1/0]: ");
  94.         scanf("%d", &ch);
  95.     } while (ch != 0);
  96.     printf("\n");
  97. }
  98.  
  99. void display(struct node *head)
  100. {
  101.     struct node *temp = head;
  102.     printf("Displaying the list elements\n");
  103.     printf("%d\t", temp->num);
  104.     temp = temp->next;
  105.     while (head != temp)
  106.     {
  107.         printf("%d\t", temp->num);
  108.         temp = temp->next;
  109.     }
  110.     printf("\n");
  111. }
  112.  
  113. void release(struct node **head)
  114. {
  115.     struct node *temp = *head;
  116.     temp = temp->next;
  117.     (*head)->next = NULL;
  118.     (*head) = temp->next;
  119.     while ((*head) != NULL)
  120.     {
  121.         free(temp);
  122.         temp = *head;
  123.         (*head) = (*head)->next;
  124.     }
  125. }

$ gcc interchangenode.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 to interchange with it's adjacent: 2
After interchanging, Displaying the list elements
1	3	2	4	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.