Thread: search

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    21

    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???

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    That doesn't help much, can you offer more code, such as what type L is and the implementation of Search?

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    21
    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??=)

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    21
    Thank you for those suggestions!!! I got the program up and running!!! Thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM