I am implementing a class Set, and there is member function, ironically called member, which returns true, if the value being passed is in the set, and false otherwise. My question is that, if I am creating a temporary node to iterate over to search for this value, do I need to delete this temporary node when I am done with it to return the memory to the heap, as it is not being returned from the function?
In this class, the node is embedded as a private struct in the class Set like this:
private:
//Precondition: linked list is sorted in ascending order
struct Node {
int value;
Node* link;
};
Node* list;
static Node* cons(int x, Node *p);
and the function I'm referring to is:
bool Set::member(int x) const {
if(list == nullptr)
return false;
Node* tempNode = list;
while(tempNode != nullptr) {
if(tempNode->value > x)
return false;
if(tempNode->value == x)
return true;
tempNode = tempNode->link;
}
return false;
}