Summing the nodes of a linked list

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):

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
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.

Thank you all
Last edited on
You have a void printlist() method.
Change that to int sumlist() where, instead of outputting values you add to a cumulative sum.

The traversal of the list would be just the same.


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
#include <iostream>
using namespace std;

template <class T> class List
{
   class Node
   {
   public:
      T data;
      Node *next = nullptr;
      Node( T x ) : data( x ) {}
   };

   Node *head = nullptr;
   Node *tail = nullptr;

public:
   List() {}

   void append( T val )
   {
      Node *n = new Node( val );
      if ( !head ) head       = n;
      else         tail->next = n;  
      tail = n;                               
   }

   void print( ostream &out = cout )
   {
      for ( Node *n = head; n; n = n->next ) out << n->data << " ";
   }

   int sum()
   {
      int total = 0;
      for ( Node *n = head; n; n = n->next ) total += n->data;
      return total;
   }
};

//======================================================================

int main()
{
   List<int> L;
   for ( int i : { 2, 3, 5, 7, 11 } ) L.append( i );
   L.print();   cout << '\n';
   cout << "Sum is " << L.sum() << '\n';
}

Last edited on
Thank you lastchance,

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


In other words it does nothing. Any ideas why?
Last edited on
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.)

Beyond that, we would need to see runnable code.
Last edited on
Thank you last chance,

I finally found the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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!
Last edited on
Topic archived. No new replies allowed.