Search through a linked list for the value X

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • APEJMAN
    New Member
    • Feb 2008
    • 33

    Search through a linked list for the value X

    I am trying to write a function to
    find(x) -- Searches through the list for the value x and returns an Iterator pointing to the first node containing value x. If x is not found in the list, it returns an Iterator pointing to NULL. ( I want to use linear search that starts from one end and checks each element one by one.
    so I wrote this code:

    [CODE=cpp]//find function
    template<typena me T>
    void LinkedList<T>:: find(T value)
    {
    Node<T>* pos=first;
    while(pos){
    if (pos.get()==val ue){
    return pos;
    }
    else{
    pos=pos->next;
    }
    }
    return pos=NULL;

    }[/CODE]

    so now lets say in the main part we have

    iter2 = rebelList2.find ("book");

    but my code dosent work
    would you help me?
    thanks
    Last edited by Ganon11; Feb 14 '08, 07:05 PM. Reason: Please use the [CODE] tags provided.
  • APEJMAN
    New Member
    • Feb 2008
    • 33

    #2
    I changed the program to this:

    but still dosent work
    ............... ............... ............... ............... .......
    [CODE=cpp]//find function
    template<typena me T>
    Iterator<T> LinkedList<T>:: find(T value)
    {

    bool Found;
    Iterator<T> iter;
    iter.position=f irst;


    Found = false;

    while ((! Found) && (iter.position! =NULL)){
    if (iter.position->data == value)
    return iter;
    else
    iter.position->next;
    }

    iter.position=f irst->prev;
    return iter ;
    }[/CODE]
    ............... ............... ............... ............... ....
    Last edited by Ganon11; Feb 14 '08, 07:05 PM. Reason: Please use the [CODE] tags provided.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Use the STL list template.

      You have another post with more linked list code that is in trouble. Please stop tortuing yourself, and us.

      Comment

      • APEJMAN
        New Member
        • Feb 2008
        • 33

        #4
        why you people are so RUDE!
        I made a mistake! OKKKKKKKKKKKKKK KKKKKKKKKKKKKKK KKK

        by the way thanks

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by APEJMAN
          why you people are so RUDE!
          I made a mistake! OKKKKKKKKKKKKKK KKKKKKKKKKKKKKK KKK

          by the way thanks
          You know, we find it rude when posters create multiple threads on the same question (is this three on linked lists for you now?), and ask things that can be answered with a simple Google search.

          It's also interesting to me that you get upset when weaknessforcats - the most prolific and knowledgable poster in the C/C++ forum - shows you the correct answer. You became upset when we (weaknessforcat s) helped you. And we (weaknessforcat s) are rude because of it... yeah... hmm...

          Comment

          Working...