有序链表错误
Ordered Linked List errors
我知道所有的代码都不起作用,但是当我第一次运行程序并且从文本字段读取第一个字符串时,程序出错了。主函数是在实现中将字符串传递给"Insert list function"。假定程序在每次从文本文件中读入字符串时插入一个节点。程序调用也调用删除函数,我知道它还没有工作(这就是为什么它被注释掉)。我只是想找到插入函数被调用时创建的错误。main函数有一个while循环,它为每个文本条目创建一个Node,并逐个传递节点,以便按照ABC顺序进行排序。
头文件:
#include <string>
using namespace std;
struct Node
{
string data;
Node * next;
};
class List
{
public:
List();
~List();
bool Insert(string);
bool Delete(string);
void Print();
bool Edit(string, string);
private:
Node * head;
Node * cur;
Node * trailer;
};
实现:#include <iostream>
#include <string>
#include <fstream>
#include "List.h"
using namespace std;
List::List():head(NULL)
{}
List::~List()
{}
bool List::Insert(string data)
{
Node* newNode = new Node;
if (newNode == NULL)
{
cout << "Error: Memory Allocation Failed" << endl;
return false;
}
newNode->data = data;
cur = head;
trailer = NULL;
if (head == NULL)
{
//cout << "head is Null" << endl;
head = newNode;
cout << head -> data << endl;
newNode->next = NULL;
//return true;
}
while (newNode->data > cur->data && cur -> next != NULL)
{
trailer = cur;
cur = cur->next;
}
if (cur->next == NULL)
{
cur->next = newNode;
newNode->next = NULL;
return true;
}
else
{
trailer->next = newNode;
newNode->next = cur;
return true;
}
}
bool List::Delete(string data)
{
/*Node *temp = head->next;
while (head != NULL)
{
delete head;
head = temp;
temp = head->next;
}
return true;*/
}
bool List::Edit(string dataDelete, string dataInsert)
{
Delete(dataDelete);
Insert(dataInsert);
return true;
}
void List::Print()
{
for (Node * Count = head; Count != NULL; Count = Count->next)
{
cout << Count->data << endl;
}
}
@Deepak是对的,问题是当你插入第一个元素时,head
变量是NULL
, cur
被设置为head
的值。
要解决这个问题,你可以简单地放置
cur = head;
trailer = NULL;
条件后if (head == NULL)
{
//cout << "head is Null" << endl;
head = newNode;
cout << head -> data << endl;
newNode->next = NULL;
//return true;
}
当您尝试插入应该放在开头的元素(值小于列表中的任何其他值)时,也会出现错误。当循环条件
trailer = NULL;
while (newNode->data > cur->data && cur -> next != NULL) { ... }
在第一次调用中为false,因此trailer
将是NULL
。要解决这个问题,您需要检查trailer
变量,像这样
if (trailer == NULL) {
newNode->next = head;
head = newNode;
return true;
}
因此,您的Insert
代码将看起来像
bool List::Insert(string data)
{
Node* newNode = new Node;
if (newNode == NULL)
{
cout << "Error: Memory Allocation Failed" << endl;
return false;
}
newNode->data = data;
if (head == NULL)
{
head = newNode;
newNode->next = NULL;
return true;
}
cur = head;
trailer = NULL;
while (newNode->data > cur->data && cur -> next != NULL)
{
trailer = cur;
cur = cur->next;
}
if (trailer == NULL) {
newNode->next = head;
head = newNode;
return true;
}
if (cur->next == NULL)
{
cur->next = newNode;
newNode->next = NULL;
return true;
}
else
{
trailer->next = newNode;
newNode->next = cur;
return true;
}
}
当插入第一个节点时,由于
出现错误while (newNode->data > cur->data && cur -> next != NULL)
在这个时刻值在cur是NULL,你正试图访问cur->data
相关文章:
- 错误:需要类名(链表c++)
- 对单向链表进行排序时出现运行时错误
- 在链表中的第 n 位插入显示分割错误
- 链表错误的中间
- 为什么我在空指针错误(链表)中获取成员访问权限
- 查找显示链表时出现的错误
- 我想在 c++ 中对单向链表进行冒泡排序,但我一直面临左值错误,无法解决它
- 在 c++ 中实现链表时出现分段错误
- 双链表的擦除值函数,未知错误
- 删除链表中的节点 - 分段错误
- 分段错误通用双链表
- C++中链表的错误"Abort signal from abort(3) (sigabrt) "
- 奇怪的C++链表错误
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- 为什么我的C++代码在以下打印链表的代码片段中显示分段错误?
- C++:按数字排序链表错误
- 为什么我在尝试添加两个链表时出现此错误?
- C++ largest_free节点函数段错误链表
- 析构函数分段错误链表
- c++分割错误链表元素添加