#include<iostream.h>
template<class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
nodeType<Type> *back;
};
template<class Type>
class doublelist
{
public:
doublelist();
// doublelist(const doublelist<Type>& otherlist);
const doublelist<Type>&operator=(const doublelist<Type>&otherlist);
virtual ~doublelist();
void initializelist();
void destory();
bool isEmptylist();
void print();
void rprint();
int length();
void insertItem(const Type& insertItem);
void deleteItem(const Type& deleteItem);
private:
nodeType<Type> *first;
};
template<class Type>
const doublelist<Type>& doublelist<Type>::operator=(const doublelist<Type>& otherlist)
{
if(first != NULL)
{
destory();
}
if(this != &otherlist)
{
if(otherlist.first == NULL)
{
first = NULL;
}
else
{
nodeType<Type>* pointer = otherlist.first;
nodeType<Type>* this_pointer = first;
while( pointer != NULL )
{
nodeType<Type>* newNode = new nodeType<Type>;
newNode->info = pointer->info;
newNode->link = NULL;
newNode->back = NULL;
if( this_pointer == NULL )
{
this_pointer = first = newNode;
newNode->back = NULL;
newNode->link = NULL;
}
else
{
newNode->back = this_pointer;
newNode->link = NULL;
this_pointer->link = newNode;
this_pointer = this_pointer->link;
}
pointer = pointer->link;
}
}
}
return *this;
}
template<class Type>
doublelist<Type>::~doublelist()
{
nodeType<Type> *temp;
while( first != NULL)
{
temp = first;
first = first->link;
delete temp;
}
//cout<<"析构函数被调用"<<endl;
}
template<class Type>
doublelist<Type>::doublelist()
{
first = NULL;
}
template<class Type>
void doublelist<Type>::initializelist()
{
doublelist<Type>::destory();
}
template<class Type>
bool doublelist<Type>::isEmptylist()
{
return(first == NULL);
}
template<class Type>
void doublelist<Type>::destory()
{
nodeType<Type> *temp;
while( first != NULL)
{
temp = first;
first = first->link;
delete temp;
}
}
template<class Type>
void doublelist<Type>::print()
{
nodeType<Type> *current;
current=first;
while(current!=NULL)
{
cout<<current->info<<" ";
current=current->link;
}
}
template<class Type>
void doublelist<Type>::rprint()
{
nodeType<Type> *current = first;
while(current!=NULL && current->link!= NULL)
current = current->link;
while(current!=NULL)
{
cout<<current->info<<" ";
current=current->back;
}
}
template<class Type>
int doublelist<Type>::length()
{
int count=0;
nodeType<Type> *current;
current=first;
while(current!=NULL)
{
count++;
current=current->link;
}
return count;
}
template<class Type>
void doublelist<Type>::insertItem(const Type& insertItem)
{
nodeType<Type>* pointer = first;
bool bFind = false;
while( pointer != NULL )
{
if( pointer->info == insertItem )
{
bFind = true;
break;
}
else
{
pointer = pointer->link;
}
}
if( bFind )
{
cout << insertItem << " have already exist!!" << endl;
}
else
{
nodeType<Type>* newnode = new nodeType<Type>();
if( first == NULL )
{
first = newnode;
newnode->back = NULL;
}
else
{
pointer = first;
while( pointer->link != NULL )
pointer = pointer->link;
pointer->link = newnode;
newnode->back = pointer;
}
newnode->link = NULL;
newnode->info = insertItem;
}
}
void main()
{
doublelist<int> b,c;
int num = 0, i = 0, j = 0;
cout<<"please input the count of number:"<<endl;
cin>>num;
while(i < num)
{
cout<<"please input the number:";
cin >> j;
cout<<endl;
b.insertItem(j);
i++;
}
cout << "b normal: ";
b.print();
cout << endl;
c=b;
cout << "normal: ";
c.print();
cout << endl;
cout << "not normal: ";
c.rprint();
cout << endl;
}