有序链表错误

Ordered Linked List errors

我知道所有代码都不起作用,但是当我第一次 运行 程序和第一个字符串从文本字段中读入时,程序错误 out.The 主函数正在传递string to "Insert list function" 在实现中。 该程序假设每次从文本文件中读入一个字符串时插入一个节点。程序调用还调用了删除函数,我知道它还没有工作(这就是它被注释掉的原因)。我只是想找出调用插入函数时产生的错误。 main 函数有一个 while 循环,它为每个文本条目创建一个节点,并将节点一个接一个地传递以按 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;
}
}

插入第一个节点时出现错误,原因是

while (newNode->data > cur->data && cur -> next != NULL)

此时 cur 中的值为 NULL 并且您正在尝试访问 cur->data.

@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;
    }
}