C++链表简单问题
我正在尝试检查给定链表中是否存在实体。这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我建议用您的代码修复一些问题。
在循环中,您在测试之前检查
helpNode
的data
成员,以查看helpNode
是否确实有效。想象一下,您位于最后一个节点 - 在下面执行的末尾 - 现在在顶部检查什么?其次,一旦您检查了
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 ofhelpNode
before testing to see ifhelpNode
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?Secondly, once you've checked for
helpNode
, next you should check thatdata
is valid before checking attributes ofdata
, what ifdata
isNULL
?And now think about what your loop is checking, it's checking that
getID() != ID
, and yet inside the loop you are testing for theID
,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 theID
matches, and return if true.那么行
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.