C++链表简单问题

发布于 2024-11-08 03:47:32 字数 1233 浏览 5 评论 0原文

我正在尝试检查给定链表中是否存在实体。这是我的代码:

bool LinkedList::existByID(int ID)
{
//create node to search through the list
Node * helpNode;

//start it at the top of the list
helpNode = head;    

if (head == NULL)
{
    return false;
}


//while the item has not yet been found


while ((helpNode->data->indicatedEntity->getID() != ID)  && (helpNode->data != NULL))
     {

    if (helpNode->data->indicatedEntity->getID() == ID)
    {
        //return true - the data exists
        return true;
    }

    else
        //if the data has not been found, move on
        helpNode=helpNode->next;
     }

//if the data has not been found and the end of the 
//list has been reached, return false - the item does
//not exist
return false;
}

从我标记为“问题行”的行,if 语句的部分,

(helpNode->data != NULL)

我收到错误 CXX0017(未找到符号“”)和错误 CXX0030(无法计算表达式)。

如果链表中没有实体,换句话说,如果头为空,则此代码有效。

Node 构造函数看起来像这样:

LinkedList::Node::Node()
{  
next=NULL;
data=NULL;
} 

我还尝试使用以下行:

(helpNode != NULL)

和 Node 构造函数

LinkedList::Node::Node(){}

所有组合都会返回相同的错误。有什么建议吗?

I'm trying to check if an entity exists in a given linkedlist. This is my code:

bool LinkedList::existByID(int ID)
{
//create node to search through the list
Node * helpNode;

//start it at the top of the list
helpNode = head;    

if (head == NULL)
{
    return false;
}


//while the item has not yet been found


while ((helpNode->data->indicatedEntity->getID() != ID)  && (helpNode->data != NULL))
     {

    if (helpNode->data->indicatedEntity->getID() == ID)
    {
        //return true - the data exists
        return true;
    }

    else
        //if the data has not been found, move on
        helpNode=helpNode->next;
     }

//if the data has not been found and the end of the 
//list has been reached, return false - the item does
//not exist
return false;
}

From the line I marked as the "problem line", the part of the if statement

(helpNode->data != NULL)

I get error CXX0017 (symbol "" not found) and error CXX0030 (expression cannot be evaluated).

This code works if there are no entities in the linkedlist - in other words, if the head is null.

The Node constructor looks like this:

LinkedList::Node::Node()
{  
next=NULL;
data=NULL;
} 

I've also tried it with the line:

(helpNode != NULL)

and Node constructor

LinkedList::Node::Node(){}

All combinations return the same errors. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-11-15 03:47:32

首先,我建议用您的代码修复一些问题。

在循环中,您在测试之前检查 helpNodedata 成员,以查看 helpNode 是否确实有效。想象一下,您位于最后一个节点 - 在下面执行的末尾 - 现在在顶部检查什么?

helpNode=helpNode->next;

其次,一旦您检查了 helpNode,接下来您应该在检查 data 的属性之前检查 data 是否有效,如果 数据NULL

现在想想你的循环正在检查什么,它正在检查 getID() != ID,但在循环内你正在测试 ID, getID () == ID?这有道理吗?

我建议在循环中,您只需检查下一个节点和 data 是否存在,然后在循环中检查 ID 是否匹配,如果为 true,则返回。

Firstly I recommend fixing a few things with your code.

In your loop you check the data member of helpNode before testing to see if helpNode is actually valid. Imagine you are on the last node - and at the end of the while the following executes - now what gets checked at the top?

helpNode=helpNode->next;

Secondly, once you've checked for helpNode, next you should check that data is valid before checking attributes of data, what if data is NULL?

And now think about what your loop is checking, it's checking that getID() != ID, and yet inside the loop you are testing for the ID, getID() == ID? does that make sense?

I recommend that in your loop, you just check that the next node and data exists, and then within the loop check that the ID matches, and return if true.

前事休说 2024-11-15 03:47:32

那么行

while ((helpNode->data->indicatedEntity->getID() != ID) && (helpNode->data != NULL))

可能是如果 data 为 NULL,则会出现问题,因为那么您将尝试进一步访问 NULL->indicatedEntity

如果 indicatesEntity 为 NULL,那么您将尝试访问 NULL->getID()

您可以重写它到

while (helpNode->data != NULL && helpNode->data->indicatedEntity != NULL && helpNode->data->indicatedEntity->getID() != ID)

这看起来不太好,但它确实确保你的指针在尝试访问它们之前不为空。

Well the line

while ((helpNode->data->indicatedEntity->getID() != ID) && (helpNode->data != NULL))

might be a problem if data is NULL, because then you would be trying to access NULL->indicatedEntity

further if indicatedEntity is NULL, then you are trying to access NULL->getID()

you can rewrite it to

while (helpNode->data != NULL && helpNode->data->indicatedEntity != NULL && helpNode->data->indicatedEntity->getID() != ID)

which doesnt look nice, but it does ensure your pointers are not null before trying to access them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文