C++ programmers, this newbie need help

This is a discussion on C++ programmers, this newbie need help within the C++ Programming forums, part of the General Programming Boards category; assuming that temp is a pointer to a node and node is a pointer to the next node, what is ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    C++ programmers, this newbie needs help

    assuming that temp is a pointer to a node and node is a pointer to the next node, what is this saying here?

    temp->nxt = NULL;
    Last edited by incognito; 02-03-2002 at 01:42 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Means there is no next node.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    I'm not an expert, but I'm fairly sure that is setting the nxt node to NULL, meaning that it no longer points to another node, making nxt the end of the chain.

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Basically is saying that temp is going to be the last node on the linked list?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,669
    right. This is the mechanism for recognizing the end of the list:

    while(temp->next!=NULL)
    temp = temp->next;



    ITSA
    Socket Library!

  6. #6
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    right. This is the mechanism for recognizing the end of the list:

    while(temp->next!=NULL)
    temp = temp->next;
    What is this saying though.....while temp is pointing to what?
    I just want to know how this might be worded in normal english to see if I can understand it.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    more questions.......

    Code:
    void add_node_at_end ()
      {  node *temp, *temp2;   // Temporary pointers
    
         // Reserve space for new node and fill it with data
         temp = new node;
         cout << "Please enter the name of the person: ";
         cin >> temp->name;
         cout << "Please enter the age of the person : ";
         cin >> temp->age;
         cout << "Please enter the height of the person : ";
         cin >> temp->height;
         temp->nxt = NULL; // how can temp be set to be the last node here and 
    
         // Set up link to this node
         if (start_ptr == NULL)
             start_ptr = temp;// be set here to be the first node
         else
           { temp2 = start_ptr;
             // We know this is not NULL - list not empty!
             while (temp2->nxt != NULL)
               {  temp2 = temp2->nxt;
                  // Move to next link in chain
               }
             temp2->nxt = temp;
           }
      }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Location
    England
    Posts
    121
    temp isnt the last node. tmp->nxt = NULL means that nxt is the last node, because it is set to NULL (doesnt point to anything else).

  9. #9
    Unregistered
    Guest
    If temp is an instance of a struct that is defined something like this:

    struct Temp
    {
    //data variables
    Temp * next;
    };

    and temp is declared something like this:

    Temp * temp = new Temp;

    then:

    while(temp->next != NULL)

    means:

    while the pointer called next in the struct called temp is not equal to NULL do whatever is in the body of the while loop OR in plain english: keep doing what is in the body of the while loop until you come to the last node of the list.

    When you first create and initialize temp in your add_node_at_end() function you need to give next some value so it isn't a stray pointer. The value usually given to this pointer at this time is NULL. At the moment this does not mean that temp is the last node in the list. In fact temp is just a node, free floating in the breeze at this point.

    Someplace else in the code you must declare start_ptr and assign it the value of NULL (so it isn't a stray pointer). The funciton should be passed the start_ptr as an argument so the function knows which list it is working with. Then in the section

    //set up link to this node

    the

    if(start_ptr == NULL)

    phrase means if there are no nodes in the list yet OR if the list is "empty" AND the phrase

    start_ptr = temp;

    assigns the address stored in temp to the pointer start_ptr so that the values in temp becomes the valuse of the first node in the list. Since it is now the ONLY node in the list, there is not next node for it to point to so next remains NULL.

    The next time you add another node to the list however, starter_ptr will no longer be NULL. So you assign the address in start_ptr to temp2 (you don't want to work directly with start_ptr if you don't have to), and then search the list until you find the node that has next == NULL, which will be the last node in the list. If the list is only one node long, then then you're already there, but if the list is x nodes long, then you have to search for it. When you find it you assign the address of the node called temp to the next pointer in the node called temp2, essentially making the node called temp the last node in the list.


    Each time you call add_node_at_end you create a completely new and different node called temp which has a completely new and distinct address from all the other nodes called temp you may have created from previous or future calls to the function, so eventhough all the nodes start life with the same "name" they are not the same node as they have different addresses. The name temp is just a symbol to make it easier to manipulate things. After it's the created and entered into the list each node will be refered to or searched for by it's address or the value of some member variable, NOT the name it started out with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-21-2006, 11:23 PM
  2. the holy grail for newbie programmers...
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2003, 07:19 PM
  3. Are programmers engineers?
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 04-01-2003, 12:55 AM
  4. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 03:38 PM
  5. Newbie Game Programmers!
    By Ryce in forum Game Programming
    Replies: 12
    Last Post: 09-07-2001, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21