TRAVERSE ELEMENTS FROM BOTH SIDES OF DOUBLY LINKED LIST

/*PROGRAM TO TRAVERSE ELEMENTS FROM BOTH SIDES OF DOUBLY LINKED LIST*/
#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *flink,*plink;
};
void main()
{
struct node *start=NULL,*ptr,*temp,*ptr1;
int num;
char ch;
clrscr();
cout<<“Inserting elements in the list: “<<endl;
do
{
temp=new node;
cout<<“Enter the element: “;
cin>>num;
temp->data=num;
if(start==NULL)
{
start=temp;
start->plink=NULL;
start->flink=NULL;
}
else
{
temp->flink=start;
temp->plink=NULL;
start->plink=temp;
start=temp;
}
cout<<“Do you want to continue?? “;
cin>>ch;
}while(ch==’y’);
cout<<“Forward traversing of elements: “<<endl;
for(ptr=start;ptr->flink!=NULL;ptr=ptr->flink)
{
cout<<ptr->data<<“\t”;
}
cout<<ptr->data<<endl;
cout<<“Backward traversing of elements: “<<endl;
for(ptr1=ptr;ptr1!=NULL;ptr1=ptr1->plink)
{
cout<<ptr1->data<<“\t”;
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *