Destroy linked list function

Hi I'm still quite new to c++ and I'm trying to create a linked list with a few functions, the first one is to add people to the list and the second one is the print the list and I've gotten them to work properly. It's the third one that is not functioning correctly.

When I put in a few names into the list and print them it works as intended, but when I remove the nodes and then put in new nodes and try to print I get a trying to read / write protected memory error with VS 2010. I'm doing all this during run time.

I bet the problem is that I still have something pointing to the old list still but I can't seem to see what is wrong with my current code. Please look it through and see if you can find what I've done wrong.

The program crashes after I've put something in and then call the remove_all() function followed up by the printList() function.


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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <string>
using namespace std;
 
 
struct node
{
    string firstName;
        string lastName;
        int memberNumber;
        struct node* next;
};
struct node* head = NULL; 
struct node* temp;
 
void add_node(int); 
void printList(); 
void remove_all(); 
 
int main()
{
        int x,counter = 1;
        while(true)
        {
                cout << "\n1. Add people to the list.\n"
                         << "2. Remove people from the list.\n"  
                         << "3. Print list.\n"
                         << "0. Quit program. \n" << endl;
                cin >> x; //
 
                if(x==0)        
                        break;
                else if(x == 1)
                {
                        add_node(counter);
                        counter++;
                }
                else if(x == 2)
                {
                        counter = 1;
                        remove_all();
                }
                else if(x == 3)
                        printList();
        }
 
 
    return 0;
}
 
 
// Add person
void add_node(int vCounter)
{
        node* current;
        current = new node;
 
        current->next = head;
        head = current;
                
        cout << "Enter first name: ";
        cin >> current->firstName;
        cout << "Enter last name: ";
        cin >> current->lastName; 
        current->memberNumber = vCounter;
}
 
// Print list
void printList()
{
        node* current = head;
 
                while(current != NULL)
                {
                        if(current == NULL)
                                break;  
 
                        else if(current != NULL)
                        {
                                cout << "# " <<current->memberNumber <<" - "<< current->firstName << " " << current->lastName << endl;
                                current = current->next;
                        }
                }
}
 
//Remove everything
void remove_all()
{
        node* current = head;
 
        while(current != NULL)
        {
                if(current == NULL)
                        break;
                else
                {
                        temp = current;
                        current = current->next;
                        delete temp;
                }
        }
        
}
Last edited on
You have to set head to NULL in remove_all().
Ah thanks, such an easy error. Have been trying alot of different things for a few hours now :P
Topic archived. No new replies allowed.