Hi, for this assignment, I am to implement a program that would read a series of cities from a text file and put it into a sorted circular doubly linked list while also using head and tail as sentinel nodes. I have already done this assignment by using a singly linked list and now I'm supposed to convert it into a circular doubly linked list. Now, I'm having some trouble trying to actually link the back pointer to the previous node in the linked list. I get the whole concept of the doubly linked list but I just have a hard time of actually trying to implement it.
Even in singly linked circular list there should be no end, or the list would not be circular.
The "head and tail as sentinel nodes" does not fit to the concept of circular list. Something is odd here.
In a doubly linked list each node points both to node before and to node after them. If you can maintain one set of links, then it should be trivial to maintain the other too. Just more work.
1 2 3 4 5 6 7 8
// Insert X between A and B
insertAfter( ListNode * pos, const Foo & data ) {
Foo * node = new Foo( data );
node->forw = pos->forw; // X.forw = B
node->forw->back = node; // B.back = X
pos->forw = node; // A.forw = X
node->back = pos; // X.back = A
}