单链表的清除
/*while(head->next!=NULL){p1=head->next;
head=head->next;
delete p1;
}*/
Node<T>*p1=head->next;
while(p1!=NULL){
Node<T>*p2=p1;
p1=p1->next;
delete p2;}
如上面的代码所示,其中:
head是指向头结点的指针, 为什么下面那个代码进行清除之后,用显示操作还会显示出来,而上面的代码却可以显示空表? ?
#include <iostream>
using namespace std;
//Node
template <class T>
class Node{
public:
T data;
Node<T> *next;
};
//linkedList
template <class T>
class linkedList{
private:
Node<T> *head, *tail;
Node<T> *getPos(int p){ //get the position of the number p node
if(p==0) return head;
int i=1;
Node<T> *tmp= head->next ;
while(i<p && tmp!=NULL){
i++;
tmp= tmp->next;
}
return tmp;
}
public:
linkedList(){
head = tail = new Node<T>;
head -> next = NULL;
}
~linkedList(){
Node<T> *tmp;
while(head!=NULL){
tmp= head;
head = head -> next;
delete tmp;
}
}
//functions
//1.append
bool append(T value){
Node<T>* p1= new Node<T>;
p1->data = value;
p1->next = NULL;
tail ->next = p1;
tail = p1;
return true;
}
//2.length
int length(){
int i=0;
Node<T>* p= head->next ;
if(p==NULL) {
cout<<"empty list!"<<endl;
return 0;}
else{
while(p!=NULL){
i++;
p=p->next;
}
}
return i;
}
//3.display
bool display(){
Node<T>* p=head->next;
if(p==NULL)cout <<"empty list"<<endl;
while(p!=NULL){
cout <<p->data<< ' ';
p= p->next;
}
cout <<endl;
return true;
}
//4.insert
bool insert(int p,T value ){
if(p<0 || p>length()) {
cout<<"illegal insert!"<<endl;
return false;}
Node<T>* a= new Node<T>;
a->data= value;
Node<T> *b,*c;
b=getPos(p-1);
c=getPos(p);
b->next = a;
a->next = c;
return true;
}
//5.clear
bool clear(){
Node<T>* p1= head->next;
if(p1==NULL){
cout<<"already empty!\n"<<endl;
}
else {
/*while(head->next!=NULL){
p1=head->next;
head=head->next;
delete p1;
}*/
while(p1!=NULL){
Node<T>*p2=p1;
p1=p1->next;
delete p2;}
cout<<"clear success!\n"<<endl;
}
return true;
}
//6.
};
int main(){
linkedList <int> LL;
int choice,p,value;
bool ok;
do{
cout<<"单链表程序(0.sign out,1.append,2.length,3.display,4.insert,5.clear,6删除,7查找,8修改,9定位),请选择:"<<endl;
cin>>choice;
switch(choice){
case 0:
cout<< "sign out !"<<endl;
break;
case 1:
cout<<"please input the value:";
cin>>value;
ok=LL.append(value);
if(ok==false) cout<<"append error!"<<endl;
else cout<<"append success!"<<endl;
break;
case 2:
p=LL.length();
cout<<"the length is:"<<p<<endl;
break;
case 3:
LL.display();
break;
case 4:
cout<<"please input the position:";
cin>>p;
cout<<"please input the value:";
cin>>value;
ok=LL.insert(p,value);
if(ok==true)cout << "insert success!\n";
else cout<<"insert error!\n";
break;
case 5:
LL.clear();
break;
}
}while(choice!=0);
return 0;
} 本帖最后由 jhq999 于 2022-4-19 08:31 编辑
第一个是错误的
while(head!=NULL){/////////
p1=head;///////////
head=head->next;
delete p1;
}
如果需要保留head可以这么写
Node<T>*p1=head->next;
head->next=NULL;//////////////
while(p1!=NULL){
Node<T>*p2=p1;
p1=p1->next;
delete p2;}
jhq999 发表于 2022-4-19 08:29
第一个是错误的
请问错在哪了,完整代码的析构函数也是这样子来写的哦 本帖最后由 jhq999 于 2022-4-19 15:47 编辑
waixiong 发表于 2022-4-19 12:36
请问错在哪了,完整代码的析构函数也是这样子来写的哦
while(head->next!=NULL){
p1=head->next;
head=head->next;
delete p1;///////////////////////////此时p1=head,释放p1也就是把现在的head释放了
}
页:
[1]