I want to keep my doubly linked list sorted all the time and keep the head and the tail node pointers updated. I managed to keep the head pointer updated in all operations but I can not keep track of the tail pointer. It only shows the last element as tail->info but can not go back as doing tail=tail->prev; here is my code:
AddInOrder(node* &head, node* &tail, int number) {
node *ptr=head;
if (head==NULL || number<head->info) {
node*temp=new node;
temp->info=number;
temp->next=head;
temp->prev=NULL;
head=temp;
tail=temp;
return 0;
}
while(ptr->next!=NULL && ptr->next->info<number) {
ptr=ptr->next;
}
node*temp=new node;
temp->info=number;
temp->next=ptr->next;
temp->prev=ptr;
ptr->next=temp;
while(ptr->next!=NULL) {
ptr=ptr->next;
}
tail=ptr;
return 0;
}
tail=tail->prev;" - I'm trying to fathom a situation where you would ever want to do that in the first place, as if anything it will put yourtailpointer somewhere it shouldn't be; not on the last node of the list.ptr->prevwhen you link a new node in. And when you add a new node to the head of the list you're wiping out yourtailpointer.