我是个初学C++者,有很多不懂的,望大家指教..我有个链表程序,就是不知道怎么改才对
#include<iostream.h>
class Node{
friend class List;
private:
int date;
Node *next; //后继指针
};
class List{
Node *PList;
static int ElemCount; //ElemCount是元素个数-1
public:
List(); //柝构函数
~List();
int Build_IN(int i,int NodeElem); //从第i个元素插入NodeElem
void Display_HT(); //从头到尾显示链表元素
void Clear();
};
int List::ElemCount=0;
List::List(){PList=new Node;PList=0;} //1
int List::Build_IN(int i,int NodeElem) //2
{
if(i>ElemCount+1) //如果i大于元素个数-1,输出错误并返回
{
cout<<"ERROR!"<<endl;
return 0;
}
int j=0;
Node *Temp_List;
Temp_List->date=NodeElem; //赋值给date
while(PList==0&&j>i-2){PList=PList->next;j++;}
if(PList->next==0){
Temp_List=PList->next;
PList->next=0;
}
Temp_List->next=PList->next;
PList->next=Temp_List;
ElemCount++;
}
void List::Display_HT()
{
cout<<"Display from first one"<<endl;
for(int i=0;i<ElemCount;i++){
cout<<PList->date<<" ";
PList=PList->next;
}
cout<<endl;
}
List::~List()
{
Clear();
}
void List::Clear()
{
Node *Temp_Delete=PList;
if(Temp_Delete==0)
return;
while(Temp_Delete!=0)
{
Node *TEMP_Delete=Temp_Delete;
Temp_Delete=Temp_Delete->next;
delete TEMP_Delete;
}
}
void main()
{
List List1;
List1.Build_IN(1,6);
List1.Build_IN(1,8);
List1.Build_IN(2,7);
List1.Display_HT();
}
以上是我不明白的部分....
1.....PList=new Node这句有没必要???????
2.....这里是不是每次调用时PList都不同???????怎么才能每次调用该函数时PLsit都为同一个值,即PList指向地址1,之后PList变了,但是第二次调用该函数时,PList也是指向地址1...
3总之,怎么改才对,帮一下手.谢谢....