I am reposting this question as a new thread here since it was previously in another thread and not easily visible. I have a singly linked list and I want to add the nodes of the list. My linked list code is the following (in summary, not in detail):
class sllist1 {
public:
node* head;
sllist1();
sllist1(node* n);
node* nodeExists(string k);
void appendnode(node* n);
void prependnode(node* n);
void insertnodeafter(string k, node* n);
void deletenode(string k);
void updatenode(string k, int d);
void printlist();
void savetofile1();
}
int main(int argc, char* argv)
{
system("title ..........");
system("color 17");
int option;
static sllist1 sll;
do {
int choose;
string key1, k1;
int data1;
do {
cout << "\nSelect:" << endl;
cout << "0. Return to initial menu" << endl;
cout << "1. Append node " << endl;
cout << "2. Prepend node" << endl;
cout << "3. Insert node" << endl;
cout << "4. Delete node" << endl;
cout << "5. Update node" << endl;
cout << "6. Print" << endl;
cout << "7. Clscr" << endl;
cout << "8. sum nodes" << endl;
cin >> choose;
node* n1 = new node();
switch (choose) {
case 0:
break;
case 1:
cin >> key1;
cin >> data1;
n1->key = key1;
n1->data = data1;
sll.appendnode(n1);
break;
case 2:
cin >> key1;
cin >> data1;
n1->key = key1;
n1->data = data1;
sll.prependnode(n1);
break;
case 3:
cin >> k1;
cin >> key1;
cin >> data1;
n1->key = key1;
n1->data = data1;
sll.insertnodeafter(k1, n1);
break;
case 4:
cin >> k1;
sll.deletenode(k1);
break;
case 5:
cin >> key1;
cin >> data1;
sll.updatenode(key1, data1);
break;
case 6:
sll.printlist();
break;
case 7:
system("cls");
break;
case 8:
//Here is where I want to apply the node summing
break;
default:
cout << "Wrong number! Enter number 1-8!" << endl;
break;
}
} while (choose != 0);
return 0;
}
My question is the following: When I have the list built how could I pass as argument the head node and the method will then sum all of the rest of the list's nodes, in the menu's case 8? I have such a method but when I do pass the head node it does not go on with the rest of them it just gives me only the first node.
I followed your tip with modifying my print method which I already had, and I ended up to the following method (something which I did at the very beginning before searching the net for other codes):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int sumnodes1() {
int sum = 0;
if (head == NULL) {
cout << "No Number of nodes in the list ";
}
else {
node* temp = head;
while (temp != NULL) {
sum+= temp->data;
temp = temp->next;
}
return sum;
}
}
However, the problem is still present and when I try to sum the nodes it gives me the following:
1 2 3 4
Sum of data is:
application path (process 16324) exited with code -1073741819
press key to continue
Well, lines 9 and 10 are the wrong way round for starters. (Edit: OP has since changed his/her post to do this.)
Also, it may not compile if your compiler doesn't like the idea of returning nothing (in the case that head == NULL).
And your function shouldn't have any arguments (particularly since you have created a new local variable with the same name). (Edit: OP has changed their post to fix this.)
double sumnodes() {
double sum = 0;
if (head == NULL) {
cout << "No Number of nodes in the list ";
}
else {
node* temp = head;
while (temp != NULL) {
sum += temp->data;
temp = temp->next;
}
return sum;
}
}
is ok, I just forgot to print the value returned with the cout. Thanks a lot again!