Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6553017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:31:50+00:00 2026-05-25T12:31:50+00:00

This is a program trying to make a linked list. #include <iostream> using namespace

  • 0

This is a program trying to make a linked list.

#include <iostream>
using namespace std;

struct node {
char name[20];
int age;
int height;
node* next; // Pointer to the next node
 };
 node* startPTR = NULL; // Start Pointer (root)
                   // This pointer permanantly points to the start of the list
                   // Initially there are no nodes

void addNode_AT_END(); // prototype for the function that 'adds nodes at the end'

int main() {
   do {
 addNode_AT_END();
     cout << "Add more ?";
     char ch;
     cin >> ch;
   } while( ch == 'y');
}

void addNode_AT_END() {
node *temp1;
node *temp2;
temp1 = new node;  // We declare space for a pointer item and assign a temporary pointer to it  
                   //*temp1 is the node that it points to
cout << "Enter the name : ";
cin >> temp1->name;
cout << endl << "Enter the age : ";
cin >> temp1->age;
cout << endl << "Enter height : ";
cin >> temp1->height;
temp1->next = NULL; // indicates that this node when inserted in the list will be the last node
  if( startPTR == NULL) {
    startPTR = temp1;  // In the empty list last node will be the first node
  }  else {
        temp2 = startPTR;
        while( temp2->next != NULL )
            temp2 = temp2->next;
        temp2->next = temp1;
     }

}

From this yet to be completed program this is what i understand :

enter image description here

If the figure after the second call to the function addNode_AT_END is true, then what does temp2->next in the statement while( temp2->next != NULL ) contain ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:31:51+00:00Added an answer on May 25, 2026 at 12:31 pm

    Your diagrams are incorrect. start = temp2 does means that start and temp2 pointers both point to the same node. Your diagram shows the next field of the temp2 pointer holds the address of start . Also after doing start->next = temp1 does not mean that if you get some new node value in temp1 (in the next function call), the start->next will still keep pointing to the new value just allocated in temp1. It will hold the old value that was in before you overwrote it with the new one. start->next = temp1 simply copies the value in temp1 ie. an address to the variable (pointer variable) the next component of the node pointed by start which is start->next . After that there is no connection between start and temp1.

    In linked list context “temp1 —-> temp2” means the next field of the node whose address is stored in temp1, holds the address of the node with an address which was held or is held by temp2 . Now after you change the value of the pointer variable temp2 does not change the next field of the node held at address stored in temp1. temp1->next still contains the value which it stored before.

    The next links does not point to some variable name, that is, start->next = temp will not make start node’s next node always pointing to the whatever node temp1 contains, but it start->next will contain the address which temp1 stored at the time of the assignment.

    note that by saying “start is pointing to temp1” means that the address

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

    will break when temp2->next = NULL which means that temp2 points to the last node of the list. At this point temp2->next = temp1 links the newly allocated node after the node currently pointed by temp2. Which is simply adds the new node at the end.

      At the end of the above while loop
    
                                                                  temp2
                                                                    |
                                                                    V
    
    (start) ----> (n1) ----> (n2) ----> (n3) . . . (n(n-1)) ----> (nn) ----> NULL
    
    
       temp2->next = temp1    makes
    
    
                                                                  temp2
                                                                    |
                                                                    V
    
    (start) ----> (n1) ----> (n2) ----> (n3) . . . (n(n-1)) ----> (nn) ----> (temp1)--->NULL
    
    
     because temp2 holds the address of (nn) therefore linking the new node to the next node of the last node.
    

    UPDATE

    First time:

    start = NULL
    a new address is allocated and the address stored into temp1 pointer. Also temp->next = NULL
    if condition becomes true and temp1 is assigned to start
    start = temp1
    
    List state
    
    
    start = addr1;
     |
     V
    (addr1) ----> (NULL)
    

    Second time:

    a new node is allocated and the address of the new node is stored into `temp1`. Let this address be `addr2`. Now `temp1` contains the value `addr2`
    
    start is NOT NULL, as start has addr1 in it from the last call.So the else part is true and we get the address of the start node `addr1` into temp2.
    
    temp2 = start;
    
    which means temp2 now points to `addr1`
    
    while loop is encountered. The first iteration the condition `temp2->next != NULL` is FALSE. This is because `temp2` points to `addr1` and the next pointer field of `addr1` has NULL from the last time the function is called. Therefore the while loop terminates.
    
    The next statement does `temp2->next = temp1` . `temp2` points to `addr1` address, and the address of the newly allocated node `addr2` contained in `temp1` is assigned into the next field of the node whose address is stored into `temp2`. Which actually assigns the address `addr2` to the next field of the node identified by the address `addr1`.
    
    
    temp1 = addr2     after allocation
    
    start = addr1;
     |
     V
    (addr1) ----> (NULL)      at begining
     ^
     |
     temp2
    
    
    after temp2->next = temp1
    
    start = addr1;
     |
     V
    (addr1) ----> (addr2) ----> (NULL)      at end
     ^
     |
     temp2
    

    Third time:

    temp1 = addr3      new node address allocated
    
    start = addr1;
     |
     V
    (addr1) ----> (addr2) ----> (NULL)      at start
     ^
     |
     temp2
    
    
    start = addr1;
     |
     V
    (addr1) ----> (addr2) ----> (NULL)      next iteration after temp2=temp2->next
                     ^                      
                     |
                   temp2
    
    
    we can see temp2->next = NULL and while condition is false. Note that temp2 contains itself the address addr2, and thus temp2->next is NOT addr2, it is NULL.
    
    start = addr1;
     |
     V
    (addr1) ----> (addr2) ----> (NULL)      next iteration after temp2=temp2->next
                     ^                      
                     |
                   temp2
    
    
    
    After linking: temp2->next = temp1;
    
    start = addr1;               temp1         the address addr3 (new node)
     |                             |          is stored in temp1. this address is assigned
     V                             V         to the next node of temp2, replacing NULL
    (addr1) ----> (addr2) ----> (addr3) ----> (NULL)      
                     ^                      
                     |
                   temp2
    

    The pointers are the means to travel/traverse through the list. The starting address of the list is held in a pointer start. As the next field of each node points to the next node, if we get the start node, then by following the next fields sequentially we can visit each node. temp1 and temp2 are pointers with which the traversals are done, they act as temporary pointers, temp1 is used to hold the newly allocated node, and temp2 is used to travel through the list by following the next links till the last, when the last link is found (detected by the NULL pointer in the next field), the NULL link of this last node is replaced by the newly allocated node held by temp1 . As now the node held by temp1 is linked/added to the end of the list, temp1 is reused to hold another new node.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a function that sorts the linked list,which sorts the
OK, so I am trying to make a program using Visual Basic that will
I'm trying to make this program in assembly. It should just convert a string
i'm trying to get this program to break down a user defined amount of
I am trying to compile a one-off script, an autogenerated C# program. This program
I'm trying to get this simple program to work on windows, but it crashes:
Hi iam new to c++ and iam trying out this vector program and i
Im trying to use Date Formatters (NSDateFormatter), but I keep getting this error: Program
I'm trying to do this: cmd.exe /C C:\Program Files\Somewhere\SomeProgram.exe > C:\temp\Folder Containing Spaces\SomeProgram.out However,
I have this output when trying to debug Program received signal SIGSEGV, Segmentation fault

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.