Thread: How to use linked lists / sentinel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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

  2. #2
    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)

  3. #3
    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

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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.

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