a. Creation
b. Traversal
c. Searching
d. Insertion
e. Deletion
Solution:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node*llink;
struct node*rlink;
};
struct
node*first,*ptr,*n,*temp,*last;
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 doubly 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()
{
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->llink=first;
first->rlink=first;
temp=first;
for(i=1;i<n;i++)
{
scanf("%d",&item);
last=(struct
node*)malloc(sizeof(struct node*));
last->data=item;
temp->rlink=last;
last->llink=temp;
last->rlink=first;
temp=last; }
}
void traverse()
{
ptr=first;
printf("\n linked list is
:- ");
printf("
%d",ptr->data);
ptr=ptr->rlink;
while(ptr!=first)
{
printf(" %d",ptr->data);
ptr=ptr->rlink;
}
}
void search()
{
int flag=0;
printf("\n enter the no.
which you want to search :- ");
scanf("%d",&item);
ptr=first;
if(ptr->data==item) {
printf("\n item found:- %d");
}
ptr=ptr->rlink;
while(ptr!=first)
{
if(item==ptr->data)
{
flag=1;
break;
}
else
ptr=ptr->rlink;
}
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)
{
first=n;
n->llink=first;
n->rlink=first;
}
else
{
n->rlink=first;
n->llink=last;
first->llink=n;
last->rlink=n;
first=n;
}}
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;
if(first==NULL)
{
first=n;
n->llink=first;
n->rlink=first;
}
else
{
ptr=first;
while(ptr->rlink!=first)
ptr=ptr->rlink;
ptr->rlink=n;
n->llink=ptr;
first->llink=n;
n->rlink=first;
ptr=n;
last=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->llink=first;
n->rlink=first;
}
else
{
temp=first;
if(temp->data==val)
{
n->rlink=temp->rlink;
temp->rlink->llink=n;
n->llink=temp;
temp->rlink=n;
}
temp=temp->rlink;
while((temp->data!=val)&&(temp!=first))
temp=temp->rlink;
if(temp==first)
printf("\n value not
found");
n->rlink=temp->rlink;
temp->rlink->llink=n;
n->llink=temp;
temp->rlink=n;
}
}
void del_beg()
{
if(first==NULL)
printf("\n list is
empty");
temp=first;
item=first->data;
if(first->rlink==temp)
{
free(temp);
first=NULL;
}
else
{
first=first->rlink;
first->llink=temp->llink;
last->rlink=temp->rlink;
free(temp);
}}
void del_end()
{
if(first==NULL)
printf("\n list is
empty");
if(first->rlink==NULL)
{
item=first->data;
temp=first;
free(temp);
first=NULL;
}
temp=first;
while(temp->rlink!=first)
{
temp=temp->rlink;
}
item=temp->data;
temp->llink->rlink=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->rlink==temp)&&(first->data==item))
{
free(temp);
first=NULL;
}
temp=temp->rlink;
while((temp->data!=item)&&(temp->rlink!=first))
{
temp=temp->rlink;
}
if(temp==first)
printf("\n item not
found");
temp->rlink->llink=temp->llink;
temp->llink->rlink=temp->rlink;
free(temp);
}
No comments:
Post a Comment