Thread: Don't Understand Linked List COUT

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    7

    Don't Understand Linked List COUT

    why is my cout as follows:

    test
    1
    test
    2
    3

    why did it not output test after 2

    Code:
    
    #include <iostream>
    #include <cstddef>
    
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    
    
    struct node{
        int data;
        node *next;
    };
    
    
    int main()
    {
    
    
    node *n;        // new
    node *t;        // temp
    node *h;        // head
    
    
    n = new node;
    n -> data = 1;
    t = n;
    h = n;
    
    
    n = new node;
    n -> data = 2;
    t -> next = n;      // t is still associated with node 1.  t make node 1 point to node 2.
    t = t -> next;      // could have said " t = n; "  Now, t points to node 2.
    
    
    n = new node;
    n -> data = 3;
    t -> next = n;
    t = t -> next;
    
    
    n = new node;
    t = t -> next;
    n -> data = 4;
    t -> next = n;
    n -> next = null;
    
    
    if ( n != 0 )
        {
            while ( n -> next != 0 )
            {
                cout<< "test" << endl;
                cout<< n->x << endl;
                n = n->next;
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Your code doesn't even compile!
    Where is "null" defined?
    Where is the "x" struct member?
    Don't post code that you just randomly typed in.
    Copy and paste the actual code that you ran.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    7
    Hey, it compiled for me.
    Attached Images Attached Images Don't Understand Linked List COUT-ll-compile-png 

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Whatever. Good luck with that.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,608
    Ignoring all the other errors that algorism pointed out, if you look carefully you should notice that you handled the 4th node differently from the others. That part of the listing says:
    Code:
    n = new node;
    t = t -> next;
    n -> data = 4;
    t -> next = n;
    n -> next = null;
    Unfortunately, the order that you wrote this in matters. You set t to t->next on the first highlighted line, which is a pointer that you cannot dereference; it doesn't point to a valid address, like that of another node. When you dereference it anyway in the second highlighted line, you risk a segfault. This would have never attached node 4 to node 3, just because this section of the code is different from the others. You did it right the first three times.

    This kind of mistake is why loops are our friends.

    Fixing other errors, a correct listing looks like this:
    Code:
    C:\Users\jk\Desktop>g++ foo.cpp
    
    C:\Users\jk\Desktop>more foo.cpp
    #include <iostream>
    #include <cstddef>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    struct node{
        int data;
        node *next;
    };
    
    int main()
    {
            node *n;        // new
            node *t;        // temp
            node *h;        // head
    
            n = new node;
            n -> data = 1;
            t = n;
            h = n;
    
    
            n = new node;
            n -> data = 2;
            t -> next = n;      // t is still associated with node 1.  t make node 1 point to node 2.
            t = t -> next;      // could have said " t = n; "  Now, t points to node 2.
    
            n = new node;
            n -> data = 3;
            t -> next = n;
            t = t -> next;
    
            n = new node;
            n -> data = 4;
            t -> next = n;
            n -> next = 0;
    
            n = h;
            if ( n != 0 )
            {
               while ( n -> next != 0 )
               {
                   cout<< "test" << endl;
                   cout<< n->data << endl;
                   n = n->next;
               }
            }
            //cout << "test\n" << n->data << endl;
    }
    
    C:\Users\jk\Desktop>a.exe
    test
    1
    test
    2
    test
    3
    Works as expected. Note that the main difference is that setting t to t->next is not performed; though you can, as in the other sections, if the list is meant to be larger.

    Note that the design of the loop purposefully leaves off the end node of a list because the next link is a null pointer. I don't know if that's what's being taught now in schools, but it's an important caveat.
    Last edited by whiteflags; 08-17-2017 at 10:03 PM. Reason: Fixing an indenting error.

  6. #6
    Registered User
    Join Date
    Aug 2017
    Posts
    7
    Wow, thanks Whiteflags.

    I'm way past school (retired Eng) and learning C++ on my own thru the Jumping into C++ book. Having a very hard time with linked lists, so I found this example from the Paulprogramming.com site.

    Your response makes perfect sense and I do appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list program, don't understand it fully
    By pwnsyoshi33 in forum C Programming
    Replies: 4
    Last Post: 02-22-2012, 11:42 AM
  2. Cant understand Linked list example:
    By satty in forum C Programming
    Replies: 15
    Last Post: 08-13-2010, 10:12 AM
  3. Dont understand something in linked list
    By elwad in forum C Programming
    Replies: 19
    Last Post: 09-21-2009, 07:26 PM
  4. I don't understand this line in a linked list
    By cdalten in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:15 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

Tags for this Thread