Sunday 16 September 2012

TRAVERSING DOUBLY LINKED LIST FROM BOTH SIDE LEFT TO RIGHT AND RIGHT TO LEFT

#include<conio.h>
#include<iostream.h>
struct node
{
struct node *plink;
int data;
struct node *flink;
};
void main()
{
clrscr();
int n,i,num;
struct node *temp,*ptr=NULL,*start=NULL;
temp =new node;
temp->data=10;
temp->plink=NULL;
temp->flink=NULL;

start=temp;
cout<<"Enter how many node:--"<<endl;
cin>>n;
for(i=1;i<=n;i++)
{
temp=new node;
cout<<"Enter data in node:--"<<endl;
cin>>num;
temp->data=num;
temp->plink=NULL;

temp->flink=start;
start->plink=temp;
start=temp;
}
cout<<"your data:--"<<endl;

cout<<"from left to right"<<endl;
ptr=start;
while(ptr->flink!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->flink ;
}
cout<<ptr->data;
cout<<"from right to left"<<endl;
while(ptr->plink!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->plink ;
}
cout<<ptr->data;

getch();

}

No comments:

Post a Comment