I'm currently studying Doubly Linked Lists and want ask something. This is a function for Node insertion at particular position.
"temp" is a pointer to node.
void insertpos(int value,int pos)
{
node *s;
temp=new node;
s=head;
for(int i=0;i<pos-1;i++)
{
s=s->next;
}
temp->data=value;
if(s->next==nullptr)
{
s->next=temp;
temp->next=nullptr;
temp->prev=s;
}
else
{
temp->next=s->next;
temp->next->prev=temp;
s->next=temp;
temp->prev=s;
}
}
What does this line of code mean temp->next->prev=temp;
The function works perfectly even without this.
I understand that to insert at particular position, you need to start from head and traverse till that (position-1) and set the position's next pointer to temp and temp's next pointer to next pointer of position and temp's previous pointer to position. But this can be achieved by the following three lines of codes
temp->next=s->next;
s->next=temp;
temp->prev=s;
So what's the use of this line temp->next->prev=temp;
What does this line mean?