What is the code for insertion at tail ? what are the mistake in my code

void insertAtTail(node *&head, int d)
{
if (head == NULL)
{
head = new node(d);
}
else
{
node *tail = head;
while (tail != NULL)
{
tail = tail->next;
}
node *new_node = new node(d);
tail ->next= new_node;
new_node->prev = tail;
tail = new_node;

    // node *new_node = new node(d);
    // tail->next = new_node;
    // new_node->prev = tail;
    // tail = new_node;
}

}

Hello @amit could you please share it by saving it on Ide.codingblocks.com

saved

Condition in you while loop should be this
While (tail->next!=NULL)
Make this change.

1 Like

okay thank you !

can you give the code for Insertion at tail in DLL ?

@tusharaggarwal272 please give code in your point of view

@amit0001 check this
void insertAtTail(int d,node*&head,node*&prev)
{
if(head==NULL )
{
head=new node(d);
return;
}
nodetail=head;
while(tail->next!=NULL) //updated here tail->next instead of head->next
{
tail=tail->next;
//tail->prev
// return;//removed return form here
}
node
n=new node(d);
tail->next=n;
n->prev=tail;
tail=n;
}