[Solved] cpp linked list program:error in display [closed]


You never assign an initial node. This is the kind of problem that a sheet of paper, a pencil, and two minutes of drawing arrows and boxes should solve, and until you’re fluent in dynamic allocation get used to doing a lot of that.

This:

if(start==NULL)
{
    p->next=NULL;
    p->prev=NULL;
    // NOTE: start is still NULL
}
else
{
    p->next=start;
    p->prev=NULL;
    p->next->prev=p;
    start=p;
}

should be this:

if(start==NULL)
{
    p->next=NULL;
    p->prev=NULL;
}
else
{
    p->next=start;
    p->prev=NULL;
    p->next->prev=p;
}
start=p; // NOTE: start always references the last node allocated.

But it is worth noting that the constructor for node already sets your member pointers next and prev to NULL. Therefore the above code is further reducible to simply :

if (start != NULL)
{
    p->next=start;
    start->prev=p;
}
start=p;

That said, the real solution is std::list<int> or std:vector<int>, but I suppose you have to start somewhere. Best of luck.

1

solved cpp linked list program:error in display [closed]