/** * If this Link is not chained to one or more other Links, insert it into a chain after that Link, * including a chain in which that Link is the only Link. This Link inherits the root of that * Link. Upon completion, this Link will be part of a chain just after that Link. A reference to * this Link is returned. If this Link is already chained to other Links, null is returned; you * must remove this Link from its own chain before inserting it into another chain. An insert is * done in O(1) time. A Link could be inserted after itself (which would have no effect on its * state), but null is returned if this is attempted. * * @param that refers to a Link that this Link is to be inserted after. * @return a reference to this Link if the insert was successful, false otherwise. */ public Link<Type> insert(Link<Type> that) { if (!isChained() && (this != that)) { next = that.next; previous = that; root = that.root; next.previous = this; that.next = this; return this; } return null; }
/** * If this Link is chained to one or more other Links, remove it from that chain. Upon completion, * this will be a valid unchained Link and the the chain that it was on will be intact without * this Link. If this Link was the root of a chain of just two Links, the other Link is rooted * back to itself. If this Link was the root of a chain with more than two Links, the resulting * chain is left still rooted to this Link, which is unlikely to be what you want. A reference to * this Link is returned. If this Link is not chained to other Links, null is returned. A remove * is done in O(1) time. * * @return a reference to this Link if the remove was successful, false otherwise. */ public Link<Type> remove() { if (isChained()) { next.previous = previous; if (next.previous == next) { next.root = next; } previous.next = next; if (previous.next == previous) { previous.root = previous; } next = this; previous = this; root = this; return this; } return null; }