PROGRAM TO SPLIT TWO DOUBLY LINKED LISTS IN TWO PARTS

/*PROGRAM TO SPLIT TWO DOUBLY LINKED LISTS IN TWO PARTS*/
#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *flink,*plink;
};
void main()
{
struct node *start=NULL,*start1=NULL,*ptr,*ptr1,*temp;
int num,item;
char ch;
clrscr();
cout<<“Inserting elements in the first 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”;
}
cout<<endl<<“Enter the element after which you want to split the list: “;
cin>>item;
ptr=start;
while(ptr!=NULL)
{
if(ptr->data==item)
{
start1=ptr->flink;
ptr->flink->plink=NULL;
ptr->flink=NULL;
break;
}
ptr=ptr->flink;
}
cout<<“After splitting the elements, two lists are: “<<endl;
cout<<“First list is:”<<endl;
for(ptr=start;ptr!=NULL;ptr=ptr->flink)
{
cout<<ptr->data<<“\t”;
}
cout<<endl<<“Second list is:”<<endl;
for(ptr=start1;ptr!=NULL;ptr=ptr->flink)
{
cout<<ptr->data<<“\t”;
}
getch();
}

Leave a Reply

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