还有一个也是这种问题
#include<iostream.h>
class Node{
friend class list;
private:
int Data;
Node *previous;
Node *next;
};
class list{
private:
Node *Head;
Node *Tail;
public:
list();
~list();
void Build_HT(int Data);
void Build_TH (int Data);
void list::Display_HT();
void list::Display_TH();
void Clear();
};
int main(void)
{
list list1;
int i;
cout<<"Add to the back of the list:"<<endl;
for(i=1;i<=20;i=i+2){
list1.Build_HT(i);
cout<<i<<" ";
}
cout<<endl;
cout<<"Add to the front of the list:"<<endl;
for(i=0;i<=20;i=i+2)
{
list1.Build_TH(i);
cout<<i<<" ";
}
cout<<endl;
list1.Display_HT();
list1.Display_TH();
return 0;
}
list::list()
{
Head=0;
Tail=0;
}
list::~list()
{
Clear();
}
void list::Build_HT(int Data)
{
Node *Buffer;
Buffer=new Node;
Buffer->Data=Data;
if(Head==0)
{
Head=Buffer;
Head->next=0;
Head->previous=0;
Tail=Head;
}
else
{
Tail->next=Buffer;
Buffer->next=0;
Tail=Buffer;
}
}
void list::Build_TH (int Data)
{
Node *NewNode;
NewNode=new Node;
NewNode->Data=Data;
if(Tail==0)
{
Tail=NewNode;
Tail->next=0;
Tail->previous=0;
Head=Tail;
}
else
{
NewNode->previous=0;
NewNode->next=Head;
Head->previous=NewNode;
Head=NewNode;
}
}
//从头到尾显示数据
void list::Display_HT()
{
Node *TEMP;
TEMP=Head;
cout<<"Display the list from Head to Tail :"<<endl;
while(TEMP!=0)
{
cout<<TEMP->Data<<" ";
TEMP=TEMP->next;
}
cout<<endl;
}
//从尾显示数据
void list::Display_TH()
{
Node *Temp;
Temp=Tail;
cout<<"Display from Tail to Head;"<<endl;
while(Temp!=0)
{
cout<<Temp->Data<<" ";
Temp=Temp->previous;
}
cout<<endl;
}
void list::Clear()
{
Node *Temp_head=Head;
if(Temp_head==0) return;
do{
Node *TEMP_NODE=Temp_head;
Temp_head=Temp_head->next;
delete TEMP_NODE;
}
while(Temp_head!=0);
}
这是个双向链表的,问题和上面的一样,都郁闷死了