循環チェーンテーブルのクラシック実装(JAVA)
public class LinkedListNode {//
public LinkedListNode previous;//
public LinkedListNode next;//
public Object object;//
public long timestamp;
public LinkedListNode(Object object, LinkedListNode next, LinkedListNode previous)
{
this.object = object;
this.next = next;
this.previous = previous;
}
public void remove() {//
previous.next = next;
next.previous = previous;
}
public String toString() {
return object.toString();
}
}
import java.util.*;
public class LinkedList {//
// , , ,
private LinkedListNode head = new LinkedListNode("head", null, null);
public LinkedList() {
head.next = head.previous = head;
}
public LinkedListNode getFirst() {//
LinkedListNode node = head.next;
if (node == head) {
return null;
}
return node;
}
public LinkedListNode getLast() {//
LinkedListNode node = head.previous;
if (node == head) {
return null;
}
return node;
}
public LinkedListNode addFirst(LinkedListNode node) {// , .
node.next = head.next;
node.previous = head;
node.previous.next = node;
node.next.previous = node;
return node;
}
public LinkedListNode addFirst(Object object) {// , .
LinkedListNode node = new LinkedListNode(object, head.next, head);
node.previous.next = node;
node.next.previous = node;
return node;
}
public LinkedListNode addLast(Object object) {// ,
LinkedListNode node = new LinkedListNode(object, head, head.previous);
node.previous.next = node;
node.next.previous = node;
return node;
}
public void clear() {//
//Remove all references in the list.
LinkedListNode node = getLast();
while (node != null) {
node.remove();
node = getLast();
}
//Re-initialize.
head.next = head.previous = head;
}
public String toString() {
LinkedListNode node = head.next;
StringBuffer buf = new StringBuffer();
while (node != head) {
buf.append(node.toString()).append(", ");
node = node.next;
}
return buf.toString();
}
}
ソース: