2

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?

6
  • I suppose this is homework? If so, please mark it as such (so that the answers can give you a nudge into the right direction rather than telling you what to do). If not, use std::list. Commented May 20, 2011 at 7:23
  • Please post exact error message. I assume its a runtime error... Commented May 20, 2011 at 7:25
  • 2
    I already see at least one problem - while condition must be rewritten as (helpNode->data != NULL)&&(helpNode->data->indicatedEntity->getID() != ID) You must check that the pointer is not null before dereferencing it. Commented May 20, 2011 at 7:26
  • @sbi - yes, it's a very small part (but unfortunately very important) of a much bigger project. I'll be sure to mark it next time @Muxecoid - thanks, @Nim said the same thing and I fixed it Commented May 20, 2011 at 7:46
  • 1
    @BIU: What do you mean, "yes"? Is it homework? (If the purpose is not to teach you the inner workings of linked lists, then why do you write your own? Use std::list!) Commented May 20, 2011 at 7:58

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! You're right, it didn't really make sense the way I was doing it.
0

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.

3 Comments

You forgot one major check in your rewritten expression.
are you referring to helpNode?
@Nim in the context of their original code, and my statement "well the line..." you can see that they already check the state of helpNode prior to the line with the while loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.