Function pointer Template

Hello, I am learning C++ so please excuse any errors.

I have to code the following search function for a Linked List template.

1
2
3

Node<NODEDATA>* find(const idx NODEDATA&, bool (*found)(const NODEDATA&, const NODEDATA&));

- idx is reference to what we are looking for in the linked list
- found returns true if second argument matches the first, else false. (i dont need to write the code for this, just need to call it to compare a value in a node)
- returns address of the node or NULL if not if not
- if what we are searching for is found, list is manipulated in a certain way (which I know how to do...).

I just dont get this requirement...if anyone can provide some direction on what I actually have to do, it would be very much appreciated.

Thanks.
Last edited on
Walk each element in the linked list in order, beginning at head. For each element you traverse, call found()
to compare that element to the one passed into the function. If they are equal, return the address of the
node of the element you just compared. If not, continue on to the next node. If you hit the end of the list
and still haven't found the item, then return NULL.
Something like:
1
2
3
4
5
6
7
8
9
10
11
12

 while (cursorNode->next)
  {
         if(found(cursornode->data(),idx)
          {   
               //manipulate the found node location in list......
               return cursornode
           } 
    
 }
return NULL


am I on the right track...?
That is very close.

You are not continuing on to the next node.

You are also missing a few semicolons.
oh ya cursornode->nxtptr in the while loop. and semi-colons after the returns..

Thanks!! Will try to implement it soon..
Topic archived. No new replies allowed.