简体   繁体   English

复制链接列表会使程序崩溃

[英]Copying a Linked List crashes the Program

I have Linked List code, the Copy_List function crashes the program and won't work: it gives no syntax error so it's logical. 我有链表代码, Copy_List函数使程序崩溃,无法正常工作:它没有语法错误,因此很合逻辑。 I'm not sure where the exact problem lies, so any help would be appreciated. 我不确定确切的问题在哪里,所以任何帮助将不胜感激。

Here is the code: 这是代码:

#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);   
}

In the new list last pointer is not initialized firstly: 在新列表中,未首先初始化last指针:

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

Supposed change - remove the line last->link=current; 可能的更改-删除行last->link=current;

   last = current;
   last->link=NULL; 

Suggestions for improvement: 改进建议:

  • Add a default constructor to node so that it gets initialized properly when constructed. node添加默认构造函数,以便在构造时可以正确初始化它。

     struct node{ node(int in = 0) : info(in), link(NULL) {} int info; node *link; }; 
  • You don't need current as a member of Linked_List . 您不需要current作为Linked_List的成员。 It's useful only in some functions as a function varible. 它仅在某些函数中作为函数变量有用。

  • Implement Initialize_List() using Insert_Last . 使用Insert_Last实现Initialize_List() That keeps the function cleaner. 这样可以使功能更清洁。 It also avoids redundant code. 它还避免了冗余代码。

     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 had assumptions about which is a valid pointer and which is not, which won't be true if you started using to from Initialize_List . Insert_Last假设哪些是有效指针,哪些不是,如果您从Initialize_List开始使用to,则将不成立。 It can be simplified 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; } } 
  • Implementation of Copy_List you posted deleted all items from the first argument and put them in the second argument. 您发布的Copy_List实现删除了第一个参数中的所有项目,并将它们放在第二个参数中。 I am not sure that was the purpose. 我不确定这是目的。 If you want to keep the contents of the first argument unchanged and want to copy its contents to the second argument, an alternate method is necessary. 如果要保持第一个参数的内容不变并且要将其内容复制到第二个参数,则需要一个备用方法。 Here's what I came up with: 这是我想出的:

     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); } } 
  • You don't have a destructor in Linked_List . 您在Linked_List没有析构Linked_List The default implementation provided by the compiler won't release the memory allocated by the class. 编译器提供的默认实现不会释放该类分配的内存。 In order to deallocate the memory allocated by the class, you need to implement a destructor. 为了取消分配由该类分配的内存,您需要实现一个析构函数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM