To append into a double linked list, you need to reset any links that change, that means the next link of the tail and the previous link of the new tail:
Code:
temp = new link;
temp->next = 0;
temp->prev = tail;
tail->next = temp;
tail = tail->next;
To prepend, you basically do the same thing reversed:
Code:
temp = new link;
temp->next = head;
temp->prev = 0;
head->prev = temp;
head = temp;
To insert, you have four links to fix:
Code:
temp = new link;
temp->next = after;
temp->prev = before;
before->next = temp;
after->prev = temp;
Naturally, you don't always need unique pointers to each node that changes. A lot of the time you can use next or prev chains to avoid temporary variables:
Code:
tail->next = new link;
tail->next->next = 0;
tail->next->prev = tail;
tail = tail->next;
Code:
head->prev = new link;
head->prev->next = head;
head->prev->prev = 0;
head = temp;
Code:
temp = new link;
temp->next = before->next;
temp->prev = before;
before->next->prev = temp;
before->next = temp;
// or
temp = new link;
temp->next = after;
temp->prev = after->prev;
after->prev->next = temp;
after->prev = temp;
p.s. Don't ever delete your post's content. Other people can learn from your question and any answers to it.