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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:42:25+00:00 2026-06-04T17:42:25+00:00

I am trying to construct a simple linked list, using a pointer pointing to

  • 0

I am trying to construct a simple linked list, using a pointer pointing to the next insertion location, and add a node one by one.

Tnode* NULL_cp = 0;
struct Tnode{
 string word;
 Tnode* left;
 Tnode* right;
};


int main(int argc, char** argv){
 int n = 0;
 Tnode head;
 head.word = "the head";
 head.left = NULL_cp;
 head.right = NULL_cp;
 Tnode* insertP = &head;
 while(n<argc-1){
  Tnode node;
  node.word = argv[n+1];
  node.left = insertP;
  node.right = NULL_cp;
  insertP->right = &node;
  insertP = &node;
  cout << "inside loop: " << insertP->word <<  endl;
  n++;
 }
 cout << "outside loop: the word is " << insertP->word << endl;
}

the output is

inside loop: hello
outside loop: the word is

if I typed in a.out hello. the part that confused me is that after one loop, the insertP should be pointing at the newly inserted node that has the the word hello, but it printed out nothing even though inside the loop it printed out hello, any idea why? Thank you very much

  • 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-06-04T17:42:28+00:00Added an answer on June 4, 2026 at 5:42 pm

    Let’s minimize the problem:

    while(n<argc-1)
    {
       Tnode node;
       //...
    }
    

    When node goes out of scope, so does its std::string member. You’ll have dangling pointers to nodes in your tree. Inside the loop it works because the object is still alive. Outside… not so much.

    Use dynamic allocation:

    while(n<argc-1){
      Tnode* node = new Tnode;
      node->word = argv[n+1];
      node->left = insertP;
      node->right = NULL_cp;
      insertP->right = node;
      insertP = node;
      cout << "inside loop: " << insertP->word <<  endl;
      n++;
    }
    

    And don’t forget to delete at the end.

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

Sidebar

Related Questions

I'm trying to construct a simple status panel using MigLayout as follows: setLayout(new MigLayout(fillx,
Am trying to construct a simple update query in my model class Model_DbTable_Account extends
I'm trying to construct a simple delete statement in mysql and can't seem to
Simple Substitution Cipher. I'm trying to make a looping construct that would loop through
I'm currently trying to construct a list of bean classes in Java from a
I'm trying to construct a FSM for a simple compiler. I've chosen to create
I'm trying to construct a database driven VB.Net app that pulls a list of
I'm trying to build a simple HTTP Server using Java, using java.net.ServerSocket = new
I'm trying to construct a simple query in for getting users with particular meta
I'm trying to write a simple function to construct field names for a form.

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.