0% found this document useful (0 votes)
6 views2 pages

Reverse Circular Singly Linked List C Program

The document contains a C program that implements a Circular Singly Linked List (CSLL) with functionalities to create, print, and reverse the list. It defines a structure for the nodes, provides functions to manage the list, and includes a main function to execute the operations. The program prompts the user for input to create nodes and displays the list before and after reversal.

Uploaded by

roboticsir07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Reverse Circular Singly Linked List C Program

The document contains a C program that implements a Circular Singly Linked List (CSLL) with functionalities to create, print, and reverse the list. It defines a structure for the nodes, provides functions to manage the list, and includes a main function to execute the operations. The program prompts the user for input to create nodes and displays the list before and after reversal.

Uploaded by

roboticsir07
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like