Sie sind auf Seite 1von 7

17. // IMPLEMENT A DOUBLY LINK LIST USING TEMPLATE.

INCLUDE FUNCTION FOR INSERTI


ON, DELETION, SEARCH OF A NO., REVERSE THE LIST.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template <class X>
class element
{
public:
X data;
element *pre,*nxt;
};
template <class X>
class LL
{
element<X> *Lstart,*Rstart,*temp;
public:
LL()
{
Rstart=Lstart=temp=NULL;
}
void insert()
{
cout<<"\n\nEnter the data...";
X info;
cin>>info;
temp=new element<X>;
temp->data=info;
if((Lstart==NULL)&&(Rstart==NULL))
{
Lstart=Rstart=temp;
temp->pre=NULL;
temp->nxt=NULL;
}
else
{
cout<<"\nInsert on Left side or Right Side... ( L / R ) ";
char ch;
ch=getch();
switch(ch)
{
case 'l':
{}
case 'L':
{
Lstart->pre=temp;
temp->pre=NULL;
temp->nxt=Lstart;
Lstart=temp;
break;
}
case 'r':
{}
case 'R':

{
Rstart->nxt=temp;
temp->pre=Rstart;
temp->nxt=NULL;
Rstart=temp;
break;
}
default:
{
cout<<"\nData not attached to List...";
}
}
}
cout<<"\nDone...";
}
void display()
{
cout<<"\nDisplay from Left Or Right (L / R) ";
char ch;
int flag=1;
ch=getch();
switch(ch)
{
case 'l':
{
}
case 'L':
{
flag=1;
temp=Lstart;
break;
}
case 'r':
{
}
case 'R':
{
flag=0;
temp=Rstart;
break;
}
default:
{
flag=1;
temp=Lstart;
}
}
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
if(flag)
{
temp=temp->nxt;
}
else
{
temp=temp->pre;
}
}

cout<<"\nDone...";
}
void del()
{
display();
cout<<"\nEnter the term you want to DELETE... ";
X info;
cin>>info;
int flag=0;
for(temp=Lstart;temp!=NULL;temp=temp->nxt)
{
if(temp->data==info)
{
flag=1;
break;
}
}
if(flag)
{
cout<<"\nFound... "<<temp->data;
if(temp==Lstart)
{
(temp->nxt)->pre=NULL;
Lstart=temp->nxt;
}
else
{
if(temp==Rstart)
{
(temp->pre)->nxt=NULL;
Rstart=temp->pre;
}
else
{
(temp->pre)->nxt=temp->nxt;
(temp->nxt)->pre=temp->pre;
}
}
delete temp;
cout<<"\nDELETED...";
}
else
{
cout<<"\nNot Found...";
}
}
void search()
{
display();
cout<<"\nEnter the term you want to SEARCH... ";
X info;
cin>>info;
int flag=0;
for(temp=Lstart;temp!=NULL;temp=temp->nxt)
{
if(temp->data==info)
{
flag=1;

break;
}
}
if(flag)
{
cout<<"\nFound... "<<temp->data;
}
else
{
cout<<"\nNot Found...";
}
}
void rev()
{
element<X> *L=NULL,*R=NULL;
X t;
L=Lstart;
R=Rstart;
while(L!=R)
{
t=L->data;
L->data=R->data;
R->data=t;
L=L->nxt;
R=R->pre;
}
cout<<"\nReversed...";
}
};
template <class X>
void menu(X x)
{
LL<X> A;
char ch;
do
{
clrscr();
cout<<"\n1)Insert\n ;
cout<< 2)Display\n ;
cout<< 3)Delete\n ;
cout<< 4)Search\n
cout<< 5)Reverse\n ;
cout<< Enter choice or 0 to exit...";
ch=getch();
switch(ch)
{
case '1':
{
do
{
A.insert();
cout<<"\nEnter another... (Y / N) ";
ch=getch();
}while(ch=='y'||ch=='Y');
break;
}
case '2':
{

A.display();
break;
}
case '3':
{
do
{
A.del();
cout<<"\nDelete another... (Y / N) ";
ch=getch();
}while(ch=='y'||ch=='Y');
break;
}
case '4':
{
do
{
A.search();
cout<<"\nSearch another... (Y / N) ";
ch=getch();
}while(ch=='y'||ch=='Y');
break;
}
case '5':
{
A.rev();
break;
}
default:
{
Cout<< \nEnter the correct choice ;
}
}
cout<<"\nPress any key... ";
getch();
}while(ch!='0');
}
void main()
// Main Function
{
clrscr();
cout<<"\nWhich data do you want to work with??\n ;
cout<< 1) Integer\n ;
cout<< 2) Float\n ;
cout<< 3)Character\n ";
char ch;
ch=getch();
switch(ch)
{
case '1':
{
menu(0);
break;
}
case '2':
{
menu(1.0);

break;
}
case '3':
{
menu('a');
break;
}
default:
{
cout<<"\nInteger assumed... Press any key";
getch();
menu(0);
}
}
cout<<"\nProgram Terminated...";
OUTPUT :-Which data do you want to work with??
1) Integer
2) Float
3)Character
2
1)Insert
2)Display
3)Delete
4)Search
5)Reverse
Enter choice or 0 to exit...
Enter the data...2.51
Done...
Enter another... (Y / N)
Enter the data...3.52
Insert on Left side or Right Side... ( L / R )
Done...
Enter another... (Y / N)
Enter the data...6.53
Insert on Left side or Right Side... ( L / R )
Done...
Enter another... (Y / N)
Enter the data...8.25
Insert on Left side or Right Side... ( L / R )
Done...
Enter another... (Y / N)
Display from Left Or Right (L / R)
3.52
2.51
6.53
8.25
Done...
Press any key...
Enter the term you want to DELETE...6.53

FOUND 6.53
DELETED
Delete another... (Y / N)
Done...
Press any key...
1)Insert
2)Display
3)Delete
4)Search
5)Reverse
Enter choice or 0 to exit...
Press any key...
Program Terminated...

Das könnte Ihnen auch gefallen