Thread: Structs and Linked Lists

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    Structs and Linked Lists

    Hey,

    So, I've been working on this all day and I'm going crazy as to why my linked list isn't actually linking together. What I mean is that when I print it out, it will only print out the node that was last inserted into the list. I have no idea what's going on. Help me please! Below is a snippet of my code..


    Code:
     
    struct Sort_list{
        int null_bool; //if 0, then not null; if 1, then null
        int val1; //int
        struct Sort_list *next;
    };
    
    
    typedef struct Sort_list* sort_list;
    
    
    
    sort_list head, tail, curr;
    
    int Insert(sort_list list, void *newObj){
    
    
        void *p1 = malloc(sizeof(int));
        void *p2 = malloc(sizeof(int));
        int num;
        int value;
        int c = 0;
    
    
        if(list->null_bool == 1){ // if the list is empty
            list->val1 = *(int*)newObj;
            list->null_bool = 0;
            head = list;
        }else{ // if the list is not empty
            list->val1 = *(int*)newObj;
            tail->next = list;
        }
    
    
        tail = list;
        tail->next = NULL;
    
    
        return 0;
    }

    note: I'm also working with void pointers, but I don't think it's really the issue. So, just ignore those parts..

    note:: Tell me if you need more of my code to understand it better.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Here is an example code fragment for a linked list showing the basic structures, one for the nodes and one for the list with pointers to the first and last nodes. If the list is empty, then the pointers to the nodes will be NULL. The list structure could optionally have a count (number of nodes in the list) member.

    Code:
    typedef struct _NODE{                       /* node struct */
        struct _NODE * next;
        int data;
    }NODE;
    
    
    typedef struct _LIST{                       /* list struct */
        NODE * head;
        NODE * tail;
    }LIST;
    Last edited by rcgldr; 10-02-2013 at 09:48 PM.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    I'm not really sure how to change my program around this. Forgive me, I'm rusty with the syntax.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Is there anything wrong though with the initial implementation of my code though?

    For instance:
    When I input 1, 2, and 3 and print out the list. Only 3 would print out..

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by ChickenChowMinh View Post
    Is there anything wrong though with the initial implementation of my code though?
    Your code is so broken and non-standard that's it's hard to say how to fix it without encouraging more bad coding.

    Normally you wouldn't use globals. Also, there's all kinds of unused variables and the bizarre malloc'ing of single integers, which is not at all practical.
    Last edited by oogabooga; 10-02-2013 at 10:16 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by ChickenChowMinh View Post
    Is there anything wrong though with the initial implementation of my code though?
    The insert function is updating a copy of list, as opposed to updating list itself. There's no need for a null boolean variable, much less in every instance of a node, since an empty list is indicated when head and tail are NULL.

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    When you grab a piece of paper and a pencil and draw your list on paper, does it look correct?

  8. #8
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by rcgldr View Post
    The insert function is updating a copy of list, as opposed to updating list itself. There's no need for a null boolean variable, much less in every instance of a node, since an empty list is indicated when head and tail are NULL.
    I prefer a dummy node, but *shrug*

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    How do I go about updating the list itself instead of the copy? This whole time I was assuming that "List" was actually being updated ><

  10. #10
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by ChickenChowMinh View Post
    How do I go about updating the list itself instead of the copy? This whole time I was assuming that "List" was actually being updated ><
    Draw it on paper, scan your drawing and post it... maybe then it will be a bit more understandable

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    I actually have a paper filled with doodles of different possible linked lists and it seems correct to me. But then again, I've been trying to write this code for hours. So, I maybe off..

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by ChickenChowMinh View Post
    I actually have a paper filled with doodles of different possible linked lists and it seems correct to me. But then again, I've been trying to write this code for hours. So, I maybe off..
    Well, the code doesn't matter all that much. The drawing (that I hope you will draw) is quite clear and implementing the code from the drawing really should be fairly straightforward

  13. #13
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    I reiterate my drawings into my code, but I think my issue is syntax. I'm kind of uncomfortable with passing structs and such.

    The only thing that's a downer is that my class doesn't allow us to change the functions in any way. So, it pretty much prevents me from coding freely the way I actually like to code. But, I'll work on it a bit more and update later!

  14. #14
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by ChickenChowMinh View Post
    I reiterate my drawings into my code, but I think my issue is syntax. I'm kind of uncomfortable with passing structs and such.

    The only thing that's a downer is that my class doesn't allow us to change the functions in any way. So, it pretty much prevents me from coding freely the way I actually like to code. But, I'll work on it a bit more and update later!
    What has your class given you?


    • struct Sort_list (Y/N)
    • typedef struct Sort_list* sort_list; (Y/N)
    • int Insert(sort_list list, void *newObj); (Y/N)
    • sort_list head, tail, curr; // Globals (Y/N)

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by ChickenChowMinh View Post
    I'm kind of uncomfortable with passing structs and such. The only thing that's a downer is that my class doesn't allow us to change the functions in any way.
    The code isn't passing structs, the parameters for the insert function are a pointer to a node struct, and a pointer to a new object. So I assume that the node struct is allocated elsewhere. The insert function should just set list->data to the integer value from *newObj. There's no reason to allocate any memory in the insert function. The declaration for the global variables, head, tail, and curr, should initialize them to NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-24-2012, 10:05 AM
  2. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  3. with linked lists and structs
    By cable in forum C Programming
    Replies: 4
    Last Post: 10-10-2011, 08:11 PM
  4. Replies: 3
    Last Post: 01-22-2002, 12:22 AM
  5. file i/o-structs,linked lists
    By new2c in forum C++ Programming
    Replies: 3
    Last Post: 12-16-2001, 11:31 PM