复制链接列表会使程序崩溃
Copying a Linked List crashes the Program
我有链表代码,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);
}
改进建议:
-
向
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; } }
在新列表中,last
指针未首先初始化:
void Insert_Last(int x) {
...
last->link=current; // for the new List last should be initialized
last = current;
last->link=NULL;
假定更改-删除线路last->link=current;
last = current;
last->link=NULL;
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 试图创建流或fopen时程序崩溃
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- DLL Made with CMake 使程序崩溃
- 程序崩溃使用boost::asio
- 调用 free() 有时会导致程序崩溃
- 邮件加密程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- For 循环在尝试读取数组 c++ 时程序崩溃
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 无法访问的代码如何导致我的程序崩溃?
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- QCompleter set模型使应用程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 删除SDL_PollEvent时程序崩溃