为什么每当我尝试运行此链接列表删除功能时都会收到分段错误错误?
Why am I getting a segmentation fault error whenever I'm trying to run this LinkedList delete function?
我正在尝试创建一个程序来删除链表第 N 位的节点。根据输出,它应该是:
乔治 贝蒂 费利克斯 蕾 妮 乔治 贝蒂 费利克斯 乔治 费利克斯 菲利克斯
在 repl.it(我的提交网站(上运行时,它显示我遇到了分段错误。但是,当我在CodeBlocks上从我的个人计算机上运行它时,它运行没有错误,但是,它只输出第一行,即George
,Betty
,Felix
和Renee
,而不会删除和重新输出。
这是我的代码:
#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
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)