#include <stdio.
h>
#include <conio.h>
#include <stdlib.h>
/* ==== Node Structure ==== */
struct node {
int data;
struct node *next;
};
/* ==== Global pointers ==== */
struct node *head = NULL, *tail = NULL, *temp = NULL, *newNode = NULL;
/* ==== Function Prototypes ==== */
void create();
void insert_begin();
void insert_end();
void insert_position();
void delete_begin();
void delete_end();
void delete_position();
void display();
void search();
void main() {
int choice;
clrscr();
while (1) {
clrscr();
printf("\n===== CIRCULAR SINGLY LINKED LIST MENU =====\n");
printf("1. Create (Add node at end)\n");
printf("2. Insert at Beginning\n");
printf("3. Insert at End\n");
printf("4. Insert at Specific Position\n");
printf("5. Delete at Beginning\n");
printf("6. Delete at End\n");
printf("7. Delete at Specific Position\n");
printf("8. Display List\n");
printf("9. Search Element\n");
printf("10. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: create(); break;
case 2: insert_begin(); break;
case 3: insert_end(); break;
case 4: insert_position(); break;
case 5: delete_begin(); break;
case 6: delete_end(); break;
case 7: delete_position(); break;
case 8: display(); break;
case 9: search(); break;
case 10: printf("\nExiting...\n"); getch(); exit(0);
default: printf("\nInvalid choice! Try again.\n");
printf("\n\nPress any key to continue...");
getch();
/* ==== Create node at end (same as insert_end) ==== */
void create() {
int value;
printf("\nEnter value to insert: ");
scanf("%d", &value);
newNode = (struct node *)malloc(sizeof(struct node));
if (!newNode) {
printf("\nMemory allocation failed!");
return;
newNode->data = value;
if (head == NULL) {
head = tail = newNode;
tail->next = head; /* circular link */
} else {
tail->next = newNode;
tail = newNode;
tail->next = head; /* maintain circular */
printf("\nNode %d inserted successfully!\n", value);
/* ==== Insert at beginning ==== */
void insert_begin() {
int value;
printf("\nEnter value to insert at beginning: ");
scanf("%d", &value);
newNode = (struct node *)malloc(sizeof(struct node));
if (!newNode) {
printf("\nMemory allocation failed!");
return;
newNode->data = value;
if (head == NULL) {
head = tail = newNode;
tail->next = head;
} else {
newNode->next = head;
head = newNode;
tail->next = head; /* maintain circular */
}
printf("\nNode %d inserted at beginning.\n", value);
/* ==== Insert at end ==== */
void insert_end() {
create(); /* same as create */
/* ==== Insert at specific position ==== */
void insert_position() {
int value, pos, i;
printf("\nEnter position to insert: ");
scanf("%d", &pos);
printf("Enter value to insert at position %d: ", pos);
scanf("%d", &value);
if (pos <= 1) { /* insert at start */
insert_begin();
return;
newNode = (struct node *)malloc(sizeof(struct node));
if (!newNode) {
printf("\nMemory allocation failed!");
return;
}
newNode->data = value;
temp = head;
for (i = 1; i < pos - 1 && temp->next != head; i++) {
temp = temp->next;
if (temp == tail) { /* insert at end */
insert_end();
return;
newNode->next = temp->next;
temp->next = newNode;
printf("\nNode %d inserted at position %d.\n", value, pos);
/* ==== Delete from beginning ==== */
void delete_begin() {
if (head == NULL) {
printf("\nList is empty!");
return;
if (head == tail) { /* only one node */
printf("\nNode %d deleted from beginning\n", head->data);
free(head);
head = tail = NULL;
return;
temp = head;
head = head->next;
tail->next = head; /* maintain circular */
printf("\nNode %d deleted from beginning\n", temp->data);
free(temp);
/* ==== Delete from end ==== */
void delete_end() {
struct node *prevNode;
if (head == NULL) {
printf("\nList is empty!");
return;
if (head == tail) { /* only one node */
printf("\nNode %d deleted from end\n", head->data);
free(head);
head = tail = NULL;
return;
temp = head;
while (temp->next != tail) {
temp = temp->next;
prevNode = temp; /* prev of tail */
temp = tail;
tail = prevNode;
tail->next = head;
printf("\nNode %d deleted from end\n", temp->data);
free(temp);
/* ==== Delete at specific position ==== */
void delete_position() {
int pos, i;
struct node *delNode;
if (head == NULL) {
printf("\nList is empty!");
return;
printf("\nEnter position to delete: ");
scanf("%d", &pos);
if (pos == 1) {
delete_begin();
return;
temp = head;
for (i = 1; i < pos - 1 && temp->next != head; i++) {
temp = temp->next;
if (temp->next == head) { /* position out of range */
printf("\nPosition out of range!");
return;
delNode = temp->next;
if (delNode == tail) { /* delete last */
delete_end();
return;
temp->next = delNode->next;
printf("\nNode %d deleted at position %d\n", delNode->data, pos);
free(delNode);
}
/* ==== Display list ==== */
void display() {
if (head == NULL) {
printf("\nList is empty!\n");
return;
printf("\nCircular List: ");
temp = head;
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(head)\n");
/* ==== Search element ==== */
void search() {
int key, pos = 1, found = 0;
if (head == NULL) {
printf("\nList is empty!");
return;
}
printf("\nEnter element to search: ");
scanf("%d", &key);
temp = head;
do {
if (temp->data == key) {
found = 1;
printf("\nElement %d found at position %d", key, pos);
break;
temp = temp->next;
pos++;
} while (temp != head);
if (!found)
printf("\nElement %d not found in the list", key);