Click here to Skip to main content
15,886,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So if we are creating a doubly linked list how is previous node managed
where does rear points
in this case I am using rear as current pointer

What I have tried:

node *newnode=new node();
    newnode->v=a;
    if(rear!=NULL)
    {
        rear->next=newnode;
        rear->next->prev=front->next;
        newnode->next=NULL;
        newnode->prev=rear;
        rear=newnode;
    }
    else
    {
        front->next=newnode;
        front->prev=NULL;
        newnode->next=NULL;
        newnode->prev=front;
        rear->prev=front;
        rear=newnode;
    }
Posted
Updated 30-Oct-17 0:14am

You have two pointers in the node, next points to the next node in the list, or NULL if there are no more. previous points to the (obviously) previous node in the list, or NULL if it is the first. You manage your list by initialising a pointer with the address of the first node in the list. As you add or remove nodes, you update their links appropriately.
 
Share this answer
 
Comments
CPallini 30-Oct-17 6:10am    
5.
Richard already gave you the answer.
Please note
Quote:
node *newnode=new node();
newnode->v=a;
if(rear!=NULL)
{
rear->next=newnode;
rear->next->prev=front->next;
newnode->next=NULL;
newnode->prev=rear;
rear=newnode;
}
else
{
front->next=newnode;
front->prev=NULL;
newnode->next=NULL;
newnode->prev=front;
rear->prev=front;//<==== BUG here
rear=newnode;
}


There is a bug in the marked line: you are dereferencing a NULL pointer (rear).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900