C - problem with insert in linked list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WarmDismalAgony
    New Member
    • Mar 2008
    • 4

    C - problem with insert in linked list

    hey, can anyone figure out why this insert function is setting all nodes in the list to have the same item?
    Code:
    void insert_list (list_ref list, list_item item) {
        listnode_ref new = malloc (sizeof (struct listnode));
        assert (is_list (list));
        assert (new != NULL);
        new->tag = listnode_tag;
        new->item = item;
        if (list->head == NULL) {
           list->head = new;
           list->last = new;
           list->curr = new;
        }else{
           new->prev = list->last;
           list->last->next = new;
           list->last = new;
           list->curr = new;
        };
    }
    i cant figure it out for the life of me. other than that problem it seems to be working fine - it creates a new node and sticks it on the list, and it appears that the links work, too. any help would be GREATLY appreciated.. i've been trying to figure this out for like 2 hours -_- i think i must be missing something obvious, but i have no idea what. i know for sure the problem has to be in this function, though (after a LOT of debug prints to pinpoint it).
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    I dont know how you have defined your doubly linked list.
    But the logic would be

    If head == NULL
    head = tail = newnode;
    else
    tail->next=newnode ;
    newnode->prev = tail;
    tail=newnode



    Raghuram

    Comment

    • WarmDismalAgony
      New Member
      • Mar 2008
      • 4

      #3
      yeah thats exactly what my code is except for tail->next=newnode and newnode>prev=ta il are switched (which shouldnt make a difference right?)

      so.. i dont see how it can be this function thats the problem.. yet i stuck a print statement for the item right before the insert function is called, and a debug function that shows all the items right after the insert function is called...

      the print statement shows different items being called in and the debug shows all the items being set to whatever the most recent one was (and a new node is added every time).

      any ideas?

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by WarmDismalAgony
        yeah thats exactly what my code is except for tail->next=newnode and newnode>prev=ta il are switched (which shouldnt make a difference right?)

        so.. i dont see how it can be this function thats the problem.. yet i stuck a print statement for the item right before the insert function is called, and a debug function that shows all the items right after the insert function is called...

        the print statement shows different items being called in and the debug shows all the items being set to whatever the most recent one was (and a new node is added every time).

        any ideas?
        I can understand other lines except this
        What does this line do?
        list->last->next = new;

        I think issue is with this line

        Raghuram

        Comment

        • WarmDismalAgony
          New Member
          • Mar 2008
          • 4

          #5
          Originally posted by gpraghuram
          I can understand other lines except this
          What does this line do?
          list->last->next = new;

          I think issue is with this line

          Raghuram

          thats the equivalent of your tail->next=newnode

          Comment

          • WarmDismalAgony
            New Member
            • Mar 2008
            • 4

            #6
            ok the problem was not creating a new space in memory for each item. i'm an idiot. problem solved.

            Comment

            Working...