package ProGAL.dataStructures; import java.util.Iterator; public class DLCyclicList implements Iterable{ protected DLNode entry; protected int size; public DLCyclicList() { entry = null; size = 0; } /** Returns true if the list is empty */ public boolean isEmpty() { return entry == null; } public DLNode getFirst() { return entry; } /** Finds the element containing given object. Worst-case O(n)-time. */ public DLNode findNode(Object obj) { DLNode elem = entry; while ((elem !=null) && (elem.obj != obj)) elem = elem.next; return elem; } public DLNode getEntry(){ return entry; } public void setEntry(DLNode n){ entry = n; } /** Adds the object before the entry of the list. Worst-case O(1)-time. */ public void pushBefore(T obj) { pushBefore(obj, entry); } /** Adds an object before the specified node in the list. Worst-case O(1)-time. */ public void pushBefore(T obj, DLNode n) { DLNode nd; if(n==null) nd = new DLNode(obj); else nd = new DLNode(obj,n.prev,n); if(entry==null) entry = nd; size++; } /** Adds an object after the specified node in the list. Worst-case O(1)-time. */ public void pushAfter(T obj, DLNode n) { DLNode nd; if(n==null) nd = new DLNode(obj); else nd = new DLNode(obj,n,n.next); if(entry==null) entry = nd; size++; } /** Deletes a node from the list and returns its object. Worst-case O(1)-time. */ public T delete(DLNode nd) { if(entry==null) throw new RuntimeException("Cannot delete from empty list"); if(entry == nd) entry = nd.next; nd.next.prev = nd.prev; nd.prev.next = nd.next; size--; return nd.obj; } /** Returns the number of elements in this cyclic list. */ public int getSize() { return size; } public Iterator iterator() { return new DLListIterator(this); } public static void main(String[] args) { DLCyclicList L = new DLCyclicList(); L.pushBefore(0, L.getEntry()); L.pushBefore(1, L.getEntry()); L.pushBefore(2, L.getEntry()); L.pushBefore(3, L.getEntry()); L.pushBefore(4, L.getEntry()); L.pushBefore(5, L.getEntry()); for(Integer i: L){ System.out.println(i); } } public static class DLNode { protected T obj; protected DLNode prev; protected DLNode next; public DLNode(T obj) { this.obj = obj; prev = this; next = this; } public DLNode(T obj, DLNode prev, DLNode next) { this.obj = obj; this.prev = prev; this.next = next; prev.next = this; next.prev = this; } /** Returns the object represented by the node */ public T getObject() { return obj; } /** Returns previous node of the doubly linked list */ public DLNode getPrev() { return prev; } /** Return next node of the doubly linked list */ public DLNode getNext() { return next; } /** Clears the node and returns its object */ public T clear() { prev = null; next = null; return obj; } } private static class DLListIterator implements java.util.Iterator { private DLCyclicList lst; private DLNode current; public DLListIterator(DLCyclicList lst) { this.lst = lst; current = null; } public boolean hasNext() { if (lst.isEmpty()) return false; else return current != lst.entry; } public T next() { if (hasNext()) { T ret = null; if (current == null) { ret = lst.entry.obj; current = lst.entry.next; }else{ ret = current.obj; current = current.next; } return ret; } else return null; } public void remove() {} } }