I am creating C program to add up the values in the nodes in Linked List by traversing using while loop.
I have coded the following:
#include <stdio.h>
int main (void)
{
struct entry
{
int value;
struct entry *next;
};
struct entry n1, n2, n3;
struct entry *list_pointer = &n1;
int sum;
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n3.next = (struct entry *) 0; // Mark list end with null pointer
while ( list_pointer != (struct entry *) 0 ) {
sum += list_pointer->value;
list_pointer = list_pointer->next;
}
printf ("%i\n", sum);
return 0;
}
However I am getting the following output:
33367
Instead of getting 600 as the output