-
search
Ok I have a search function and and search helper function...I need to cout wheter or not the item is found...
What I have is from my main:
L.Search(3);//Looking for those #'s 3 is in my list and 60 isn't
L.Search(60);
if (L.Search() == NULL){
cout << "Not Found" << endl;
}else{
cout << "Found"<< endl;
}
}
return 0;
}
Problem is it only returns Not Found!! It doesn't say that 3 was ever found...any suggestions???
-
That doesn't help much, can you offer more code, such as what type L is and the implementation of Search?
-Prelude
-
Sure I can!! I didn't want to load you up on info...sorry=) I tend to leave out the important stuff??
Node* OrdList ::SearchHelper(Node *r, ItemType item){
if(r->ptr == NULL){
return NULL;
}else{
if (r->ptr->datum == item){
return r->ptr;
}else{
SearchHelper (r->ptr,item);
};
};
}
Node* OrdList ::Search(ItemType item){
return SearchHelper(orderedlist, item);
}
//These are my implementaions for my program...Is that enough??=)
-
believe it or not still more code is needed. Specifically code regarding Node class/struct. My presumption is that code looks something like this:
struct Node
{
Itemtype datum;
Node * ptr;
};
such that ptr will hold the address of the next Node object in the list and you are searching for a node whose Itemtype matches the one passed to SearchHelper. The initial Node * you pass to SearchHelper appears to be the head node. The code in SearchHelper appears to have three options:
//if this is the last node in the list then don't look any further
if(r->ptr == NULL){
return NULL;
}
//if the current node has the desired value
else{
if (r->ptr->datum == item){
return r->ptr;
}
//otherwise send next node to SearchHelper
else{
SearchHelper (r->ptr,item);
Assuming this is the set up then there is one probable coding error and one probable logic error. the apparent coding error is here:
r->ptr->datum
I think it would be better to use this:
r->datum
instead, as r is the current node and r->ptr is the next node in the list.
The logic error is that you should should check for datum == item first. If the last node in the list has the desired value, the sequence you use will miss it.
The return value from the original call to SearchHelper() could be used like this in the original call:
int main
{
OrdList L;
//fill L with Nodes here
//now indicate what to look for
ItemType find = 3;
//then look for it.
//If a non-NULL ptr is returned
if(L.Search(find))
cout << "found" << endl;
//if a NULL ptr was returned
else
cout << "not found" << endl;
//etc
-
Thank you for those suggestions!!! I got the program up and running!!! Thanks!!