Thread: How to use linked lists / sentinel

  1. #16
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    I did that for the fourth

    Code:
    void list_push_front(struct list *list, struct list *elm)
    {
        list = list -> next;
    	list -> data = *elm;
    }
    I am struggling for the next one, it is struct list *list_pop_front(struct list *list);
    which extracts the first element (not the sentinel) of the list. How could the pointer go BACK (the opposite of list = list -> next)

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,147
    You want the sentinel to point to the second element.
    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

  3. #18
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    Yes that is what I tried to do

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,147
    Okay. Take a break and write a function that prints the data of the second element, if it exists.
    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

  5. #20
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    Wait you are talking about which function here ?

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,147
    I'm talking about an entirely new function, which you will delete once you wrote it such that it works: this function will print the data associated with the second element, if it exists. If it doesn't exist, it won't print anything.

    EDIT:
    Oh wait, actually, that's overkill. You don't need that to understand how to write list_pop_front. Rather, write a function that will print the data associated with the first element, if it exists. If it doesn't exist, it won't print anything.
    Last edited by laserlight; 5 Days Ago at 05:16 AM.
    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

  7. #22
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    Is my fourth function correct ?
    I did what you advised me to do:

    Code:
    void print(structlist *list)
    {
         list = list -> next //this is to go to the element after the sentinel
         if (list -> next != NULL) //if the element doesn't exist (we have NULL) we don't print it.
            {
              printf(%d, list -> data)
            }
    }
    I am quite sure this is right

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,147
    No, it is not correct. The problem is that your check for whether the element exists or not is wrong. You should check for list, not for list->next.

    But that's not so important. What's important is that you did access list->next... and that points to the second element of the list. For list_pop_front, you want the sentinel to point to the second element.
    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

  9. #24
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    Ok so I have this:

    Code:
    void print(structlist *list)
    {
         list = list -> next //this is to go to the element after the sentinel
         if (list != NULL) //if the element doesn't exist (we have NULL) we don't print it.
            {
              printf(%d, list -> data)
            }
    }
    


    Tell me first, is this correct? (to see if I have understood)

  10. #25
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    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

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,147
    Yes, although you need to fix the printf's format string, but that's not important here.

    What you should do is to call this function from the main function, then compile and run your program to test that your function works. You should do this for all your functions. Don't ask whether something is correct until you've tested it. If it doesn't work, then you've answered your own question. If it works, then it is a good time to ask if it works because perhaps it only works in the few test cases that you've thought of, so peer review can help you find flaws in your program that you overlooked.
    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

  12. #27
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    I do not know how I could test this function..

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,147
    Eh? Construct a linked list and call it.
    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

  14. #29
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    Sorry I am lost

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,147
    Haven't you been testing that your functions work before moving on to implement the next one?
    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