Find all matching nodes in linked list.

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.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    #include "List.h"
    
    void extra(list &);
    
    /***********************************
     * Main
     * Test function - DO NOT CHANGE
     ***********************************/ 
    void main()
    {
      list a;
      extra(a);
    }
    
    /***********************************
     * Extra Credit
     * Test function - DO NOT CHANGE
     ***********************************/ 
    
    void extra(list &a)
    { int i,n;
      node_ptr map[4];
      string first,last;
    
    // Find node
    
      cout << endl;
      cout << "Enter First and Last name: ";
      cin >> first >> last;
      n = a.findall(first,last,map,4);
     // Display forwards
    
      cout << endl;
      cout << "Find List\n--------------\n";
      for (i = 0; i < n; i++)
      {
    	  map[i]->put(cout);
      }
    }


List.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    #include "Node.h"
    #include <iostream>
    #include <string>
    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;
    };


Node.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
    #include <string>
    using namespace std;
    
    
    class list;
    
    class node
    { friend list;
      public:
        node();                           // Null constructor
    	~node();                          // Destructor 
    
        void put(ostream &out);           // Put
        
      private:
        string first,last;
        int age;
        node *next;
    };
    
    typedef node * node_ptr;

List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "List.h"
    #include <iostream>
    #include <string>
    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->next;
    		delete t;
    	}
    
    	head = nullptr;
    }
    
    /**
    * Locate node
    *
    */
    
    node *list::find(string last, string first) 
    {
    	node *temp = head;
    	while (temp)
    	{
    		if (temp->first == first && temp->last == last) return temp;
    		temp = temp->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)->next;
    		*map = find(first, last);
    
    	}
    	return ans;
    }


Node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "Node.h"
    #include <iostream>
    #include <string>
    #include <iomanip>
    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 &out)
    { out << setw(14) << left << last << setw(14) << first << setw(10) <<   age << endl; 
    }

I really appreciate your help. Thank you.
You have an infinite loop in list::findall.
Topic archived. No new replies allowed.