PROGRAM INSERT ELEMENT IN THE BEGINING OF DOUBLY LINKED LIST

/*PROGRAM TO INSERT AN ELEMENT IN THE BEGINING 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’||ch==’Y’);
cout<<“Elements in the forward direction are: “<<endl;
for(ptr=start;ptr->flink!=NULL;ptr=ptr->flink)
{
cout<<ptr->data<<“\t”;
}
cout<<ptr->data<<endl;
cout<<“Elements in the backward direction are: “<<endl;
for(ptr1=ptr;ptr1!=NULL;ptr1=ptr1->plink)
{
cout<<ptr1->data<<“\t”;
}
//DELETION
cout<<endl<<“Do you want to delete elements?? “;
cin>>ch;
if(ch==’y’||ch==’Y’)
{
do
{
if(start==NULL)
{
cout<<“!!List is empty!!”;
break;
}
else
{
cout<<“Deleted element is: “<<start->data<<endl;
start=start->flink;
start->plink=NULL;
if(start!=NULL)
{
cout<<“Elements in the forward direction are: “<<endl;
for(ptr=start;ptr->flink!=NULL;ptr=ptr->flink)
{
cout<<ptr->data<<“\t”;
}
cout<<ptr->data<<endl;
cout<<“Elements in the backward direction are: “<<endl;
for(ptr1=ptr;ptr1!=NULL;ptr1=ptr1->plink)
{
cout<<ptr1->data<<“\t”;
}
}
}
cout<<endl<<“Do you want to continue?? “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
if(start!=NULL)
{
cout<<“Remaining elements in the list are: “<<endl;
for(ptr=start;ptr!=NULL;ptr=ptr->flink)
{
cout<<ptr->data<<“\t”;
}
}
}
else
cout<<“!!Press any key to exit!!”;
getch();
}

Leave a Reply

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