【问题标题】:Copying a Linked List crashes the Program复制链接列表会使程序崩溃
【发布时间】:2014-04-21 03:46:52
【问题描述】:

我有链接列表代码,Copy_List 函数使程序崩溃并且无法工作:它没有给出语法错误,所以它是合乎逻辑的。我不确定确切的问题出在哪里,因此将不胜感激。

代码如下:

#include <iostream>
using namespace std;

struct node{
   int info;
   node *link;
};


class Linked_List{
   private :

   int count;
   node *first;
   node *last;
   node *current;

   public:


   Linked_List() {

      count=0;
      first=NULL;
      last=NULL;
   }

   void Initialize_List(){

      cout<<"Enter Number OF Nodes"<<endl;
      cin>>count;

      first=last=current=new node;

      for(int i =0;i<count;i++){
         cout<<"Enter New Node Info :"<<endl;
         cin>>current->info;
         last->link=current;
         last=current;
         current=new node;
      }

      last->link=NULL;
   }

   bool Is_Empty(){
      if(first==NULL)
      {
         return true;
      }
      else{
         return false;
      }
   }

   int Front () {
      if (first != NULL)

      return first-> info; 
      else return 0;
   }

   int Back () {

      if (last != NULL)
      return last-> info;
      else return 0; 
   }

   void Insert_Last(int x){

      count++;
      current=new node;
      current->info=x;

      last->link=current;

      last=current;
      last->link=NULL;

      if(first==NULL)

      first=current;
   }

   void Delete_First(){

      if(!Is_Empty())  // Or if(first==NULL)
      { 

         node *p;
         p=first;
         first=first->link;
         delete p;
         count --;
         if(count==0)
            first=last=NULL;
      }
   }

  friend void Copy_List (Linked_List &n,Linked_List &m);

};

void Copy_List (Linked_List &n,Linked_List &m){

   Linked_List temp;
   while(!n.Is_Empty()){
      temp.Insert_Last(n.Front());
      n.Delete_First(); 
   }
   while (!temp.Is_Empty()) {

      n.Insert_Last(temp.Front());
      temp.Delete_First(); 
   }
}


void main (){
   Linked_List obj,obj2;
   cout<<"Is the list empty ?"<<"  "<<boolalpha<<obj.Is_Empty(); cout<<endl;
   obj.Initialize_List();
   cout<<"Is the list empty ?"<<"  "<<boolalpha<<obj.Is_Empty(); cout<<endl;
   Copy_List (obj,obj2);   
}

【问题讨论】:

  • Copy_List 中设置一些调试点,看看它在哪里崩溃。然后告诉我们哪一行,以及场景是什么。此外,在您调试时,一些打印语句可能对您有用..
  • Linked_List &amp;m 的意义何在?你没有使用它..但我假设你认为你是。
  • 没关系;但你没有这样做——甚至没有尝试这样做。 m 在您的 Copy_List 方法中不存在。
  • 我不能用其他任何东西来评判你。我不认识你,也没有读过你的任何作品。如果。你。不能。调试。你。不能。写。程式。真的就是这么简单。告诉你的教授/助教,要求你在没有任何调试辅助或技能的情况下“编写程序”只是浪费每个人的时间,包括你的。
  • 此外,获得高分的学生不太可能只是提供从 SO 这样的网站提供的修复程序中复制的代码。他们将学习如何编写程序,并主动学习如何调试它们以使它们工作。不这样做的学生也被淘汰了。

标签: c++ linked-list copy nodes


【解决方案1】:

改进建议:

  • node添加一个默认构造函数,以便在构造时正确初始化。

    struct node{
        node(int in = 0) : info(in), link(NULL) {}
       int info;
       node *link;
    };
    
  • 您不需要 current 作为 Linked_List 的成员。它仅在某些函数中作为函数变量有用。

  • 使用Insert_Last 实现Initialize_List()。这使功能保持清洁。它还避免了冗余代码。

    void Initialize_List(){
      cout<<"Enter Number OF Nodes"<<endl;
      int num;
      cin>>num;
    
      for(int i =0;i<num;i++){
         cout<<"Enter New Node Info :"<<endl;
         int info;
         cin >> info;
         this->Insert_Last(info);
      }
    }
    
  • Insert_Last 假设哪个是有效指针,哪个不是有效指针,如果您从 Initialize_List 开始使用 to,这将是不正确的。可以简化为:

    void Insert_Last(int x){
      count++;
      node* current=new node;
      current->info=x;
    
      if ( first == NULL )
      {
         first = last = current;
      }
      else
      {
         last->link=current;
         last=current;
      }
    }
    
  • 您发布的Copy_List 的实现删除了第一个参数中的所有项目并将它们放在第二个参数中。我不确定这就是目的。如果您想保持第一个参数的内容不变并且想将其内容复制到第二个参数,则需要另一种方法。这是我想出的:

    void Copy_List (Linked_List &n,Linked_List &m){
    
       Linked_List temp;
       node* current = n.first;
       for ( ; current != NULL; current = current->link )
       {
          temp.Insert_Last(current->info);
       }
    
       current = temp.first;
       for ( ; current != NULL; current = current->link )
       {
          m.Insert_Last(current->info);
       }
    }
    
  • Linked_List 中没有析构函数。编译器提供的默认实现不会释放类分配的内存。为了释放类分配的内存,你需要实现一个析构函数。

    ~Linked_List() {
      node* current=first;
      while ( current != NULL )
      {
         node* temp = current->link;
         delete current;
         current = temp;
      }
    }
    

【讨论】:

  • 非常感谢!我刚刚编辑了我的 Insert_Last 并且它起作用了! :D 感谢您解决了我 2 小时的痛苦问题!
【解决方案2】:

在新列表中last指针首先没有被初始化:

void Insert_Last(int x) {
   ...
   last->link=current;   // for the new List last should be initialized 
   last = current;
   last->link=NULL;

假设更改 - 删除行 last-&gt;link=current;

   last = current;
   last->link=NULL; 

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多