program-
#include< stdio.h>
#include< conio.h>
#include< alloc.h>
struct node{
struct node *pre;
struct node *next;
int data;
};
struct node *first=NULL,*last=NULL;
void insertion(int n,int pos)
{
struct node *ptr,*cpt=first;
int i=1;
ptr=(struct node*)malloc(sizeof(struct node));
ptr->data=n;
while(1){
if(i++==pos-1)
break;
cpt=cpt->next;
}
ptr->next=cpt->next;
cpt->next=ptr;
ptr->pre=cpt;
}
void traverse()
{
struct node *ptr;
ptr=first;
while(ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
void main()
{
struct node *cpt,*ptr;
char ch;
clrscr();
first=(struct node*)malloc(sizeof(struct node));
printf("Enter first element in Linklist\n");
scanf("%d",&first->data);
first->pre=NULL;
ptr=first;
do
{
cpt=(struct node*)malloc(sizeof(struct node));
printf("Enter another data\n");
scanf("%d",&cpt->data);
ptr->next=cpt;
cpt->pre=ptr;
ptr=ptr->next;
printf("Continue(y/n) ?");
ch=getch();
}while(ch=='y');
ptr->next=NULL;
last=ptr;
printf("\nDoubly Linked-list is\n");
traverse();
printf("\nAfter insertion of 10 at 3rd position\n");
insertion(10,3);
traverse();
printf("\nAfter insertion of 13 at 2th position\n");
insertion(13,2);
traverse();
getch();
}
Enter first element in Linklist
4
Enter another data
5
Continue(y/n)?
Enter another data
1
Continue(y/n)?
Enter another data
9
Continue(y/n)?
Enter another data
0
Continue(y/n)?
Doubly Linked-List is
4 5 1 9 0
After insertion of 10 at 3rd position
4 5 10 1 9 0
After insertion of 13 at 2nd position
4 13 5 10 1 9 0