Thread: linked list: why does 'root-> next' change when...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    Question linked list: why does 'root-> next' change when...

    Got this code from tutorial

    Code:
    #include <iostream>
    using namespace std;
    struct node {
      int x;
      node *next;
    };
    
    int main()
    {
      node *root;       // This won't change, or we would lose the list in memory
      node *conductor;  // This will point to each node as it traverses the list
    
      root = new node;  // Sets it to actually point to something
      root->next = 0;   //  Otherwise it would not work well
      root->x = 12;
      conductor = root; // The conductor points to the first node
      if ( conductor != 0 ) {
        while ( conductor->next != 0)
          conductor = conductor->next;
      }
     cout << "Before    " << root->next << endl;
      conductor->next = new node;  // Creates a node at the end of the list
    
    //Why does 'root -> next' change  here?
     cout << "After    " << root->next;
      conductor = conductor->next; // Points to that node
      conductor->next = 0;         // Prevents it from going any further
      conductor->x = 42;
    cin.get();
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    root and conductor are pointing to the same thing before the second node gets created. When you add a new node to conductor->next, you are also effectively updating root->next.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code does not free root. Consequently, it's bad code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    conductor->next = 0; //conductor is 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM