#include <iostream>
using namespace std;
struct list
{
int data;
struct list* next;
struct list* prev;
};
typedef struct list node;
node *head=NULL;
int flag=1;
void insert_x_y()
{
int x,y,f=0;
node *temp=head,*back_,*t;
cout<<"Please Enter x and y: "<<endl;
cin>>x;
cin>>y;
if(temp==NULL)
{
head = new node();
head->data=y;
head->next=NULL;
head->prev= NULL;
///back_ = head;
cout<<"INSERT after dummy node."<<endl;
}
else
{
while(temp->next!=NULL)
{
if(temp->data==x)
{
f=1;
break;
}
temp=temp->next;
}
if(f==1)
{
t =new node();
t->data=y;
t->next = temp->next;
temp->next->prev = t;
t->prev = temp;
temp->next = t;
cout<<"INSERT after "<<x<<endl;
}
else
{
cout<<"INSERT after dummy node."<<endl;
t =new node();
t->data=y;
t->next = head;
t->prev = NULL;
head->prev = t;
head=t;
}
}
}
void print_node()
{
node *temp=head;
if(temp==NULL)
{
cout<<"
No Data Found
"<<endl;
return;
}
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
void delete_node()
{
int x,flag=0;
node *temp=head;
cout<<"Enter The Value You Want to Delete: ";
cin>>x;
while(temp!= NULL)
{
if(temp->data==x)
{
flag=1;
break;
}
if(flag==1)break;
temp=temp->next;
}
if(flag==1)
{
cout<<"Node with key value "<<x<<" is DELETED."<<endl;
if(head->data==x)
{
head = temp->next;
temp->prev=NULL;
}
else
{
temp->next->prev=temp->prev;
temp->prev->next = temp->next;
}
delete(temp);
}
else
{
cout<<"
DELETE not possible. "<<endl;
}
}
void search_node()
{
int x,flag=0;
node *temp=head;
cout<<"Enter The Value You Want to Search: ";
cin>>x;
while(temp->next != NULL)
{
if(temp->data==x)
{
cout<<"
Node with key value "<<x<<" is FOUNDED. "<<endl;
return;
}
temp = temp->next;
}
cout<<"
Not FOUND. "<<endl;
return;
}
int menu()
{
int n;
cout<<"
Press 1 for Insert command
Press 2 for remove command
Press 3 for search command
Press 4 for show all command
Press 5 for exit command"<<endl;
cin>>n;
if(n==1)
{
insert_x_y();
}
else if(n==2)
{
delete_node();
}
else if(n==3)
{
search_node();
}
else if(n==4)
{
print_node();
}
else if(n==5)
{
flag=0;
return 0;
}
else
{
cout<<"
Wrong Input"<<endl;
menu();
}
}
int main()
{
while(1)
{
if(flag==1)
menu();
else break;
}
return 0;
}
Output
Press 1 for Insert command
Press 2 for remove command
Press 3 for search command
Press 4 for show all command
Press 5 for exit command
Add your comment