/** * CircularList. * * @author rpj * @version 24x08 */ public class CircularList { protected Node tail; /* * We maintain a pointer to the tail which, in turn, points to the head * If we ever need the head, it is simply tail.getNext() * head tail * 1 -> 2 -> 3 -> ... -> 42 -| * ^ | * |-------------------------------------------| */ public CircularList() { tail = null; } /** * add * * @param stuff to be added */ public void add(T stuff) { if (tail == null) { // we're adding to an empty list tail = new Node (stuff, null); // make a new node tail.setNext(tail); // link it to itself } else { // make a new node whose successor is the head tail.setNext(new Node (stuff, tail.getNext())); // link it in at the tail of the existing list tail = tail.getNext(); } } /** * peek * * @return thing at head, or null if empty */ public T peek() { return (tail == null) ? null : tail.getNext().getData(); } /** * rotate * * advance to point to next item */ public void rotate() { // be careful of the empty list } /** * removeFromHead * remove item at head of list */ public void removeFromHead() { if (tail.getNext() == tail) tail = null; // removed last item else { Node head = tail.getNext(); tail.setNext(head.getNext()); } } }