Results 1 to 2 of 2

Thread: Recursive search C++ [Linked List]

  1. #1
    Join Date
    May 2006
    Posts
    666
    Rep Power
    0

    Default Recursive search C++ [Linked List]

    okay, so i was asked to do a recursive search on a singly linked list. Problem is the function works to search only one node, the first node in the list and display it, but crashes on searching any other than the first. Please take a look at my code.

    void linkedList::recursiveSearch(int key)
    {
    node* temp = start_ptr;
    if(temp == NULL)
    cout<<"No list to search from, Please try again"<<endl;


    else if(temp->getData.empId == key)
    { cout<<"Info for this ID# is as follows: "<<endl;
    cout << "First Name : " << temp->getData.firstName << endl;
    cout << "Last Name : " << temp->getData.lastName << endl;
    cout << "Employee ID : " << temp->getData.empId << endl;
    cout << "Employment type: " << temp->getData.empType<<" time" << endl;
    cout << "Salary :$" << temp->getData.salary << endl;
    cout << endl;
    }
    else if(temp->getData.empId != key)
    {
    recursiveSearch(key);temp = temp->nxt;
    }

    }
    and this is what i used to call the function simple case.

    case 6:
    system("cls");
    int key;
    cout<<"enter key id record to search for: "<<endl;
    cin>>key;
    linky.recursiveSearch(key);
    break;

    OK CORRECTED IT WITHOUT YA'LL HELP -.- anyways where is the solution lol...

    changes to Cpp file
    case 5:
    int id;
    cout<<"enter key id record to search for: "<<endl;
    cin>>id;
    caller.rSearchy(caller.getStart(), id, 0);
    break;

    changes to the header file

    void linker::rSearchy(node* n, int id, int count)
    {

    if(count == 0);
    node* temp = n;

    if(temp == NULL)

    cout<<"This is an empty list, Please Store some data in it first."<<endl;


    else if(temp->data.ID == id)

    {
    cout<<"The Information for the searched ID# is:"<<endl;
    cout << "Employee ID : " << temp->data.ID << endl;
    cout << "First Name : " << temp->data.firstname << endl;
    cout << "Last Name : " << temp->data.lastname << endl;
    cout << "Employment type: " << temp->data.employType<<" time" << endl;
    cout << "Salary :$" << temp->data.salary << endl;
    cout << endl;


    }

    else if(n->data.ID != id)
    {
    count = count + 1;

    temp = temp->next;

    rSearchy(temp, id, count);
    }

    }
    and added also in the header file

    node* linker::getStart()
    {
    return head;
    }
    and in the prototype

    node* getStart();
    Last edited by BoneZ; Apr 28, 2010 at 12:36 AM.
    ---

  2. #2
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default good

    glad you noticed on your own that you need an internal method to implement this recursively .

    your methods are doing too much, its named search but it search and print. your search should (ideally) return the record for the given id, either return null if it is missing or throw.

    in the driver file:
    Code:
    case 5:
       int id;
       cout<<"enter key id record to search for: "<<endl;
       cin>>id;
       node = caller.Search( id); 
    
       if(node == 0) cout << "Employee not forund for id :" << id;
       else node->data.Display();
       break;
    changes to the implementation file
    Code:
    Node* LinkedList::Search(int id)
    {
       Node * start = getStart();
       return internalSearch(start, id);
    }
    
    Node* LinkedList::internalSearch(Node* n, int id)
    { 
       if(n == 0) return 0;
       else if(n->data.id == id) return n;
       else return internalSearch(n->next, id);
    }
    
    void Data::Display()
    {
    cout << "Employee ID : " << ID << endl;
    cout << "First Name : " << firstname << endl;
    cout << "Last Name : " << lastname << endl;
    cout << "Employment type: " << employType<<" time" << endl;
    cout << "Salary :$" << salary << endl;
    cout << endl; 
    }
    and added also in the header file
    Code:
    Node* LinkedList::getStart()
    {
       return head;
    }
    and in the prototype

    Code:
    public:
    Node* getStart();
    Node* Search(int);
    
    private:
    Node* internalSearch(Node *node, int id);
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •