Thread: How to use linked lists / sentinel

  1. #31
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    Usually I get provided with a main.c and I just have to compile it and test the function.
    Here I don't have it so I am not sure what to do.
    Code:
    int main()
    {
        //what to write here ?
        return 0
    }
    How could I call the functions I want I am a bit lost. For example to initalize the list, it needs to exist right ? So in the main I need to create an empty list ?
    Then I need to print it ? I have all in my mind but I am not sure how to write all that and in what order

  2. #32
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    I get this error multiples times "dereferencing pointer to incomplete type ‘struct list"

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,147
    Post the smallest and simplest program that you expect should compile but which demonstrates that error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User
    Join Date
    Apr 2013
    Posts
    1,620
    Quote Originally Posted by centrecmercial View Post
    I tried to write the second function
    You might want to add a check to make sure that the input parameter is not NULL. This check could be added to all of the functions, to avoid getting run time errors if the input pointer is NULL.

    Code:
    int list_is_empty(struct list *list)
    {
        if (list == NULL || list -> next == NULL)
        {
            return 1;
        }
        return 0;
    }

  5. #35
    Registered User
    Join Date
    Apr 2013
    Posts
    1,620
    Quote Originally Posted by centrecmercial View Post
    Wait wait wait I want to be sure I have understood. Is this correct?

    Code:
     void list_push_front(struct list *list, struct list *elm){
        list = list -> next;
        list -> data = *elm;
    }
    It inserts an element in front of the sentinel
    What that code is doing is replacing the data of the first element of the list (not including the sentinel node) with the data from elm . What it should be doing is setting the node elm to point to the first element of the list, then set the sentinel to point to elm. The node member data should not be modified.

  6. #36
    Registered User
    Join Date
    Apr 2013
    Posts
    1,620
    I just noticed the due date was Dec 7, so probably no more questions from the OP. I hope the issues were solved. In my opinion, the list structure should have been named "node" instead, since it defines a single node, while a list should refer to a set of linked nodes (the set could be empty, have a single node, or multiple nodes). For a working pointer to node, I would have used a name like pnode, nodeptr, or since the code called a single node elm, perhaps just call a working pointer tmp .

  7. #37
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,147
    I wrote this in post #14:
    Quote Originally Posted by laserlight
    Despite the name, a struct list object that is an actual element of the linked list is a node in the linked list, not the linked list itself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM

Tags for this Thread