Beginners linked list

Hey guys. I made a linked list. I would like to do threw the entire list and add all the data. Any ideas?
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
#include <iostream>
#include <string>
using namespace std;

struct node
{
    int data;
    node * link;
};


int main()
{
    int total;
    node * head;
    head = new node;
    head -> data = 5;
    head -> link = new node;
    head -> link -> data = 6;
    head -> link -> link = new node;
    head -> link -> link -> data = 7;
    head -> link -> link -> link = NULL;
    
    cout << head -> data << endl;
    cout << head -> link -> data << endl;
    cout << head -> link -> link -> data << endl;
    
    while(link! == NULL)
    {
        total+= data;
        cout << "Total is: " << total;
    }
    
    system("pause");
    return 0;
}
1
2
3
4
5
6
7
8
node* Node=head;
while(Node)
{
total+=Node->data;
Node=Node->link;
}

cout<<"Total is"<<total;


I suggest you read up on how to properly create a linked list.

Why do you need line 1 and line 5? Can you explain to me what they are saying to the compiler?
Topic archived. No new replies allowed.