Aim: Write a C Program to Reverse a Singly
Circular Linked List
Solution
#include <stdio.h>
#include <stdlib.h>
int count = 0;
struct SNode {
int data;
struct SNode* next;
};
struct SNode *headS = NULL, *tailS = NULL;
struct SNode *getSNode() {
struct SNode* newNode = (struct SNode*)malloc(sizeof(struct SNode));
int data = 0;
printf(" Enter the Data for newNode: ");
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
count++;
return newNode;
}
void createCSLL() {
int more = 1;
printf(">Creating a Circular Singly Linked List\n");
while (more) {
struct SNode* newNode = getSNode();
if (headS == NULL) {
headS = newNode;
tailS = newNode;
tailS->next = headS;
} else {
tailS->next = newNode;
tailS = newNode;
tailS->next = headS;
}
printf("> Do you want to create more nodes? (1/0): ");
scanf("%d", &more);
}
}
void reverseSCLL(){
struct SNode *prevNode=NULL, *currentNode, *nextNode;
currentNode = headS;
nextNode = headS;
while(nextNode!=NULL){
nextNode=nextNode->next;
currentNode->next=prevNode;
prevNode=currentNode;
currentNode=nextNode;
}
headS=prevNode->next;
}
void printCSLL() {
if (headS == NULL) {
printf("\nList is empty.\n");
return;
}
struct SNode* temp = headS;
printf("\nCircular Singly Linked List: ");
do {
printf("%d, ", temp->data);
temp = temp->next;
} while (temp != headS);
printf("\n");
}
int main(){
createCSLL();
printCSLL();
reverseSCLL();
printCSLL();
}
Output