Linked List Issues

Hi guys, I am opening up this thread because I am having some issues with my Linked List, especially after I delete some nodes....
If I delete a node, and later on I insert some new ones at the beggining of the list, the destructor does not destroyes all my nodes...

Could some good soul helping me out?

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <iostream>

using namespace std;

class Node
{
public:
	int data;
	string name;
	Node* head;
	Node* next;
	Node* last;
	~Node();
	Node() : head(NULL), next(NULL), last(NULL) {;}
	Node(int dt, string nm) : data(dt), name(nm) {;}
	void insertFirst(int dt, string nm)
	{
		Node* tmp = new Node(dt,nm);
		tmp->next = head;
		if(head == NULL)
			last = tmp;
		head = tmp;
	}

	void insertLast(int dt, string nm)
	{
		Node* tmp = new Node(dt, nm);
		if(last == NULL)
			head = last = tmp;

		/* tmp->next = last->next
		 * last = tmp;
		 */
		else
		{
			last->next = tmp;
			last = tmp;
		}


	}

	void removeFirst()
	{
		/* ptr set to head which is pointing to first node*/
		Node* ptr = head;
		if(head == NULL)	/* empty list */
		{
			cout << "\nThe list is empty!!\n";
			return;
		}
		else if(head == last)	/* just one node */
			head = last = NULL;

		else
		{
			/* I update head to point to the next node */
			head = head->next;
			cout << "\nRemoving Node[" << ptr->data << ", " << ptr->name << "]\n";
			delete ptr;
		}

	}
	void removeLast()
	{
		Node* curr = head;
		Node* prev;
		if(head == NULL)
		{
			cout << "\nThe List is empty!\n";
			return;
		}

		while(curr != last)
		{
			prev = curr;
			curr = curr->next;
		}

		prev->next = last->next;
		last = prev;

		cout << "\nRemoving Node[" << curr->data << ", " << curr->name << "]\n";

		delete curr;
	}

	Node* find(int key)
	{
		Node* ptr = head;
		while(ptr->data != key)
		{
			if(ptr->next == NULL)
				return NULL;	/* key not found! */
			else
				ptr = ptr->next;
		}
		return ptr;
	}

	void show() const
	{
		Node* ptr = head;

		if(head == NULL)
			cout << "\nThe list is empty!" << endl;

		cout << "head --> ";
		while(ptr != NULL)
		{
			cout << "[" << ptr->data << ", " << ptr->name << "] --> ";
			ptr= ptr->next;
		}
		cout << "NULL" << endl;
	}


};

int main()
{
	Node n;

	cout << "\nThe List contains: " << endl;

	n.show();
	cout << endl;

	cout << "\nCalling InsertFist function: " << endl;
	cout << "\nThe List contains: " << endl;

	n.insertFirst(1, "Giuseppe");
	n.insertFirst(2, "Piero");
	n.insertFirst(3, "Matteo");
	n.insertFirst(4, "Esterina");
	n.insertFirst(5, "Putinino");
	n.insertFirst(6, "Barbara");
	n.show();

	cout << "\nCalling InsertLast function: " << endl;
	n.insertLast(-1, "1");
	n.insertLast(-2, "2");
	n.insertLast(-3, "3");
	n.insertLast(-4, "4");
	n.insertLast(-5, "5");
	n.insertLast(-6, "6");
	n.insertLast(-7, "7");
	n.insertLast(-8, "8");
	n.insertLast(-9, "9");
	n.insertLast(-10, "10");
	n.insertLast(-11, "11");
	cout << "\nThe List contains: " << endl;
	n.show();

	cout << "\nCalling RemoveFirst function: " << endl;
	n.removeFirst();
	n.show();
	cout << "\nCalling RemoveLast function: ";
	n.removeLast();
	n.show();

//      cout << "\nCalling InsertFist function: " << endl;
//	n.insertFirst(100, "Giuseppe");
//	n.insertFirst(200, "Piero");
//	n.insertFirst(300, "Matteo");
//	n.insertFirst(400, "Esterina");
//	n.insertFirst(500, "Putinino");
//	n.insertFirst(600, "Barbara");
//	cout << "\nThe List contains: " << endl;
//	n.show();


	cout << "\n\n*************Cleaning Memory********************\n";
	return 0;
}

Node::~Node()
{
	Node* curr = head;
	Node* prev;

	while(curr != NULL)
	{
		prev = curr;
		cout << "\nDestroying node [" <<prev->data << ", "
				<< prev->name << "]\n";
		curr = curr->next;
		delete prev;
	}
}


If I do not insert new Nodes, it works
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
The List contains: 

The list is empty!
head --> NULL


Calling InsertFist function: 

The List contains: 
head --> [6, Barbara] --> [5, Putinino] --> [4, Esterina] --> [3, Matteo] --> [2, Piero] --> [1, Giuseppe] --> NULL

Calling InsertLast function: 

The List contains: 
head --> [6, Barbara] --> [5, Putinino] --> [4, Esterina] --> [3, Matteo] --> [2, Piero] --> [1, Giuseppe] --> [-1, 1] --> [-2, 2] --> [-3, 3] --> [-4, 4] --> [-5, 5] --> [-6, 6] --> [-7, 7] --> [-8, 8] --> [-9, 9] --> [-10, 10] --> [-11, 11] --> NULL

Calling RemoveFirst function: 

Removing Node[6, Barbara]
head --> [5, Putinino] --> [4, Esterina] --> [3, Matteo] --> [2, Piero] --> [1, Giuseppe] --> [-1, 1] --> [-2, 2] --> [-3, 3] --> [-4, 4] --> [-5, 5] --> [-6, 6] --> [-7, 7] --> [-8, 8] --> [-9, 9] --> [-10, 10] --> [-11, 11] --> NULL

Calling RemoveLast function: 
Removing Node[-11, 11]
head --> [5, Putinino] --> [4, Esterina] --> [3, Matteo] --> [2, Piero] --> [1, Giuseppe] --> [-1, 1] --> [-2, 2] --> [-3, 3] --> [-4, 4] --> [-5, 5] --> [-6, 6] --> [-7, 7] --> [-8, 8] --> [-9, 9] --> [-10, 10] --> NULL


*************Cleaning Memory********************

Destroying node [5, Putinino]

Destroying node [4, Esterina]

Destroying node [3, Matteo]

Destroying node [2, Piero]

Destroying node [1, Giuseppe]

Destroying node [-1, 1]

Destroying node [-2, 2]

Destroying node [-3, 3]

Destroying node [-4, 4]

Destroying node [-5, 5]

Destroying node [-6, 6]

Destroying node [-7, 7]

Destroying node [-8, 8]

Destroying node [-9, 9]

Destroying node [-10, 10]


But if I add some Nodes... This is what I get :(
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

The List contains: 

The list is empty!
head --> NULL


Calling InsertFist function: 

The List contains: 
head --> [6, Barbara] --> [5, Putinino] --> [4, Esterina] --> [3, Matteo] --> [2, Piero] --> [1, Giuseppe] --> NULL

Calling InsertLast function: 

The List contains: 
head --> [6, Barbara] --> [5, Putinino] --> [4, Esterina] --> [3, Matteo] --> [2, Piero] --> [1, Giuseppe] --> [-1, 1] --> [-2, 2] --> [-3, 3] --> [-4, 4] --> [-5, 5] --> [-6, 6] --> [-7, 7] --> [-8, 8] --> [-9, 9] --> [-10, 10] --> [-11, 11] --> NULL

Calling RemoveFirst function: 

Removing Node[6, Barbara]
head --> [5, Putinino] --> [4, Esterina] --> [3, Matteo] --> [2, Piero] --> [1, Giuseppe] --> [-1, 1] --> [-2, 2] --> [-3, 3] --> [-4, 4] --> [-5, 5] --> [-6, 6] --> [-7, 7] --> [-8, 8] --> [-9, 9] --> [-10, 10] --> [-11, 11] --> NULL

Calling RemoveLast function: 
Removing Node[-11, 11]
head --> [5, Putinino] --> [4, Esterina] --> [3, Matteo] --> [2, Piero] --> [1, Giuseppe] --> [-1, 1] --> [-2, 2] --> [-3, 3] --> [-4, 4] --> [-5, 5] --> [-6, 6] --> [-7, 7] --> [-8, 8] --> [-9, 9] --> [-10, 10] --> NULL

Calling InsertFist function: 

The List contains: 
head --> [600, Barbara] --> [500, Putinino] --> [400, Esterina] --> [300, Matteo] --> [200, Piero] --> [100, Giuseppe] --> [5, Putinino] --> [4, Esterina] --> [3, Matteo] --> [2, Piero] --> [1, Giuseppe] --> [-1, 1] --> [-2, 2] --> [-3, 3] --> [-4, 4] --> [-5, 5] --> [-6, 6] --> [-7, 7] --> [-8, 8] --> [-9, 9] --> [-10, 10] --> NULL


*************Cleaning Memory********************

Destroying node [600, Barbara]

Destroying node [500, Putinino]

Destroying node [400, Esterina]

Destroying node [300, Matteo]

Destroying node [200, Piero]


Bye guys and thank you very much.
Last edited on
The problem is head and last in your Node class. Since each node is supposed to know this, you need to update each node when head or last last changes. Otherwise you will get a mess. I'd recommend create another class for head and last
Topic archived. No new replies allowed.