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

  • SEARCH
  • Home
  • 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 6383879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:42:18+00:00 2026-05-25T02:42:18+00:00

struct node { char name[20]; int age; int height; node* next; // Pointer to

  • 0
struct node {
    char name[20];
    int age;
    int height;
    node* next; // Pointer to the next node
};
node* startPTR = NULL; // global

void addNode_AT_END() {
    node *temp1;
    node *temp2;

    temp1 = new node;  

    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;

    if( startPTR == NULL) {
       startPTR = temp1; 
    }  else {
       temp2 = startPTR;

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

       temp2->next = temp1;
    }
 }

In this snippet , when the function is called the third time , else part works. The address of the startPTR is assigned to the temp2. Now what does temp2->next contain when the condition is being checked in the while loop? (during third call) If you say it contains the address of the second node , please tell how does it know the address of the second node,because we had assigned the address of the second node to the first node during the second call to the function using the statement temp2->next = temp1 but because of it’s local scope we loose the address.

This is the way i am currently thinking.Please explain how the condition is checked during the third call to the function and how the linked list is being formed?

  • 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-25T02:42:19+00:00Added an answer on May 25, 2026 at 2:42 am

    It’s only the pointer variables temp1 and temp2 that are of local scope. The actual linked-list nodes (of type struct node) are allocated on the heap via the call to new. Heap allocated data persists until it’s free’d via a call to delete.

    This means that the line temp2->next = temp1 is storing the address of the new node that’s appended to the tail of the list, and this information will be available on subsequent calls to the function addNode_AT_END.

    The code seems to have a variety of issues – you’re using global variables, unless there are subsequent calls to delete somewhere you have memory leaks, there’s already the std container class std::list that’s available…

    Hope this helps.

    EDIT: Regarding your comment – when you make a call new node you are constructing a node object on the heap. This object will available for use until a subsequent call to delete is made.

    When you make the call temp2->next = temp1 the following is true:

    1. temp2 points to the address in memory where the last node in the list is stored (this node would have been created by the call to new on the last iteration).
    2. The value of the pointer variable temp1 is assigned as the next pointer for the last node in the list (the data “pointed to” by temp2). This means that the address is stored on the heap, not within the local temp2 pointer varaible.

    When your function exits, yes the local pointer variables temp1 and temp2 go out of scope, but the heap allocated linked-list nodes are not destroyed – and this is where the addresses are stored.

    EDIT: Second comment – in the else branch of the function, the pointer temp2 is initialised to point to the head of the list with the line temp2 = startPTR;

    The next lines (the while loop) traverse the linked-list from the head node until the pointer temp2 points to the last node in the list (until temp2->next = NULL)

    At this stage the new node is appended to the list, as discussed above.

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

Sidebar

Related Questions

#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
#include <stdio.h> #include <stdlib.h> typedef struct { char *Name; int grade; int cost; }Hotel;
I've got a node struct struct Node{CString text, int id;}; in a sorted vector.
Having construction in a form: struct Node { Node():left_(nullptr), right_(nullptr) { } int id_;
A link is a pointer to a node typedef struct node * link; In
struct elem { int i; char k; }; elem user; // compile error! struct
struct TimerEvent { event Event; timeval TimeOut; static void HandleTimer(int Fd, short Event, void
struct { char a; int b; } x; Why would one define a struct

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.