double linked list
#include<stdio.h>
void ins_front();
void ins_end();
void ins_between();
void delete_node();
//void display();
struct node
{
struct node *lptr;
int info;
struct node *rptr;
}*start=NULL, *last=NULL;
void ins_front()
{
struct node *n=(struct node*)malloc(sizeof(struct node));
if(n==NULL)
{
printf("\n Overflow");
exit(0);
}
else
{
printf("\n Enter number:");
scanf("%d",n->info);
n->lptr=NULL;
if(start==NULL)
{
start=n;
last=n;
start->rptr=NULL;
}
else
{
n->rptr=start;
start->lptr=n;
start=n;
}
}
}
void ins_end()
{
struct node *n=(struct node*)malloc(sizeof(struct node));
if(n==NULL)
{
printf("\n Overflow");
exit(0);
}
else
{
printf("\n Enter number:");
scanf("%d",&n->info);
n->rptr=NULL;
if(start==NULL)
{
start=n;
start->lptr=NULL;
}
else
{
last->rptr=n;
n->lptr=last;
last=n;
}
}
}
int count()
{
struct node *r;
int c=0;
r=start;
while(r!=NULL)
{
r=r->rptr;
c++;
}
return c;
}
void ins_between()
{
struct node *n,*r,*t;
int k,pos,c;
n=(struct node*)malloc(sizeof(struct node));
printf("enter the position:");
scanf("%d",&pos);
c=count();
if(pos>c+1||pos<=0)
printf("insertion not possible");
else if(pos==1)
{
ins_front();
return;
}
else if(pos==c+1)
ins_end();
else
{
printf("\n enter the number:");
scanf("%d",n->info);
r=start;
for(k=1;k<pos;k++)
{
t=r;
r=r->rptr;
}
r->lptr=n;
t->rptr=n;
n->rptr=r;
n->lptr=t;
}
}
void delete_node()
{
int i,loc;
struct node *r,*t;
printf("\n enter the location to delete the element:");
scanf("%d",&loc);
if(start==NULL)
{
printf("\n empty linked list.deletion not possible");
}
else
{
if(loc>0&&loc<=count())
{
if(loc==1)
{
r=start;
start=start->rptr;
printf("\n node with data %d deleted",r->info);
free(r);
}
else
{
r=start;
for(i=1;i<loc;i++)
{
t=r;
r=r->rptr;
}
if(r==last)
{
t->rptr=r->rptr;
printf("\n node with data %d deleted",r->info);
free(r);
last=t;
}
else
{
r->rptr->lptr=t;
t->rptr=r->rptr;
printf("\n node with data %d deleted",r->info);
free(r);
}
}
}
else
printf("\n deletion not possible");
}
}
/* void display()
{
struct node *r;
if(start==NULL)
printf(" not possible");
else
{
//struct node *r;
r=last;
while(r!=NULL)
{
printf("\n %d",r->info);
r=r->lptr;
}
}
}
*/
void main()
{
int ch;
while(1)
{
printf("\n1.Insert a number at front");
printf("\n2..Insert a number at end");
printf("\n3..Insert a number at middle");
printf("\n4.Deletion a number at middle");
printf("\n5.count");
printf("\n6.display");
printf("\n 7.Get out of the list");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
ins_front();
break;
}
case 2:
{
ins_end();
break;
}
case 3:
{
ins_between();
break;
}
case 4:
{
delete_node();
break;
}
case 5:
{
printf("\n No. of elements in the list:",count());
break;
}
/* case 6:
{
display();
break;
}*/
case 7:
{
exit(0);
}
default:
printf("\n Invalid choice");
}
}
}
void ins_front();
void ins_end();
void ins_between();
void delete_node();
//void display();
struct node
{
struct node *lptr;
int info;
struct node *rptr;
}*start=NULL, *last=NULL;
void ins_front()
{
struct node *n=(struct node*)malloc(sizeof(struct node));
if(n==NULL)
{
printf("\n Overflow");
exit(0);
}
else
{
printf("\n Enter number:");
scanf("%d",n->info);
n->lptr=NULL;
if(start==NULL)
{
start=n;
last=n;
start->rptr=NULL;
}
else
{
n->rptr=start;
start->lptr=n;
start=n;
}
}
}
void ins_end()
{
struct node *n=(struct node*)malloc(sizeof(struct node));
if(n==NULL)
{
printf("\n Overflow");
exit(0);
}
else
{
printf("\n Enter number:");
scanf("%d",&n->info);
n->rptr=NULL;
if(start==NULL)
{
start=n;
start->lptr=NULL;
}
else
{
last->rptr=n;
n->lptr=last;
last=n;
}
}
}
int count()
{
struct node *r;
int c=0;
r=start;
while(r!=NULL)
{
r=r->rptr;
c++;
}
return c;
}
void ins_between()
{
struct node *n,*r,*t;
int k,pos,c;
n=(struct node*)malloc(sizeof(struct node));
printf("enter the position:");
scanf("%d",&pos);
c=count();
if(pos>c+1||pos<=0)
printf("insertion not possible");
else if(pos==1)
{
ins_front();
return;
}
else if(pos==c+1)
ins_end();
else
{
printf("\n enter the number:");
scanf("%d",n->info);
r=start;
for(k=1;k<pos;k++)
{
t=r;
r=r->rptr;
}
r->lptr=n;
t->rptr=n;
n->rptr=r;
n->lptr=t;
}
}
void delete_node()
{
int i,loc;
struct node *r,*t;
printf("\n enter the location to delete the element:");
scanf("%d",&loc);
if(start==NULL)
{
printf("\n empty linked list.deletion not possible");
}
else
{
if(loc>0&&loc<=count())
{
if(loc==1)
{
r=start;
start=start->rptr;
printf("\n node with data %d deleted",r->info);
free(r);
}
else
{
r=start;
for(i=1;i<loc;i++)
{
t=r;
r=r->rptr;
}
if(r==last)
{
t->rptr=r->rptr;
printf("\n node with data %d deleted",r->info);
free(r);
last=t;
}
else
{
r->rptr->lptr=t;
t->rptr=r->rptr;
printf("\n node with data %d deleted",r->info);
free(r);
}
}
}
else
printf("\n deletion not possible");
}
}
/* void display()
{
struct node *r;
if(start==NULL)
printf(" not possible");
else
{
//struct node *r;
r=last;
while(r!=NULL)
{
printf("\n %d",r->info);
r=r->lptr;
}
}
}
*/
void main()
{
int ch;
while(1)
{
printf("\n1.Insert a number at front");
printf("\n2..Insert a number at end");
printf("\n3..Insert a number at middle");
printf("\n4.Deletion a number at middle");
printf("\n5.count");
printf("\n6.display");
printf("\n 7.Get out of the list");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
ins_front();
break;
}
case 2:
{
ins_end();
break;
}
case 3:
{
ins_between();
break;
}
case 4:
{
delete_node();
break;
}
case 5:
{
printf("\n No. of elements in the list:",count());
break;
}
/* case 6:
{
display();
break;
}*/
case 7:
{
exit(0);
}
default:
printf("\n Invalid choice");
}
}
}
Comments
Post a Comment