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

The Archive Base Latest Questions

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

I am trying to insert a value at the end of a doubly linked

  • 0

I am trying to insert a value at the end of a doubly linked list , I get successful in inserting the value at head or first node but the second value is not getting inserted

The issue here is while entering the second value

class d_list
{
private:

    struct node
    {
        double data;
        node *next;
        node *previous;
    };

    node *first;
    node *last ;
public:
    d_list(void)
    {
        first = nullptr;
        last = nullptr;
    };
    void append(double);

};

void d_list::append(double num)
{
    node *ptr;
    node *toinsert;
    if(!first)
    {
        first = new node;
        first->previous= nullptr;
        first->data = num;
        last= new node;
        first->next= last->previous;
        last->previous = first->next;
        last->next= nullptr;

    }
    else
    {
        if(last->next == nullptr)
        {
            ptr = new node;
            ptr->next =last->previous;
            ptr->data=num;
            last->previous = ptr->next  ;
        }


        last->next= nullptr;
    }

}


int _tmain(int argc, _TCHAR* argv[])
{
    d_list aa;
    cout<<"going to append first"<<endl;
    aa.append(44);
    cout<<"going to append second"<<endl;
    aa.append(50.5);

    return 0;
}
  • 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-30T11:22:26+00:00Added an answer on May 30, 2026 at 11:22 am

    You have a number of problems in your code:

    • Your node next and previous members are never initialized anywhere and as a result are undefined when used. Either add a constructor to node or ensure they are initialized after allocation.
    • The addition of a node to an empty list is not correct. first->next is left undefined and why are you creating two nodes, both first and last? In a list with one element then first == last. The setting of next/previous in first/last doesn’t make any sense either.
    • In a well formed double-linked list then last->next should always be null, as should first->previous.
    • The addition of a node into a non-empty list is also incorrect.
    • While you don’t show it in the example, you’ll eventually need a destructor as well as a copy operator and copy constructor (the rule of three). At the moment you are leaking memory and if you try to delete nodes you’ll likely result in a double-free and crash.

    I would suggest taking a step back from the code for a bit to ensure you properly understand the concepts behind a doubly-linked list. Draw out a list on paper with next/prev arrows and see how they need to be changed when adding nodes to an empty/non-empty list as well as how to delete and move nodes around. Once you figure out how next/prev should be set then translating that into code should be relatively straight forward.

    Edit to answer comment:
    To add a new node you can technically add it anywhere but it is usually added at the end (at least from what I’ve seen). See the other answers for a complete and correct code for adding new nodes in an empty and non-empty list.

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

Sidebar

Related Questions

I am trying to insert a time only value, but get the following error
I am trying to insert a value in the database but when I open
I'm new and this is my first question. I'm trying to insert a value
I'm trying to cache a price value using HttpRuntime.Cache.Insert(), but only appears to hold
I'm trying to insert a false boolean value in a SQLite3 table but it
I'm trying to insert some value pairs into a std::map. In the first case,
Im trying to create linked-list insert function that takes a list (or more correctly
I am trying to insert a value to sql_variant column in sql server from
I am trying to insert an value as '019393' into a table with a
This is driving me insane. I'm trying to INSERT a value into the following

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.