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 6472697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:22:39+00:00 2026-05-25T06:22:39+00:00

This is a piece of code that tries to build a linked list. struct

  • 0

This is a piece of code that tries to build a linked list.

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

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;
    }
 }

The following is diagram after 2 back to back calls to the above function.

start = addr1;
|
V
(addr1) ----> (addr2) ----> (NULL)      at end
^
|
temp2

where addr1 and addr2 are the address of the first and second nodes respectively.

What happens after the third call ? How the iteration will go on for the third call? I am unable to understand how the list links up after the second call.According to me all that has been build up till know will vanish.Then how will list move further ? How is node placed during the third call?

  • 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-25T06:22:40+00:00Added an answer on May 25, 2026 at 6:22 am

    Here is where all the magic happens:

    1. temp2 = startPTR;
    2. while( temp2->next != NULL )
    3.    temp2 = temp2->next;
    4. temp2->next = temp1;
    

    First, temp2 will point to the beginning of the list. In lines 2 and 3, you change temp2 to the next node until you reach the node where temp2->next is NULL. This node is the last node of the list, regardless of the size of the list.

    Finally, in line 4 you change temp2->next to temp1 so now it points to the new node (that is last node now points to the new node). temp1->next is also NULL, so temp1 now represents the end of the list.

    After line 1 you have

    start = addr1;
    |
    V
    (addr1) ----> (addr2) ----> (NULL)
    ^
    |
    temp2
    

    temp2->next is not NULL (it is addr2), so you iterate and execute line 3 and you get:

    start = addr1;
    |
    V
    (addr1) ----> (addr2) ----> (NULL)
                  ^
                  |
                  temp2
    

    temp2->next is now NULL. So you stop the loop and execute line 4 and you get:

    start = addr1;
    |
    V
    (addr1) ----> (addr2) ----> (addr3) ----> (NULL)
                  ^             ^
                  |             |
                  temp2         temp1
    

    Note: Do you know how pointers work? Imagine this: You have a node, which is some data in the memory. When you have variables in memory, these variables have addresses. Let’s say addr1 is 10, addr2 is 150 and addr3 (which is the node just newed) is 60. start has value 10. Therefore, “pointing” to the first node of the list (that is using this address, you have access to its data). One of these data, is the next field. The first node’s next field has value 150, thus pointing to the next node. When you say temp2 = start, you put number 10 in temp2, at this point temp2->next has value 150. When you say temp2=temp2->next, you simply put value 150 in temp2, overwriting the previous value. This way you have effectively moved your pointer from pointing to the first node, to now pointing to the second node. Now temp2->next is NULL (that is 0). When you now say temp2->next=temp1, you put value 60 in the next field of temp2. So now temp2->next is 60. temp2->next->next is NULL.

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

Sidebar

Related Questions

I have a piece of code that looks like this: downloadsByExtensionCount = defaultdict(int) downloadsByExtensionList
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have this piece of code that is giving me trouble. I know all
I've got this really simple piece of code that I thought was the correct
Assume that I have this piece of code: @interface Foo : NSObject { Bar
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I've come across a piece of Haskell code that looks like this: ps@(p:pt) What
I have this program that should execute a piece of code base on the
I have got a piece of code, that should countdown some number (in this

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.