# Help with linked lists in C???

This is a discussion on Help with linked lists in C??? within the C Programming forums, part of the General Programming Boards category; Im having trouble writing a linked list that and a simple summation of the nodes.... Code: #include <stdio.h> typedef struct ...

1. ## Help with linked lists in C???

Im having trouble writing a linked list that and a simple summation of the nodes....

Code:
```#include <stdio.h>
typedef struct Node {
int val;
struct Node *next;
} *list_pointer;

/* sum_list - Sum the elements of a linked list */
int sum_list(list_pointer ls)
{
int val = 0;
while (ls) {
val += ls->val;
ls = ls->next;
}
return val;
}

int main()
{

Node a;
Node b;
Node c;
Node d;

a.val = 1;
b.val = 3;
c.val = 5;
d.val = 9;

a.next = &b;
b.next = &c;
c.next = &d;

}```
Im sortof new to C, can anyone help me debug this.....

2. Just fix the function main. Go by the comments

Code:
```int main(void)
{

/* Not just "Node" but "struct Node" */
struct Node a;
struct Node b;
struct Node c;
struct Node d;

a.val = 1;
b.val = 3;
c.val = 5;
d.val = 9;

a.next = &b;
b.next = &c;
c.next = &d;
/* Close the list with null */
d.next = NULL;

printf( " %d \n", sum_list(&a));

printf("Press enter to end ....\n");
getchar();
return(0);
}```

3. Code:
```int main()
{
Node a;
Node b;
Node c;
Node d;

a.val = 1;
b.val = 3;
c.val = 5;
d.val = 9;

a.next = &b;
b.next = &c;
c.next = &d;
d.next = NULL;

}```
list_pointer is already a pointer. It should be initialized to the first element of the list. d.next should be set to NULL so that we know where the end is.

4. Code:
```int main()
{

Node a;
Node b;
Node c;
Node d;

a.val = 1;
b.val = 3;
c.val = 5;
d.val = 9;

a.next = &b;
b.next = &c;
c.next = &d;
d.next=NULL;