dimanche 14 octobre 2018

Sequential Search Function using Recursion return value error

So my professor would like us to design recursive functions for a linked list from this class 'addressBookType' which is derived from 4 other classes. The program basically creates an address book with the person's name, address, date, and relationship, each of those has their own classes.

The recursive functions she wants to make are a print, append, delete, and sequential search.

I've made all of them recursive except for the sequential search function.

Here is the original and the recursive version:

bool addressBookType::seqSearch(extPersonType item) const
{
    bool found = false;
    ListNode *nodePtr; // pointer to traverse the list

    nodePtr = head; // start the search at the first node

    found = seqSearchRecursive(nodePtr, item); // call recursive function

    if(found)
        found = (nodePtr->value == item); // test for equality

    return found;
}

bool addressBookType::seqSearchRecursive(ListNode *nPtr, extPersonType obj) const
{
    if(nPtr == NULL) // return false if value not found
    {
        return false;
    }
    else if(nPtr->value == obj) // return true if object found
    {
        return true;
    }
    else
        seqSearchRecursive(nPtr->next, obj); // call recursive funct with next value
}

The program compiles and runs, but it is not giving me the correct output.

In my testing I deleted an object from the list and searched for it and returned false. Then I searched for an object in the list and it returned false. So something is making the function always come back false.. anyone have any suggestions?

Aucun commentaire:

Enregistrer un commentaire