CIRCULAR_SLL
CIRCULAR_SLL
h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *first=NULL;
void insert_beg();
void insert_end();
void delete_begin();
void delete_end();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 7)
{
printf("\n*********Main Menu*********\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\
n4.Delete from last\n5.Search for an element\n6.Display\n7.Exit\n");
printf("\nEnter your choice?\n");
scanf("%d",&choice);
switch(choice)
{
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: delete_begin();
break;
case 4: delete_end();
break;
case 5: search();
break;
case 6: display();
break;
case 7: exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insert_beg()
{
struct node *new_node,*temp;
int item;
new_node = (struct node *)malloc(sizeof(struct node));
if(new_node == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item);
new_node->data = item;
if(first == NULL)
{
first = new_node;
new_node->next =first;
}
else
{
temp =first;
while(temp->next!=first)
{
temp = temp->next;
}
temp->next=new_node;
new_node->next =first;
first=new_node;
}
printf("\nnode inserted\n");
}
}
void insert_end()
{
struct node *new_node,*temp;
int item;
new_node=(struct node *)malloc(sizeof(struct node));
if(new_node== NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
new_node->data = item;
if(first == NULL)
{
first = new_node;
new_node->next =first;
}
else
{
temp =first;
while(temp->next!=first)
{
temp = temp->next;
}
temp->next = new_node;
new_node->next =first;
}
printf("\nnode inserted\n");
}
void delete_begin()
{
struct node *ptr;
if(first == NULL)
{
printf("\nList is empty");
}
else if(first->next ==first)
{
printf("\ndeleted node is %d\n",first->data);
first = NULL;
free(first);
else
{
ptr =first;
while(ptr->next!=first)
{
ptr=ptr->next;
}
ptr->next =first->next;
printf("\ndeleted node is %d\n",first->data);
free(first);
first = ptr->next;
}
}
void delete_end()
{
struct node *ptr, *prev;
if(first==NULL)
{
printf("\nList is empty");
}
else if (first ->next ==first)
{
printf("\ndeleted node is %d\n",first->data);
first = NULL;
free(first);
}
else
{
ptr =first;
while(ptr->next!=first)
{
prev=ptr;
ptr = ptr->next;
}
prev->next=first; //or u can write (prev->next = ptr->next;)
printf("\ndeleted node is %d\n",ptr->data);
free(ptr);
}
}
void search()
{
struct node *ptr;
int key,pos=0,flag=1;
ptr=first;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&key);
if(first->data == key)
{
printf("item found at location %d",pos+1);
flag=0;
}
else
{
while (ptr->next!=first)
{
flag=1;
if(ptr->data==key)
{
printf("item found at location %d ",pos+1);
flag=0;
break;
}
ptr = ptr->next;
pos++;
}
if(flag != 0)
{
printf("Item not found\n");
}
}
}
}
void display()
{
struct node *ptr;
ptr=first;
if(first == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n contents are ... \n");
while(ptr->next!=first)
{
printf("%d\n",ptr->data);
ptr = ptr->next;
}
printf("%d\n",ptr->data);
}
}
// Girish S cheluvaraj.