Home »
C/C++ Data Structure Programs
C program to swap two nodes in a circular linked list
Here, we are going to learn how to interchange the two adjacent nodes in a given circular linked list using C program?
By Piyas Mukherjee Last updated : August 02, 2023
Input
A singly linked list whose address of the first node is stored in a pointer say head and the node we have to exchange with its next node.
Output
The singly linked list with interchanged node
Data Structure Used
Circular linked list where each node contains a data element say data, and the address of the immediate next node say next, with Head holding the address of the first node and the next part of the last node contains head.
Pseudo Code
//assumed all node has different key, so searching node by key value
temp=head;
while(temp->data!=key)
begin
temp1=temp
temp=temp->next
End while
//node found which is to be interchanged
IF(temp=head)
temp1=temp->next
temp->next=temp1->next
temp1->next=temp
head=temp1
ELSE
temp2=temp->next
temp->next=temp2->next
temp2->next=temp
temp1->next=temp2
End IF-ELSE
End
C program to swap two nodes in a circular linked list
#include <stdio.h>
#include <stdlib.h>
typedef struct list {
int data;
struct list* next;
} node;
void display(node* temp)
{
node* temp1 = temp;
printf("\nNow the list is :\n%d->", temp->data);
temp = temp->next;
while (temp != temp1) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("%d\n", temp1->data);
}
// Main code
int main()
{
node *head = NULL, *temp, *temp1, *temp2;
int choice, key;
do {
temp = (node*)malloc(sizeof(node));
if (temp != NULL) {
printf("\nEnter the element in the list : ");
scanf("%d", &temp->data);
if (head == NULL) {
head = temp;
}
else {
temp1 = head;
while (temp1->next != head) {
temp1 = temp1->next;
}
temp1->next = temp;
}
temp->next = head;
}
else {
printf("\nMemory not avilable...node allocation is not possible");
}
printf("\nIf you wish to add m ore data on the list enter 1 : ");
scanf("%d", &choice);
} while (choice == 1);
display(head);
printf("\nEnter the data of the node which you want to exchange with it's next : ");
scanf("%d", &key);
temp = head;
while (temp->data != key) {
temp1 = temp;
temp = temp->next;
}
if (temp == head) {
temp1 = temp->next;
temp->next = temp1->next;
temp1->next = temp;
head = temp1;
}
else {
temp2 = temp->next;
temp->next = temp2->next;
temp2->next = temp;
temp1->next = temp2;
}
display(head);
return 0;
}
Output
Enter the element in the list : 1
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 2
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 3
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 4
If you wish to add m ore data on the list enter 1 : 1
Enter the element in the list : 5
If you wish to add m ore data on the list enter 1 : 0
Now the list is :
1->2->3->4->5->1
Enter the data of the node which you want to exchange with it's next : 3
Now the list is :
1->2->4->3->5->1