C Program – Implementation of circular linked list
[sociallocker]
#include
#include
#include
typedef struct cllist{
int x;
struct cllist *next;
}nd;
nd*insert(nd**,nd**,nd**);
nd*delete(nd**,nd**,nd**);
nd*insfro(nd**,nd**,nd**);
nd*insaftnd(nd**,nd**,nd**);
void display(nd*,nd*);
void main()
{
int n;
nd *start,*current,*head;
head=(nd*)malloc(sizeof(nd));
start=head;
current=start;
do{
clrscr();
textbackground(RED);
textcolor(BLACK+BLINK);
printf(“CIRCULAR LINKED LIST”);
printf(“MAIN MENU”);
printf(”
1>. INSERT
2>.INSERTION AT FRONT
3>.INSERION AFTER A NODE
4>.DELETION
5>.END
“);
printf(” ______________________________________________________ “);
printf(”
enter your choice: “);
scanf(“%d”,&n);
switch(n)
{
case 1:
insert(&head,&start,¤t);
break;
case 2:
insfro(&head,&start,¤t);
break;
case 3:
insaftnd(&head,&start,¤t);
break;
case 4:
if(start==head){
printf(”
the list empty!”);
break;
}
delete(&head,&start,¤t);
break;
case 5:
clrscr();
printf(” “);
printf(“press any key to exit…”);
getch();
exit(0);
default:
printf(“invalid choice!”);
getch();
break;
}
}while(n!=5);
getch();
}
nd*insert(nd**head,nd**start,nd**current)
{
int m;
nd*temp,*val;
clrscr();
printf(“INSERTION “);
do{
temp=(nd*)malloc(sizeof(nd));
if(!temp ){
printf(“memory allocation not possible!”);
getch();
exit(0);
}
val=temp;
temp=temp->next;
printf(”
enter the element:”);
scanf(“%d”,&m);
val->x=m;
val->next=*head;
if(*start==*head){
(*head)->next=val;
*start=val;
*current=*start;
}
else{
(*current)->next=val;
*current=val;
}
printf(“any more inserton(y/n):”);
fflush(stdin);
}while(toupper(getch())!=’N’);
display(*head,*start);
getch();
return(*head,*start,*current);
}
void display(nd*head,nd*start)
{
if(head==start){
printf(“the list is empty!”);
getch();
return;
}
else{
printf(“the list is now:”);
do{
printf(“%5d”,start->x);
start=start->next;
}while(start!=head);
}
return;
}
nd*delete(nd**head,nd**start,nd**current)
{
int ele;
nd *temp,*save,*val,*pred;
clrscr();
printf(“DELETION”
);
save=*start;
do{
display(*head,*start );
val=save;
printf(“enter the element to be deleted:”);
scanf(“%d”,&ele);
while(save->x!=ele && save->next!=val){
pred=save;
save=save->next;
}
if(save->next==val){
printf(“the elment is not in the list!”);
getch();
break;
}
temp=save;
printf(“the element to be deleted is:%d”,temp->x);
printf(“r u sure(y/n):”);
fflush(stdin);
if(toupper(getch())!=’N’){
if(save==(*head)->next){
*start=(*start)->next;
free(temp);
save=*start;
}
else if(save->next==*head){
pred->next=*head;
*current=pred;
free(temp);
save=pred;
}
else{
pred->next=temp->next;
free(temp);
save=pred;
}
}
else
break;
printf(“anymore deletion(y/n):”);
fflush(stdin);
}while(toupper(getch())!=’N’);
display(*head,*start);
getch();
return(*head,*start,*current);
}
nd*insfro(nd**head,nd**start,nd**current)
{
int m;
nd *temp,*val;
clrscr();
printf(“INSERTION IN FRONT”);
printf(” __________________________________ “);
do{
temp=(nd*)malloc(sizeof(nd));
if(!temp){
printf(“memory allocation not possible!”);
getch();
exit(0);
}
val=temp;
temp=temp->next;
printf(“enter the element:”);
scanf(“%d”,&m);
val->x=m;
if(*start==*head){
val->next=*head;
(*head)->next=val;
*start=val;
*current=*start;
}
else{
(*head)->next=val;
val->next=*start;
*start=val;
}
printf(“any more insertion to be performed(y/n):”);
fflush(stdin);
}while(toupper(getch())!=’N’);
display(*head,*start);
getch();
return(*head,*start,*current);
}
nd*insaftnd(nd**head,nd**start,nd**current)
{
int trg,n=1,m;
nd *temp,*val,*save,*pred;
save=*start;
clrscr();
printf(” INSERTION AFTER A NODE”);
printf(“__________________________________”);
do{
display(*head,*start);
temp=(nd*)malloc(sizeof(nd));
if(!temp){
printf(“memory allocation not possible!”);
getch();
exit(0);
}
val=temp;
temp=temp->next;
printf(“enter the element:”);
scanf(“%d”,&m);
val->x=m;
if(*start==*head){
val->next=*head;
(*head)->next=val;
*start=val;
*current=*start;
}
else{
printf(“enter the position of insertion:”);
scanf(“%d”,&trg);
while(trg!=n){
pred=save;
save=save->next;
n++;
if(save==*head)
n=0;
}
pred->next=val;
val->next=save;
save=save->next;
}
printf(“any more insertion(y/n):”);
fflush(stdin);
}while(toupper(getch())!=’N’);
display(*head,*start);
getch();
return(*head,*start,*current);
}
[/sociallocker]