Coded
This is a discussion on input filestream to a doubly linked list within the C++ Programming forums, part of the General Programming Boards category; Coded...
Coded
Last edited by Undying Mirai; 06-26-2006 at 06:47 AM. Reason: Already done it
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:
To prepend, you basically do the same thing reversed:Code:temp = new link; temp->next = 0; temp->prev = tail; tail->next = temp; tail = tail->next;
To insert, you have four links to fix:Code:temp = new link; temp->next = head; temp->prev = 0; head->prev = temp; head = 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:temp = new link; temp->next = after; temp->prev = before; before->next = temp; after->prev = temp;
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;p.s. Don't ever delete your post's content. Other people can learn from your question and any answers to it.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;
My best code is written with the delete key.