Thread: add node in a list

  1. #1
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246

    add node in a list

    i'm having troubles with this, it compiles fine, but doesn't printf anything, it seems that no nodes are added to the list at all
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node 
    {
      int dat;
      struct node *next;
    };
    typedef struct node* link;
    
    void addnode(link *list, int d);
    void delall(link *list);
    int count(link list);
    
    int main(void)
    {
      link root = NULL, index; 
      int x;
    
      for (x = 0; x < 5; x++)
        addnode(&root, x);
      
      for (index = root; index != NULL; index = index->next)
        printf("%d ", index->dat);
    
      putchar('\n');
    
      printf("%d nodes\n", count(root) );
    
      delall(&root);
    
      return 0;
    }
    
    
    void addnode(link *list, int d)
    {
      link new;
      link cur = *list;
      new = malloc( sizeof(*new) );
      if (new == NULL)
        exit(1);
      else
        new->dat = d;
    
      while (cur != NULL)
        cur = cur->next;
    
      cur = new;
      new->next = NULL;
    
    }
    
    void delall(link *list)
    {
      link cur = *list, sav;
      while (cur != NULL)
      {
        sav = cur->next;
        free(cur);
        cur = sav;
      }
    }
    
    int count(link list)
    {
      link cur = list;
      int c = 0;
      while (cur != NULL)
      {
        cur = cur->next;
        c++;
      }
    
      return c;
    }

  2. #2
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    problem is on this loop
    Code:
    while (cur != NULL)
        cur = cur->next;
    this runs as long cur is null and when you are at list end
    previous node->next wont point any allocated mem. area.
    so assigning new to cur don't have any effects to cur previous
    node->next. it changes only cur to point new.

    run loop as long cur->next is not null
    and assigning
    cur->next = new;
    cur = cur->next.

  3. #3
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Code:
    void addnode(link *list, int d)
    {
      link new;
      link cur = *list;
      new = malloc( sizeof(*new) );
      if (new == NULL)
        exit(1);
      else
        new->dat = d;
    
      while (cur->next != NULL)
        cur = cur->next;
    
      cur->next = new;
      cur = cur->next;
    
    }
    Segfault

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    I've tried this also, no segfault, but the output is still '0 nodes'.
    Code:
    void addnode(link *list, int d)
    {
      link new;
      link cur = *list;
      new = malloc( sizeof(*new) );
      if (new == NULL)
        exit(1);
      else
        new->dat = d;
    
      if (cur == NULL)
      {
        cur = new;
        cur->next = NULL;
      }
      else
      {
    
        while (cur->next != NULL)
          cur = cur->next;
    
        cur->next = new;
        new->next = NULL;
      }
    
    }

  5. #5
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    allocate memory to root before passing it to addnode.

    ... or modify addnode to do it before accessing it elements.
    Last edited by tjohnsson; 06-16-2004 at 06:23 AM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Some variation of the following is what you want:
    Code:
    void addnode(link *list, int d)
    {
      link new = malloc( sizeof(*new) );
    
      if (new == NULL)
        exit(1);
      new->dat = d;
      new->next = NULL;
      if (*list == NULL) {
        *list = new;
      }
      else {
        link cur = *list;
        while (cur != NULL && cur->next != NULL)
          cur = cur->next;
        cur->next = new;
      }
    }
    >allocate memory to root before passing it to addnode.
    That's naturally one way to go about it, but I get the impression that viaxd wants the function to accept an empty list (signified by a null pointer for the root node) and treat it accordingly.
    My best code is written with the delete key.

  7. #7
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    it works perfectly!! thanks Prelude
    however, I don't understand where my mistake was and why was it printing 0 nodes?

    That's naturally one way to go about it, but I get the impression that viaxd wants the function to accept an empty list (signified by a null pointer for the root node) and treat it accordingly.
    Yes, exactly what i wanted, i believe that is called the head pointer, null tail linked list type (i'm so confused in all these list types right now )

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't understand where my mistake was and why was it printing 0 nodes?
    You never modified the list in main. By assigning *list to cur when *list is a null pointer, you guarantee that any modification of cur won't be mirrored by *list. That's why a null root must be handled as a special case. But even if that were fixed, you would still only have one item in the list because your search for NULL walks off the end of the list. The way you get there is different, but the problem is the same. You can't add to a linked list when all you have is a null pointer.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me debug this linked list !
    By Dark Angel in forum C Programming
    Replies: 6
    Last Post: 04-18-2008, 02:10 PM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  3. Help here with qsort!
    By xxxrugby in forum C Programming
    Replies: 11
    Last Post: 02-09-2005, 08:52 AM
  4. Dynamic list of Objects in External File
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2002, 02:05 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM