//double linked list
#include<iostream>
using namespace std;
#include<conio.h>
#define double Double
template<class T>
class node
{
public:
T data;
node *left,*right;
};
template<class T>
class double
{
private:
node<T> *first;
public:
double()
{
first==NULL;
}
void createlist();
void deletion();
void insertion();
void display();
};
template<class T>
void double<T>::createlist()
{
int a;
node<T> *cur,*ptr;
first=NULL;
cout<<"\n enter data for new node:";
cin>>a;
while(a)
{
cur=new node<T>;
cur->data=a;
cur->left=cur->right=NULL;
if(first==NULL)
{
first=cur;
}
else
{
cur->left=ptr;
ptr->right=cur;
}
ptr=cur;
cout<<"\nenter data of node:";
cin>>a;
}
}
template<class T>
void double<T>::display()
{
node<T> *ptr,*last;
ptr=first;
last=NULL;
cout<<"\n the list is:\n:";
while(ptr!=NULL)
{
cout<<ptr->data<<"\t";
if(ptr->right==NULL)
last=ptr;
ptr=ptr->right;
}
cout<<"\n reverse list is:";
ptr=last;
while(ptr!=NULL)
{
cout<<ptr->data<<"\t";
ptr=ptr->left;
}
}
template<class T>
void double<T>::insertion()
{
T ele;
char ch;
node<T> *ptr,*cur;
ptr=first;
cur=new node<T>;
cout<<"enter data for the node:";
cin>>cur->data;
cur->right=cur->left=NULL;
cout<<"do u want to insert first [y/n]:";
cin>>ch;
if(ch=='y'||ch=='Y')
{
ptr->left=cur;
cur->right=ptr;
first=cur;
}
else
{
cout<<"after which element do u want to insert:";
cin>>ele;
while(ptr!=NULL)
{
if(ptr->data==ele)
{
cur->right=ptr->right;
cur->left=ptr;
ptr->right->left=cur;
ptr->right=cur;
break;
}
else
{
ptr=ptr->right;
}
}
}
}
template<class T>
void double<T>::deletion()
{
T ele;
char ch;
node<T> *ptr;
ptr=first;
cout<<"do u want to delete first element[y/n]:";
cin>>ch;
if(ch=='y'||ch=='Y')
{
ptr->right->left=NULL;
first=ptr->right;
}
else
{
cout<<"enter the element which u want to delete:";
cin>>ele;
while(ptr!=NULL)
{
if(ptr->data==ele)
{
ptr->right->left=ptr->left;
ptr->left->right=ptr->right;
break;
}
else
{
ptr=ptr->right;
}
}
}
}
int main()
{
int ch;
double<int> d;
d.createlist();
do
{
cout<<"\n 1.insert \n 2.delete \n 3.display \n 4.exit \n";
cout<<"enter the choice:";
cin>>ch;
switch(ch)
{
case 1: d.insertion();
break;
case 2: d.deletion();
break;
case 3: d.display();
break;
case 4:
break;
}
}while(ch<=4);
_getch();
return 0;
}
#include<iostream>
using namespace std;
#include<conio.h>
#define double Double
template<class T>
class node
{
public:
T data;
node *left,*right;
};
template<class T>
class double
{
private:
node<T> *first;
public:
double()
{
first==NULL;
}
void createlist();
void deletion();
void insertion();
void display();
};
template<class T>
void double<T>::createlist()
{
int a;
node<T> *cur,*ptr;
first=NULL;
cout<<"\n enter data for new node:";
cin>>a;
while(a)
{
cur=new node<T>;
cur->data=a;
cur->left=cur->right=NULL;
if(first==NULL)
{
first=cur;
}
else
{
cur->left=ptr;
ptr->right=cur;
}
ptr=cur;
cout<<"\nenter data of node:";
cin>>a;
}
}
template<class T>
void double<T>::display()
{
node<T> *ptr,*last;
ptr=first;
last=NULL;
cout<<"\n the list is:\n:";
while(ptr!=NULL)
{
cout<<ptr->data<<"\t";
if(ptr->right==NULL)
last=ptr;
ptr=ptr->right;
}
cout<<"\n reverse list is:";
ptr=last;
while(ptr!=NULL)
{
cout<<ptr->data<<"\t";
ptr=ptr->left;
}
}
template<class T>
void double<T>::insertion()
{
T ele;
char ch;
node<T> *ptr,*cur;
ptr=first;
cur=new node<T>;
cout<<"enter data for the node:";
cin>>cur->data;
cur->right=cur->left=NULL;
cout<<"do u want to insert first [y/n]:";
cin>>ch;
if(ch=='y'||ch=='Y')
{
ptr->left=cur;
cur->right=ptr;
first=cur;
}
else
{
cout<<"after which element do u want to insert:";
cin>>ele;
while(ptr!=NULL)
{
if(ptr->data==ele)
{
cur->right=ptr->right;
cur->left=ptr;
ptr->right->left=cur;
ptr->right=cur;
break;
}
else
{
ptr=ptr->right;
}
}
}
}
template<class T>
void double<T>::deletion()
{
T ele;
char ch;
node<T> *ptr;
ptr=first;
cout<<"do u want to delete first element[y/n]:";
cin>>ch;
if(ch=='y'||ch=='Y')
{
ptr->right->left=NULL;
first=ptr->right;
}
else
{
cout<<"enter the element which u want to delete:";
cin>>ele;
while(ptr!=NULL)
{
if(ptr->data==ele)
{
ptr->right->left=ptr->left;
ptr->left->right=ptr->right;
break;
}
else
{
ptr=ptr->right;
}
}
}
}
int main()
{
int ch;
double<int> d;
d.createlist();
do
{
cout<<"\n 1.insert \n 2.delete \n 3.display \n 4.exit \n";
cout<<"enter the choice:";
cin>>ch;
switch(ch)
{
case 1: d.insertion();
break;
case 2: d.deletion();
break;
case 3: d.display();
break;
case 4:
break;
}
}while(ch<=4);
_getch();
return 0;
}
No comments:
Post a Comment