title: Find all matching nodes in linked list c++
body: <p>I'm writing a function to find all occurrences of a node in a linked list, the function will return the number of occurrences to the main function which will then display those occurrences. The program does compile but the it just freezes and nothing seems to happen when I enter the correct name to look for, if I enter the wrong name, which is not in the list, the findall function returns 0 and the rest of the program works fine. Please take a look. </p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  #include "List.h"
  
  void extra(list &amp;);
  
  /***********************************
   * Main
   * Test function - DO NOT CHANGE
   ***********************************/ 
  void main()
  {
    list a;
    extra(a);
  }
  
  /***********************************
   * Extra Credit
   * Test function - DO NOT CHANGE
   ***********************************/ 
  
  void extra(list &amp;a)
  { int i,n;
    node_ptr map[4];
    string first,last;
  
  // Find node
  
    cout &lt;&lt; endl;
    cout &lt;&lt; "Enter First and Last name: ";
    cin &gt;&gt; first &gt;&gt; last;
    n = a.findall(first,last,map,4);
   // Display forwards
  
    cout &lt;&lt; endl;
    cout &lt;&lt; "Find List\n--------------\n";
    for (i = 0; i &lt; n; i++)
    {
        map[i]-&gt;put(cout);
    }
  }
  </code></pre>
  
  <p>List.h</p>
  
  <pre><code>#include "Node.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  class list 
  {  public:
          list();                                             // Empty constructor
          ~list();                                            // Destructor
  
          int findall(string, string, node_ptr*, int);
  
          node *find(string, string);                         // Locate a note
  
  
      private:
          node *head;
  };
  </code></pre>
  
  <p>Node.h</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  
  class list;
  
  class node
  { friend list;
    public:
      node();                           // Null constructor
      ~node();                          // Destructor 
  
      void put(ostream &amp;out);           // Put
  
    private:
      string first,last;
      int age;
      node *next;
  };
  
  typedef node * node_ptr;
  </code></pre>
  
  <p>List.cpp</p>
  
  <pre><code>#include "List.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  /**
  * Empty Constructor
  *
  */
  
  list::list()
  {
      head = nullptr;
  }
  
  /**
  * Destructor Constructor
  *
  */
  
  list::~list()
  {  if (head == nullptr) return;
  
      node *p = head, *t;
  
      while (p) 
      {
          t = p;
          p = p-&gt;next;
          delete t;
      }
  
      head = nullptr;
  }
  
  /**
  * Locate node
  *
  */
  
  node *list::find(string last, string first) 
  {
      node *temp = head;
      while (temp)
      {
          if (temp-&gt;first == first &amp;&amp; temp-&gt;last == last) return temp;
          temp = temp-&gt;next;
      }
      return nullptr;
  }
  
  
  /**
  * Find all.
  *
  */
  
  int list::findall(string first, string last, node_ptr* map, int n)
  {
      int ans;
      ans = 0;
      *map = find(first, last);
      while (*map != NULL)
      {
          ans++;
          *map = (*map)-&gt;next;
          *map = find(first, last);
  
      }
      return ans;
  }
  </code></pre>
  
  <p>Node.cpp</p>
  
  <pre><code>#include "Node.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  /**
  * Empty Constructor
  * 
  */
  
  node::node()
  {
      last  = "";
      first = "";
      age   = 0;
      next  = nullptr;
  }
  
  
  /**
  * Destructor
  *
  */
  
  node::~node()
  { if (next != nullptr) next = nullptr; 
  }
  
  /**
  * Put
  *
  */
  
  void node::put(ostream &amp;out)
  { out &lt;&lt; setw(14) &lt;&lt; left &lt;&lt; last &lt;&lt; setw(14) &lt;&lt; first &lt;&lt; setw(10) &lt;&lt;   age &lt;&lt; endl; 
  }
  </code></pre>
  
  <p>I really appreciate your help. Thank you.</p>