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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:53:17+00:00 2026-05-20T17:53:17+00:00

I am using the following function to insert a new node into a sorted

  • 0

I am using the following function to insert a new node into a sorted linked list of integers

 // Insert new element
template <class Type>
bool list<Type> :: Insert (const Type& NewElement)
{
    Node *NewNode, *TempNext, *TempPrevious;
    NewNode = new Node;
    NewNode -> Element = NewElement;

    for (TempNext = Head; TempNext != NULL; TempPrevious = TempNext, TempNext = TempNext -> Next) 
    {
        NewNode -> Next = TempNext;
        if (TempNext == Head) Head = NewNode; // Check for empty list
        else if (NewNode -> Element >= TempNext -> Element) continue; // Check for correct point in list
        else TempPrevious -> Next = NewNode;
        return true;
    }

    // If execution reaches this point, then the new node goes at the end of the list    
    TempPrevious -> Next = NewNode;
    return true;
}

Whenever I try to insert an element into an empty list using this algorithm, the program returns a segmentation fault. A check with GDB identifies the TempPrevious -> Next = NewNode; line at the very end as the cause, but execution shouldn’t be reaching there since the return true at the end of the for loop should return control to the invoking function, but for some reason it isn’t. Can anyone see where I’m going wrong here?

  • 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-20T17:53:18+00:00Added an answer on May 20, 2026 at 5:53 pm

    Notice that if the list is empty, TempPrevious will an uninitialized pointer. When you try running the for loop on an empty list, TempNext will immediately be NULL and you won’t execute the statement TempPrevious = TempNext. Since you never set TempPrevious to have a default value, it will be uninitialized, and so the code

    TempPrevious -> Next = NewNode;
    

    Will dereference a garbage pointer, hence your crash.

    To fix this, you will either need to special-case the case when the list is empty, or use some other approach to list insertion (perhaps keeping a pointer to the node pointer to rewrite) that gracefully handles insertion into an empty list.

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

Sidebar

Related Questions

I am attempting to insert a new node into a sorted linked list of
I'm using the Node.js native driver. The following works fine db.collection(test).insert({hello:'world_safe'}, {safe: true}, function(err,
I'm using the following function to insert data into a MySQL database. Can someone
I am an iOS newbie. I am using the following function to insert new
I have the following code: public function createNewGuide($userID,$guideName) { $sql = INSERT INTO myTable(name,
I insert into an Oracle DB Table using the following insert string (Approx --
I'm using PHP PDO with PostgreSQL for a new project. Given the following function,
I'm using the following code to insert extra form fields. function addFormField() { $(#divTxt).append(<div
I'm using the following to grab some updated HTML and insert it into a
I'am new to canvas. I was using the following code to insert a image

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.