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. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    5

    Exclamation 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()
    {
    
        list_pointer *head;
        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;
    
       printf( " %d \n", sum_list(head);
        
    
    }
    Im sortof new to C, can anyone help me debug this.....

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    Just fix the function main. Go by the comments

    Code:
    int main(void)
    {
    
    /* Dont need head */
        list_pointer *head;
    /* 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. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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 head = &a;
    	printf( " %d \n", sum_list(head));
    }
    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. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    int main()
    {
    
        //list_pointer *head;
        list_pointer head=NULL;
        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;
    
       head=&a;
    
       printf( " %d \n", sum_list(head));
        
        return 0;
    }
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 07:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21