为什么每当我尝试运行此链接列表删除功能时都会收到分段错误错误?

Why am I getting a segmentation fault error whenever I'm trying to run this LinkedList delete function?

本文关键字:错误 分段 功能 删除 试运行 列表 链接 为什么      更新时间:2023-10-16

我正在尝试创建一个程序来删除链表第 N 位的节点。根据输出,它应该是:

乔治 贝蒂 费利克斯 蕾 妮 乔治 贝蒂 费利克斯 乔治 费利克斯 菲利克斯

在 repl.it(我的提交网站(上运行时,它显示我遇到了分段错误。但是,当我在CodeBlocks上从我的个人计算机上运行它时,它运行没有错误,但是,它只输出第一行,即GeorgeBettyFelixRenee,而不会删除和重新输出。

这是我的代码:

#include <iostream>
#include <string>
using namespace std;
class Node {
public:
string name;
Node* next;
};
Node* head;
class LinkedList {
public:
LinkedList();
~LinkedList();
void push(string);
void output();
void remove(int);
private:
Node *first;
};

void LinkedList::remove(int n)
{
struct Node* temp1 = head;
if(n == 1)
{
head = temp1 -> next;
delete(temp1);
return;
}
int i = 0;
for(i = 0; i < n - 2; i++)
{
temp1 = temp1 -> next;
}
struct Node* temp2 = temp1 -> next;
temp1 -> next = temp2 -> next;
delete(temp2);
}

LinkedList::LinkedList()
{
first = NULL;
}
LinkedList::~LinkedList()
{
Node *current=first;
while(current!=NULL)
{
Node *ptr=current;
current = current->next;
delete(ptr);
}
}
void LinkedList::push(string data)
{
Node *temp;
temp = new Node;
(*temp).name = data;
(*temp).next = first;
first = temp;
}
void LinkedList::output()
{
Node *current = first;
while(current!=NULL)
{
cout << (*current).name << endl;
current = (*current).next;
}
cout << endl;
}
int main() {
LinkedList students;
students.push("Renee");
students.push("Felix");
students.push("Betty");
students.push("George");

students.output();
students.remove(3);
students.output();
students.remove(1);
students.output();
students.remove(0);
students.output();

}

LinkedList::remove之外的所有代码都通过first成员变量管理列表。但LinkedList::remove引用head,一个可疑的未使用的全局变量。我相信这根本不应该出现在代码中。

删除全局head,并将LinkedList::remove更改为:

void LinkedList::remove(int n)
{
Node **pp = &first;
while (*pp && n-- > 1)
pp = &(*pp)->next;
if (*pp)
{
Node *tmp = *pp;
*pp = tmp->next;
delete tmp;
}
}

对于初学者来说,目前还不清楚这个声明是什么

Node* head;

在程序中做。

Node应在类LinkedList中声明,并且列表的用户应无法访问。

在C++指数从0开始。因此,如果您将列表中的索引也从 0 开始计算会更好。函数 remove 的参数n的类型应具有无符号整数类型,例如类型size_t

您没有在函数中检查当前指针是否等于 nullptr。因此,该函数具有未定义的行为。

此外,该函数引用全局变量head而不是数据成员first

struct Node* temp1 = head;

似乎您复制并粘贴了某个地方(在互联网中(单链表的 C 代码,蚂蚁试图更新它。

如果使用指针指向指针,函数的代码可以非常简单。例如

void LinkedList::remove( size_t n )
{
Node **current = &first;
while ( n-- != 0 && *current != nullptr )
{
current = &( *current )->next;
}
if ( *current != nullptr )
{
Node *tmp = *current;
*current = ( *current )->next;
delete tmp;
}
}

传递给函数的索引从 0 开始,因为它应该在 C++ 程序中。

这是一个演示程序。

#include <iostream>
#include <string>
#include <functional>
class LinkedList 
{
public:
LinkedList() = default;
LinkedList( const LinkedList & ) = delete;
LinkedList & operator =( const LinkedList & ) = delete;
~LinkedList()
{
while ( head )
{
delete std::exchange( head, head->next );
}           
}
void push( const std::string &s )
{
head = new Node { s, head };
}
bool remove( size_t n )
{
Node **current = &head;
while ( n-- != 0 && *current != nullptr )
{
current = &( *current )->next;
}
bool success = *current != nullptr;
if ( success )
{
delete std::exchange( *current, ( *current )->next );
}
return success;
}
friend std::ostream & operator <<( std::ostream &os, const LinkedList &list )
{
for ( Node *current = list.head; current != nullptr; current = current->next )
{
os << current->name << " -> ";
}
return os << "null";
}
private:
struct Node
{
std::string name;
Node* next;
} *head = nullptr;
};
int main() 
{
LinkedList students;
students.push("Renee");
students.push("Felix");
students.push("Betty");
students.push("George");
std::cout << students << 'n';
students.remove(3);
std::cout << students << 'n';
students.remove(1);
std::cout << students << 'n';
students.remove(0);
std::cout << students << 'n';
return 0;
}

它的输出是

George -> Betty -> Felix -> Renee -> null
George -> Betty -> Felix -> null
George -> Felix -> null
Felix -> null