program to create two way linked list.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define
NULL 0
void
main()
{
struct
dnode
{
struct
dnode *prev;
int
data;
struct
dnode *next;
}*start,*temp,*k,*p,*pre;
int
i,c;
char
ch;
clrscr();
start=NULL;
for(i=0;i<=1;i++)
{
if(start==NULL)
{
temp=(struct
dnode*)malloc(sizeof(struct dnode));
printf("\nEnter
the value for node: -");
scanf("%d",&temp->data);
start=
temp;
temp->prev=NULL;
temp->next=NULL;
}
else
{
while(1)
{
printf("\nDo
you want to create one more node(Y/N):- ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='y'
|| ch=='Y')
{
temp->next=(struct
dnode*)malloc(sizeof(struct dnode));
printf("\nEnter
the value of node: -");
k=temp->next;
scanf("%d",&k->data);
k->prev=temp;
k->next=NULL;
temp=k;
}
else
{
temp->next=NULL;
break;
}
}
}
}
//For
forward Displaying the linked list.
printf("\n\n\nBy
forward Displaying: -");
printf("\nNodes
of linked list are-\n");
temp=start;
while(temp!=NULL)
{
printf("%4d",temp->data);
temp=temp->next;
}
//For
backward Displaying the linked list.
printf("\n\n\nBy
backward Displaying:");
printf("\nNodes
of linked list are-\n");
temp=k;
while(temp!=NULL)
{
printf("%4d",temp->data);
temp=temp->prev;
}
getch();
}
output:
Enter
the value for node: -14
Do
you want to create one more node(Y/N):- y
Enter
the value of node: -63
Do
you want to create one more node(Y/N):- y
Enter
the value of node: -87
Do
you want to create one more node(Y/N):- y
Enter
the value of node: -69
Do
you want to create one more node(Y/N):- y
Enter
the value of node: -100
Do
you want to create one more node(Y/N):- n
By
forward Displaying: -
Nodes
of linked list are-
14
63 87 69 100
By
backward Displaying:
Nodes
of linked list are-
100
69 87 63 14
No comments:
Post a Comment