/* Circular Link List */
#include<stdlib.h>
#include<malloc.h>
struct circular_list
{
int d;
struct circular_list *next;
};
typedef struct circular_list node;
void create(node *);
void display(node *,node *);
node *search(node *,node *);
void insert(node *,node *);
void displaypos(node *,node *);
int main()
{
int choice;
node *root,*head,*tag;
root=(node *)malloc(sizeof(node));
head=root;
printf("\n ***********************MENU FOR LIST******************* \n");
printf("\n Press 1 to create:");
printf("\n Press 2 to display:");
printf("\n Press 3 to search:");
printf("\n Press 4 to insert");
printf("\n Press 5 to display with respect to an element in the list:");
printf("\n Press 6 to exit \n");
while(1)
{
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create(root);
break;
case 2:
display(root,head);
break;
case 3:
tag=search(root,head);
if(tag==NULL)
printf("\n Element not found:\n");
else
printf("\n The element is %d",tag->d);
break;
case 4:
insert(root,head);
break;
case 5:
displaypos(root,head);
break;
case 6:
exit(0);
break;
default:
break;
}
}
return 0;
}
void create(node *record)
{
int n,i;
node *p;
p=record;
printf("\n Enter the number of elements to be added:");
scanf("%d",&n);
printf("\n Enter the elements:\n");
scanf("%d",&record->d);
for(i=1;i<=n-1;i++)
{
record->next=(node *)malloc(sizeof(node));
record=record->next;
scanf("%d",&record->d);
}
record->next=p;
return;
}
void display(node *record,node *head)
{
printf("\n List elements are:\n");
while(1)
{
printf(" %d ",record->d);
record=record->next;
if(record==head)
break;
}
return;
}
node *search(node *record,node *head)
{
display(record,head);
node *p;
int n,flag=0;
printf("\n Enter the item to be searched:");
scanf("%d",&n);
while(record->next!=NULL)
{
if(record->d==n)
{
flag=1;
p=record;
break;
}
record=record->next;
if(record==head)
break;
}
if(flag==1)
return (p);
else
return (NULL);
}
void insert(node *record,node *head)
{
node *pos,*p,*temp;
printf("\n Search the element after which the item has to be inserted:");
pos=search(record,head);
if(pos==NULL)
{
printf("\n Element cannot be inserted:");
}
else
{
p=(node *)malloc(sizeof(node));
printf("\n Enter the item to be inserted:");
scanf("%d",&p->d);
temp=pos->next;
pos->next=p;
p->next=temp;
}
}
void displaypos(node *record,node *head)
{
node *tag,*p;
printf("\n Search the item w.r.t which the list will be displayed:");
tag=search(record,head);
if(tag==NULL)
{
printf("\n List Cannot be displayed:\n");
}
else
{
p=tag;
printf("\n List with respect to %d",tag->d);
while(tag->next!=p)
{
printf(" %d ",tag->next->d);
tag=tag->next;
}
}
return;
}