Menu

WAP in C to perform various operations on Singly circular linked list


a.      Creation
b.      Traversal
c.       Searching
d.      Insertion
e.      Deletion
Solution:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node*link;
};
struct node*first,*ptr,*n,*temp,*p;
int item,ch;
void main()
{
void create();
void traverse();
void search();
void ins_beg();
void ins_end();
void ins_any();
void del_beg();
void del_end();
void del_any();
int c;
clrscr();
printf("\n Singly circular Linked list");
while(1)
{
printf("\n\n choose a no. of the following list:- ");
printf("\n 1. creation");
printf("\n 2. traverse");
printf("\n 3. searching");
printf("\n 4. insertion");
printf("\n 5. deletion");
printf("\n 6. exit\n");
printf("\n enter the choice:-  ");
scanf("%d",&c);
switch(c)
{
case 1:
{
create();
break;
}
case 2:
{
traverse();
break;
}
case 3:
{
search();
break;
}
case 4:
{
printf("\n which type of insertion do you want");
printf("\n 1. at beginning");
printf("\n 2. at end");
printf("\n 3. at any location\n ");
scanf("%d",&ch);
if(ch==1){
ins_beg();
break;    }
if(ch==2)  {
ins_end();
break;      }
if(ch==3)    {
ins_any();
break;        }
}
case 5:
{
printf("\n which type of deletion do you want");
printf("\n 1. at beginning");
printf("\n 2. at end");
printf("\n 3. at any location\n ");
scanf("%d",&ch);
if(ch==1){
del_beg();
break;    }
if(ch==2)  {
del_end();
break;      }
if(ch==3)    {
del_any();
break;        }
case 6:
{
exit(1);
}
default:
{
printf("\n invalid choice");
}
getch();
}
}}

void create()
{
struct node*last;
int i,n;
printf("\n how many elements do you want to enter :-   ");
scanf("%d",&n);
printf("enter the elements of linked list\n");
scanf(" %d",&item);
first=(struct node*)malloc(sizeof(struct node*));
first->data=item;
first->link=first;
temp=first;
for(i=1;i<n;i++)
{
scanf(" %d",&item);
last=(struct node*)malloc(sizeof(struct node*));
last->data=item;
last->link=NULL;
temp->link=last;
temp=last;
temp->link=first;}
}
void traverse()
{
ptr=first;
printf("\n linked list is :- \n ");
printf("%d",ptr->data);
ptr=ptr->link;
while(ptr!=first)
{
printf("\n%d",ptr->data);
ptr=ptr->link;
}

}
void search()
{
int flag=0;
printf("\n enter the no. which you want to search :-  ");
scanf("%d",&item);
ptr=first;
if(item==ptr->data)
flag=1;
else
ptr=ptr->link;
while(ptr!=first)
{
if(item==ptr->data)
{
flag=1;
break;
}
else
ptr=ptr->link;
}
if(flag==1)
printf("\n item found :- %d ",ptr->data);
else
printf("\n item not found");
}
void ins_beg()
{
n=(struct node*)malloc(sizeof(struct node*));
if(n==NULL)
printf("\n overflow");
printf("\n enter the new data which you want to add at beginning :-  ");
scanf("%d",&item);
n->data=item;
if(first==NULL)
{
n->link=NULL;
first=n;
n->link=first;
}
else
{
temp=n;
temp->link=first;
first=temp;
last->link=first;
}}
void ins_end()
{
n=(struct node*)malloc(sizeof(struct node*));
if(n==NULL)
printf("\n overflow");
printf("\n enter the new data which you want to add at end :-  ");
scanf("%d",&item);
n->data=item;
n->link=NULL;
if(first==NULL)
{
first=n;
n->link=first;
}
else
{
ptr=first;
while(ptr->link!=first)
ptr=ptr->link;
ptr->link=n;
n->link=first;
ptr=n;
}}
void ins_any()
{
int val;
n=(struct node*)malloc(sizeof(struct node*));
if(n==NULL)
printf("\n overflow");
printf("\n enter the new data which you want to add :-  ");
scanf("%d",&item);
printf("\n enter the value after which you want to insert your new node:- ");
scanf("%d",&val);
n->data=item;
if(first==NULL)
{
first=n;
n->link=first;
}
else
{
temp=first;
while((temp->data!=val)&&(temp!=first))
temp=temp->link;
if(temp==NULL)
printf("\n value not found");
n->link=temp->link;
temp->link=n;
}
}
void del_beg()
{
if(first==NULL)
printf("\n list is empty");
temp=first;
item=first->data;
if(first->link==temp)
{
free(temp);
first=NULL;
}
else
{
first=first->link;
last->link=first;
free(temp);
}}
void del_end()
{
if(first==NULL)
printf("\n list is empty");
if(first->link==NULL)
{
item=first->data;
temp=first;
free(temp);
first=NULL;
}
temp=first;
p=NULL;
while(temp->link!=first)
{          p=temp;
temp=temp->link;
}
item=temp->data;
p->link=first;
free(temp);
}
void del_any()
{
if(first==NULL)
printf("\n list is empty");
printf("\n enter the value which you want to delete :-  ");
scanf("%d",&item);
temp=first;
if((first->link==temp)&&(first->data==item))
{
free(temp);
first=NULL;
}
p=NULL;
while((temp->data!=item)&&(temp->link!=first))
{
p=temp;
temp=temp->link;
}
if(temp==first)
printf("\n item not found");
p->link=temp->link;
free(temp);
}

No comments:

Post a Comment