C++ Recursive Search

Posted on 2007-08-07
Last Modified: 2013-11-27
Greetings experts,
I am currently helping my nephew with a C++ class, though my C++ is marginal at best, and I must admint I'm a bit stumped on a question he has for an assingment. The question is to write a recursive sequential search function for a linked list and an array. The thing that is troubling me is that the functions prototype is to only pass to values, an int for the value to search for and a bool item to mark the search as true/false. So the prototype looks like..
void ItemType::Look(int val, bool& gotit)
I'm stumped to code this recursively..
P.S. I plan to help him not give the answer to him.. HA HA
Question by:PIXADMIN
    LVL 11

    Accepted Solution

    An item in a list has a pointer to the next item, e.g.

    class ItemType
      ItemType* m_pNext;
      int m_Value;

    The function will be something like:

    void ItemType::Look(int val, bool& gotit)
      if (m_Value == val)
        gotit = true;

      // here comes recursion:
      if (m_pNext)
        return m_pNext->Look(val, gotit):

    In an array, the pointer to next item can be retrieved as follows:

    ItemType* pNext = this+1;

    But, without additional information, in plain C++ you cannot know where the array ends.
    LVL 11

    Expert Comment

    If the author does not object, my answer should be considered as a solution.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
    With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now