鱼C论坛

 找回密码
 立即注册
查看: 3478|回复: 3

单链表的清除

[复制链接]
发表于 2022-4-18 23:00:44 | 显示全部楼层 |阅读模式
15鱼币
  1.         /*while(head->next!=NULL){
  2.                                         p1=head->next;
  3.                                         head=head->next;
  4.                                         delete p1;
  5.                                 }*/
  6.                                 Node<T>*p1=head->next;
  7.                                 while(p1!=NULL){
  8.                                         Node<T>*p2=p1;
  9.                                         p1=p1->next;
  10.                                        
  11.                                         delete p2;}
复制代码

如上面的代码所示,其中:
head是指向头结点的指针, 为什么下面那个代码进行清除之后,用显示操作还会显示出来,而上面的代码却可以显示空表? ?
  1. #include <iostream>
  2. using namespace std;


  3. //Node
  4. template <class T>
  5. class Node{
  6.         public:
  7.                 T data;
  8.                 Node<T> *next;
  9. };

  10. //linkedList
  11. template <class T>
  12. class linkedList{
  13.         private:
  14.                 Node<T> *head, *tail;
  15.                
  16.                 Node<T> *getPos(int p){   //get the position of the number p node
  17.                         if(p==0) return head;
  18.                         int i=1;
  19.                         Node<T> *tmp= head->next ;
  20.                         while(i<p && tmp!=NULL){
  21.                                 i++;
  22.                                 tmp= tmp->next;
  23.                         }
  24.                         return tmp;
  25.                 }
  26.         public:
  27.                 linkedList(){
  28.                         head = tail = new Node<T>;
  29.                         head -> next = NULL;
  30.                 }
  31.                
  32.                 ~linkedList(){
  33.                         Node<T> *tmp;
  34.                         while(head!=NULL){
  35.                                 tmp= head;
  36.                                 head = head -> next;
  37.                                 delete tmp;
  38.                         }
  39.                 }
  40.                
  41.                
  42.                 //functions
  43.                 //1.append
  44.                 bool append(T value){
  45.                         Node<T>* p1= new Node<T>;
  46.                         p1->data = value;
  47.                         p1->next = NULL;
  48.                         tail ->next = p1;
  49.                         tail = p1;
  50.                         return true;
  51.                 }
  52.                
  53.                 //2.length
  54.                 int length(){
  55.                         int i=0;
  56.                         Node<T>* p= head->next ;
  57.                         if(p==NULL) {
  58.                         cout<<"empty list!"<<endl;
  59.                         return 0;}
  60.                         else{
  61.                                 while(p!=NULL){
  62.                                         i++;
  63.                                         p=p->next;
  64.                                 }
  65.                         }
  66.                         return i;
  67.                 }
  68.                
  69.                 //3.display
  70.                 bool display(){
  71.                         Node<T>* p=head->next;
  72.                         if(p==NULL)cout <<"empty list"<<endl;
  73.                         while(p!=NULL){
  74.                                 cout <<p->data<< ' ';
  75.                                 p= p->next;
  76.                         }
  77.                         cout <<endl;
  78.                         return true;
  79.                 }
  80.                
  81.                 //4.insert
  82.                 bool insert(int p,T value ){
  83.                         if(p<0 || p>length()) {
  84.                                 cout<<"illegal insert!"<<endl;
  85.                                 return false;}
  86.                         Node<T>* a= new Node<T>;
  87.                         a->data= value;
  88.                         Node<T> *b,*c;
  89.                         b=getPos(p-1);
  90.                         c=getPos(p);
  91.                         b->next = a;
  92.                         a->next = c;
  93.                         return true;
  94.                 }
  95.                
  96.                 //5.clear
  97.                 bool clear(){
  98.                         Node<T>* p1= head->next;
  99.                         if(p1==NULL){
  100.                                 cout<<"already empty!\n"<<endl;
  101.                         }
  102.                         else {
  103.                                
  104.                                 /*while(head->next!=NULL){
  105.                                         p1=head->next;
  106.                                         head=head->next;
  107.                                         delete p1;
  108.                                 }*/
  109.                                 while(p1!=NULL){
  110.                                         Node<T>*p2=p1;
  111.                                         p1=p1->next;
  112.                                        
  113.                                         delete p2;}
  114.                                 cout<<"clear success!\n"<<endl;
  115.                         }
  116.                         return true;
  117.                 }
  118.                 //6.
  119. };
  120. int main(){
  121.         linkedList <int> LL;
  122.         int choice,p,value;
  123.         bool ok;
  124.         do{
  125.                 cout<<"单链表程序(0.sign out,1.append,2.length,3.display,4.insert,5.clear,6删除,7查找,8修改,9定位),请选择:"<<endl;
  126.                 cin>>choice;
  127.                 switch(choice){
  128.                         case 0:
  129.                                 cout<< "sign out !"<<endl;
  130.                                 break;
  131.                         case 1:
  132.                                 cout<<"please input the value:";
  133.                                 cin>>value;
  134.                                 ok=LL.append(value);
  135.                                 if(ok==false) cout<<"append error!"<<endl;
  136.                                 else cout<<"append success!"<<endl;
  137.                                 break;
  138.                         case 2:
  139.                                 p=LL.length();
  140.                                 cout<<"the length is:"<<p<<endl;
  141.                                 break;
  142.                         case 3:
  143.                                 LL.display();
  144.                                 break;
  145.                         case 4:
  146.                                 cout<<"please input the position:";
  147.                                 cin>>p;
  148.                                 cout<<"please input the value:";
  149.                                 cin>>value;
  150.                                 ok=LL.insert(p,value);
  151.                                 if(ok==true)cout << "insert success!\n";
  152.                                 else cout<<"insert error!\n";
  153.                                 break;
  154.                         case 5:
  155.                                 LL.clear();
  156.                                 break;
  157.                 }
  158.         }while(choice!=0);
  159.         return 0;
  160. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-19 08:29:24 | 显示全部楼层
本帖最后由 jhq999 于 2022-4-19 08:31 编辑

第一个是错误的
      

  1.                                while(head!=NULL){/////////
  2.                                         p1=head;///////////
  3.                                         head=head->next;
  4.                                         delete p1;
  5.                                 }
复制代码


如果需要保留head可以这么写

  1.                                 Node<T>*p1=head->next;
  2.                                 head->next=NULL;//////////////
  3.                                 while(p1!=NULL){
  4.                                         Node<T>*p2=p1;
  5.                                         p1=p1->next;
  6.                                        
  7.                                         delete p2;}
  8.                                
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-19 12:36:39 | 显示全部楼层
jhq999 发表于 2022-4-19 08:29
第一个是错误的
        

请问错在哪了,完整代码的析构函数也是这样子来写的哦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-19 15:42:21 | 显示全部楼层
本帖最后由 jhq999 于 2022-4-19 15:47 编辑
waixiong 发表于 2022-4-19 12:36
请问错在哪了,完整代码的析构函数也是这样子来写的哦

  1. while(head->next!=NULL){
  2.         p1=head->next;
  3.         head=head->next;
  4.         delete p1;///////////////////////////此时p1=head,释放p1也就是把现在的head释放了
  5.                   }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 07:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表