what's the problem

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
#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;}
	~linklist(void);
	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;

	
}


can anyone teach me how to destory the list?
Topic archived. No new replies allowed.