Doubly-Linked Lists — linked lists containing integer values or pointers to data, with the ability to iterate over the list in both directions.
Language : C
[sourcecode language='c']
//Doubly Linked List
#include
#include
#include
void insert();
void del();
void display();
struct node
{
int a;
struct node *pred, *succ;
}*list, *temp,*save;
void main()
{ int a;
char ch;
clrscr();
list=(struct node *)malloc(sizeof(struct node));
list=NULL;
do{
printf(“\n1.Insert\n2.Delete\n3.Display : “);
scanf(“%d”, &a);
switch(a)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
}
printf(“\nMENU: (Y/N)”);
fflush(stdin);
scanf(“%c”, &ch);
}while(ch==’Y’ || ch==’y');
}
void insert()
{ int x,a,v,ctr,pos;
char ch;
do{
temp=(struct node *)malloc(sizeof(struct node));
printf(“\nEnter the element : “);
scanf(“%d”, &x);
temp->a=x;
if(list==NULL)
{
temp->pred=NULL;
temp->succ=NULL;
list=temp;
save=list;
}
else
{
printf(“\n1.At front\t2.At End\t3.By Position”);
scanf(“%d”, &a);
switch(a)
{
case 1:
list->pred=temp;
temp->succ=list;
temp->pred=NULL;
list=temp;
save=list;
break;
case 2:
while(list->succ!=NULL)
list=list->succ;
temp->succ=NULL;
temp->pred=list;
list->succ=temp;
break;
case 3:
printf(“\nEnter the Position : “);
scanf(“%d”, &pos);
ctr=0;
save=list;
while(ctr
list=list->succ;
ctr++;
}
temp->succ=list;
temp->pred=list->pred;
list->pred->succ=temp;
list->pred=temp;
list=save;
break;
}
}
printf(“Continue (Y/N)”);
fflush(stdin);
scanf(“%c”, &ch);
}while(ch==’y’ || ch==’Y');
display();
}
void del()
{
int a,pos,ctr;
if(list=NULL)
{
printf(“\nUnderflow!!!”);
return;
}
printf(“\n1.At front\t2.At End\t3.By Position”);
scanf(“%d”, &a);
list=save;
switch(a)
{
case 1:
printf(“\nThe deleted element : %d”,list->a);
temp=list;
list=list->succ;
temp->succ=NULL;
free(temp);
save=list;
break;
case 2:
list=save;
while(list->succ!=NULL)
list=list->succ;
printf(“\nThe deleted element : %d”,list->a);
temp=list;
list->pred->succ=NULL;
free(temp);
list=save;
break;
case 3:
printf(“Enter the position : “);
scanf(“%d”, &pos);
ctr=0;
temp=NULL;
list=save;
while(ctr
if(list==NULL)
{
printf("Wrong position!");
return;
}
list=list->succ;
ctr++;
}
printf(“\nThe deleted element is : %d”, list->a);
list->pred->succ=list->succ;
list->succ->pred=list->pred;
list->succ=NULL;
list->pred=NULL;
free(list);
list=save;
break;
}
display();
}
void display()
{
if(list==NULL)
{ printf(“\nList is Empty!!!”);
return;
}
temp=save;
printf(“\nLinked List : \n”);
do{
printf(“%d\t”,temp->a);
temp=temp->succ;
}while(temp!=NULL);
list=save;
}
[/sourcecode]







{ 1 comment… read it below or add one }
when running this programmethen show 26 error. please check the solution