Advertisement
kyle1320

Untitled

Nov 2nd, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. private class DLLIterator implements ListIterator<E> {
  2.     private int index;
  3.     private DoubleLinkedNode<E> node;
  4.     private DoubleLinkedNode<E> lastNode;
  5.  
  6.     public DLLIterator() {
  7.         index = 0;
  8.         node = head;
  9.         lastNode = null;
  10.     }
  11.  
  12.     public DLLIterator(int index) throws IndexOutOfBoundsException {
  13.         if (index < 0 || index >= size)
  14.             throw new IndexOutOfBoundsException();
  15.  
  16.         this.index = index;
  17.         node = head.getNext();
  18.         for (int i=0; i < index; i++)
  19.             node = node.getNext();
  20.     }
  21.  
  22.     public boolean hasNext() {
  23.         return size > 0;
  24.     }
  25.  
  26.     public boolean hasPrevious() {
  27.         return size > 0;
  28.     }
  29.  
  30.     public E next() throws NoSuchElementException {
  31.         if (size <= 0)
  32.             throw new NoSuchElementException();
  33.  
  34.         node = nextFrom(node);
  35.         lastNode = node;
  36.         index = incIndex();
  37.  
  38.         return node.getValue();
  39.     }
  40.  
  41.     public E previous() throws NoSuchElementException {
  42.         if (size <= 0)
  43.             throw new NoSuchElementException();
  44.  
  45.         if (node == head)
  46.             node = head.getPrev();
  47.  
  48.         E value = node.getValue();
  49.         lastNode = node;
  50.         node = node.getPrev();
  51.         index = decIndex();
  52.  
  53.         return value;
  54.     }
  55.  
  56.     public int nextIndex() {
  57.         if (size > 0)
  58.             return incIndex();
  59.         return -1;
  60.     }
  61.  
  62.     public int previousIndex() {
  63.         if (size > 0)
  64.             return index;
  65.         return -1;
  66.     }
  67.  
  68.     public void remove() throws IllegalStateException {
  69.         if (lastNode == null)
  70.             throw new IllegalStateException();
  71.         if (lastNode == node)
  72.             node = node.getPrev();
  73.  
  74.         DoubleLinkedList.this.remove(lastNode);
  75.         lastNode = null;
  76.     }
  77.  
  78.     public void add(E element) {
  79.         DoubleLinkedList.this.addBetween(element, node, node.getNext());
  80.         next();
  81.         lastNode = null;
  82.     }
  83.  
  84.     public void set(E element) throws IllegalStateException {
  85.         if (lastNode == null)
  86.             throw new IllegalStateException();
  87.         lastNode.setValue(element);
  88.     }
  89.  
  90.     private int decIndex() {
  91.         return (size + (index - 1)) % size;
  92.     }
  93.  
  94.     private int incIndex() {
  95.         return (index + 1) % size;
  96.     }
  97.  
  98.     private DoubleLinkedNode<E> nextFrom(DoubleLinkedNode<E> node) {
  99.         if (node == head.getPrev())
  100.             return head.getNext();
  101.         return node.getNext();
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement