How to destory the link list?

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
#include <iostream>

using namespace std;

const short unsigned int NUMVALS = 10;

struct ListNode {
	int value;
	ListNode* next;
};
ListNode* head = NULL;

bool insertNode(int num);
void displayList(void);
bool apendinglist (int num);

int main(void)
{
	int dumVals[NUMVALS];
	for (int i = 0; i<NUMVALS; i++)
	{
		cout<<"please enter your "<<i+1<<" number";
		cin>>dumVals[i];
	}
	short unsigned int i;
	for(i=0; i<NUMVALS; i++) { // insert the array values into the linked list
		if(insertNode(dumVals[i])) {
			cout << "Error in function insertNode!\n";
			cout << "Program will halt!\n";
			exit(1);
		}
	}

	displayList();
	
}

bool insertNode(int num)
// This function inserts a node into our linked list.
{
	ListNode* newNode;
	ListNode* nodePtr = head;
	ListNode* prevNodePtr = NULL;
	
	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}

	newNode->value = num;
	newNode->next = NULL;

	if(head==NULL) { // check if list is currently empty
		cout << "List is currently empty - " << newNode->value;
		cout << " is part of the list's first node!\n";
		head = newNode;
	}
	else {
		while(nodePtr != NULL && nodePtr->value<num) {
			prevNodePtr = nodePtr;  // advance prevNodePtr up to nodePtr
			nodePtr = nodePtr->next;  // advance nodePtr by 1 node
		}
		if(prevNodePtr == NULL) {// at beginning of list
			newNode->next = head;
			head = newNode;
		}
		else {
			prevNodePtr->next = newNode;
			newNode->next = nodePtr;
		}
	}

	return 0;
}

void displayList(void)
// This function displays our linked list!
{
	ListNode* nodePtr = head;
	

	if(head == NULL) {
		cout << "List is currently empty!\n";
	}
	else {
		while (nodePtr != NULL) {
			cout << nodePtr->value << endl;
			nodePtr = nodePtr->next;
		}
	}
}

void deleteNode(int num)
// This function deletes a node, if it is in face there.
{
	ListNode* nodePtr = head;
	ListNode* prevNodePtr = NULL;
	
	if(head==NULL) { // list is currently empty
		cout << "The list is currently empty - ";
		cout << "There are no nodes to delete!\n";
	}
	else if(nodePtr->value == num) { // node is first one
		head = nodePtr->next;
		delete nodePtr;
	}
	else {
		while(nodePtr != NULL && nodePtr->value != num) {
			prevNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		if(nodePtr == NULL) {
			cout << "Your value (i.e., " << num << ")";
			cout << " was not in the list!\n";
		}
		else {
			prevNodePtr->next = nodePtr->next;
			delete nodePtr;
		}
	}
	
	return;
}

bool apendinglist(int num)
// This function inserts a node into our linked list.
{
	ListNode* newNode;
	ListNode* nodePtr = head;
	ListNode* prevNodePtr = NULL;
	
	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}

	newNode->value = num;
	newNode->next = NULL;

	if(head==NULL) { // check if list is currently empty
		cout << "List is currently empty - " << newNode->value;
		cout << " is part of the list's first node!\n";
		head = newNode;
	}
	else 
	{
		while(nodePtr->next != NULL)
			nodePtr=nodePtr->next;
		nodePtr->next = newNode;
	}
	return 0;

	
}


I am to write a destructor that destory the link list.
~Listnode to destory the list, but i do not have the knowledge to destory the list. how i do that? My approach is to write a for loop on deleting node but i think that's not hte correct approach anyone would like to help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct ListNode {
	ListNode* next;
	...

	//this will delete the whole list by just deleting the head
	// (if you don't want that, then you need to use a different approach like below)
	~ListNode() {
		if( next != NULL )
			delete next;
	}

	//another method
	static void recursiveDelete(ListNode *node) {
		if( node->next != NULL )
			recursiveDelete(node->next);
		delete node;
	}
};
Last edited on
i add it but the "next" is having the red line under it why? i modify my code a little check it 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


#include <iostream>

using namespace std;

const short unsigned int NUMVALS = 10;

class linklist
{
private:
	struct ListNode
	{
		int value;
		ListNode* next;
	};
	ListNode *head;
public:
	linklist(void) {head = NULL;}
	static void recursiveDelete(linklist *node);
	
	bool insertNode(int num);
	void displayList(void);
	bool apendinglist (int num);
	bool deleteNode(int num);
};
int main(void)
{
	linklist mylist;
	int dumVals[NUMVALS];
	for (int i = 0; i<NUMVALS; i++)
	{
		cout<<"please enter your "<<i+1<<" number";
		cin>>dumVals[i];
	}
	short unsigned int i;
	for(i=0; i<NUMVALS; i++) { // insert the array values into the linked list
		if(mylist.insertNode(dumVals[i])) {
			cout << "Error in function insertNode!\n";
			cout << "Program will halt!\n";
			exit(1);
		}
	}

      mylist.displayList();
	
}

bool linklist::insertNode(int num)
// This function inserts a node into our linked list.
{
	ListNode* newNode;
	ListNode* nodePtr = head;
	ListNode* prevNodePtr = NULL;
	
	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}

	newNode->value = num;
	newNode->next = NULL;

	if(head==NULL) { // check if list is currently empty
		cout << "List is currently empty - " << newNode->value;
		cout << " is part of the list's first node!\n";
		head = newNode;
	}
	else {
		while(nodePtr != NULL && nodePtr->value<num) {
			prevNodePtr = nodePtr;  // advance prevNodePtr up to nodePtr
			nodePtr = nodePtr->next;  // advance nodePtr by 1 node
		}
		if(prevNodePtr == NULL) {// at beginning of list
			newNode->next = head;
			head = newNode;
		}
		else {
			prevNodePtr->next = newNode;
			newNode->next = nodePtr;
		}
	}

	return 0;
}

void linklist::displayList(void)
// This function displays our linked list!
{
	ListNode* nodePtr = head;
	

	if(head == NULL) {
		cout << "List is currently empty!\n";
	}
	else {
		while (nodePtr != NULL) {
			cout << nodePtr->value << endl;
			nodePtr = nodePtr->next;
		}
	}
}

bool linklist::deleteNode(int num)
// This function deletes a node, if it is in face there.
{
	ListNode* nodePtr = head;
	ListNode* prevNodePtr = NULL;
	
	if(head==NULL) { // list is currently empty
		cout << "The list is currently empty - ";
		cout << "There are no nodes to delete!\n";
	}
	else if(nodePtr->value == num) { // node is first one
		head = nodePtr->next;
		delete nodePtr;
	}
	else {
		while(nodePtr != NULL && nodePtr->value != num) {
			prevNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}
		if(nodePtr == NULL) {
			cout << "Your value (i.e., " << num << ")";
			cout << " was not in the list!\n";
		}
		else {
			prevNodePtr->next = nodePtr->next;
			delete nodePtr;
		}
	}
	
	return 0;
}

bool linklist::apendinglist(int num)
// This function inserts a node into our linked list.
{
	ListNode* newNode;
	ListNode* nodePtr = head;
	ListNode* prevNodePtr = NULL;
	
	newNode = new ListNode;
	if(newNode == NULL) {
		cout << "Error allocating memory for new list member!\n";
		return 1;
	}

	newNode->value = num;
	newNode->next = NULL;

	if(head==NULL) { // check if list is currently empty
		cout << "List is currently empty - " << newNode->value;
		cout << " is part of the list's first node!\n";
		head = newNode;
	}
	else 
	{
		while(nodePtr->next != NULL)
			nodePtr=nodePtr->next;
		nodePtr->next = newNode;
	}
	return 0;

	
}

void linklist::recursiveDelete(linklist *node)
{
	if( node->next != NULL )
			recursiveDelete(node->next);
		delete node;
}
You want to delete the nodes ListNode *node

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
class linklist
{
private:
	struct ListNode
	{
		...
		ListNode* next;

		//create the method here, as a member of ListNode
		static void recursiveDelete(ListNode *node);
	};
	ListNode *head;
public:
	linklist(void) {head = NULL;}
	~linklist() {
	  //invoke it when you delete the linklist
		if( head != NULL )
			recursiveDelete(head);
	}	
	...
};

//definition
void linklist::ListNode::recursiveDelete(linklist *node)
{
	if( node->next != NULL )
		recursiveDelete(node->next);
	delete node;
}
Last edited on
it's still not letting me do this next,
there are 2 problems in your method
1)line 18, is not valid "recursiveDelete identifier not found"
2)line 24~29 voidlinklist::listnode::recursivedelete(linklist *) overloaded member function not found in linklist::listnode (ignore my cap mistakes). I fix it by changing linklist to ListNode, but i can't fix problem one.
Last edited on
Line 18: ListNode::recursiveDelete(head);
Topic archived. No new replies allowed.