有序链表错误



我知道所有的代码都不起作用,但是当我第一次运行程序并且从文本字段读取第一个字符串时,程序出错了。主函数是在实现中将字符串传递给"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

相关内容

  • 没有找到相关文章

最新更新