DELETE A NODE WITH SPECIFIED VALUE IN THE DOUBLY LINKED LIST

/*PROGRAM TO DELETE A NODE WITH SPECIFIED VALUE IN THE DOUBLY LINKED LISTS*/

#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *flink,*plink;
};
void main()
{
struct node *start=NULL,*ptr,*ptr1,*temp;
int num,item;
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”;
}
//DELETING ELEMENT FROM THE LIST
do
{
if(start==NULL)
{
cout<<“!!List is empty!!”<<endl;
break;
}
cout<<endl<<“Enter the element you want to delete from the list: “;
cin>>item;
if(start->data==item)
{
cout<<“Deleted node is: “<<start->data<<endl;
start=start->flink;
start->plink=NULL;
}
else
{
ptr=start->flink;
while(ptr!=NULL)
{
if(ptr->data==item)
{
cout<<“deleted node is: “<<ptr->data<<endl;
ptr->plink->flink=ptr->flink;
ptr->flink->plink=ptr->plink;
break;
}
ptr=ptr->flink;
}
if(ptr==NULL)
{
cout<<“Specified element not found in the list”<<endl;
}
else
{
cout<<“Remaining elements in the list are:”<<endl;
for(ptr=start;ptr!=NULL;ptr=ptr->flink)
{
cout<<ptr->data<<“\t”;
}
}
}
cout<<endl<<“Do you want to delete more elements from the list?? “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
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”;
}
}
else
{
cout<<“!!Press any key to exit!!”<<endl;
}
getch();
}

Leave a Reply

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