Data Structures, Algorithms, & Applications in Java
Chapter 6, Exercise 47
Two doubly-linked circular lists may be joined in
O(1) time as below.
public class DoubleCircularListWithJoin extends DoubleCircularList
{
/** append list a to this
* following the join, a is empty */
public void join(DoubleCircularListWithJoin a)
{
// special cases
if (a.size == 0)
// a is empty, nothing to join to this
return;
if (size == 0)
{// this is empty,
// move all nodes of a to this
lastNode = a.lastNode;
size = a.size;
// make a empty
a.lastNode = null;
a.size = 0;
return;
}
// neither this nor a is empty
// link a into end of this
DoubleNode firstNode = lastNode.next;
DoubleNode aFirstNode = a.lastNode.next;
lastNode.next = aFirstNode;
aFirstNode.previous = lastNode;
lastNode = a.lastNode;
firstNode.previous = lastNode;
lastNode.next = firstNode;
size += a.size;
// make a empty
a.clear();
return;
}
}