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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:50:48+00:00 2026-06-02T10:50:48+00:00

I have a fairly basic program that is intended to sort a list of

  • 0

I have a fairly basic program that is intended to sort a list of numbers via a Linked List.

Where I am getting hung up is when the element needs to be inserted at the beginning of the list. Here is the chunk of code in question

Assume that root->x = 15 and assume that the user inputs 12 when prompted:

void addNode(node *root)
{
int check = 0;  //To break the loop
node *current = root;   //Starts at the head of the linked list
node *temp = new node;


cout << "Enter a value for x" << endl;
cin >> temp->x;
cin.ignore(100,'\n');


if(temp->x < root->x)
{
    cout << "first" << endl;
    temp->next=root;
    root=temp;

        cout << root->x << " " << root->next->x; //Displays 12 15, the correct response
}

But if, after running this function, I try

cout << root->x;

Back in main(), it displays 15 again. So the code

root=temp;

is being lost once I leave the function. Now other changes to *root, such as adding another element to the LL and pointing root->next to it, are being carried over.

Suggestions?

  • 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-02T10:50:54+00:00Added an answer on June 2, 2026 at 10:50 am

    This because you are setting the local node *root variable, you are not modifying the original root but just the parameter passed on stack.

    To fix it you need to use a reference to pointer, eg:

    void addNode(node*& root)
    

    or a pointer to pointer:

    void addNode(node **root)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent-child relationship setup that is fairly basic. The end result is
I have a fairly basic SQL Server Reporting Services report that is using nested
I have a fairly basic dialog maker for jquery that works in 2 out
I have an ASP.NET application that is fairly basic. It queries some data and
I have a Visual C# Project that is fairly basic (no more than 100
I have a fairly basic app that only has two Activities, the splash screen
I have a fairly basic TCP server keeping track of a couple connections and
I am making a webapp. I have a fairly basic question about javascript performance.
basic c++ question i'm fairly sure. if i have a base class with a
I have a fairly standard OLTP normalised database and I have realised that I

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.