Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class DLLIterator implements ListIterator<E> {
- private int index;
- private DoubleLinkedNode<E> node;
- private DoubleLinkedNode<E> lastNode;
- public DLLIterator() {
- index = 0;
- node = head;
- lastNode = null;
- }
- public DLLIterator(int index) throws IndexOutOfBoundsException {
- if (index < 0 || index >= size)
- throw new IndexOutOfBoundsException();
- this.index = index;
- node = head.getNext();
- for (int i=0; i < index; i++)
- node = node.getNext();
- }
- public boolean hasNext() {
- return size > 0;
- }
- public boolean hasPrevious() {
- return size > 0;
- }
- public E next() throws NoSuchElementException {
- if (size <= 0)
- throw new NoSuchElementException();
- node = nextFrom(node);
- lastNode = node;
- index = incIndex();
- return node.getValue();
- }
- public E previous() throws NoSuchElementException {
- if (size <= 0)
- throw new NoSuchElementException();
- if (node == head)
- node = head.getPrev();
- E value = node.getValue();
- lastNode = node;
- node = node.getPrev();
- index = decIndex();
- return value;
- }
- public int nextIndex() {
- if (size > 0)
- return incIndex();
- return -1;
- }
- public int previousIndex() {
- if (size > 0)
- return index;
- return -1;
- }
- public void remove() throws IllegalStateException {
- if (lastNode == null)
- throw new IllegalStateException();
- if (lastNode == node)
- node = node.getPrev();
- DoubleLinkedList.this.remove(lastNode);
- lastNode = null;
- }
- public void add(E element) {
- DoubleLinkedList.this.addBetween(element, node, node.getNext());
- next();
- lastNode = null;
- }
- public void set(E element) throws IllegalStateException {
- if (lastNode == null)
- throw new IllegalStateException();
- lastNode.setValue(element);
- }
- private int decIndex() {
- return (size + (index - 1)) % size;
- }
- private int incIndex() {
- return (index + 1) % size;
- }
- private DoubleLinkedNode<E> nextFrom(DoubleLinkedNode<E> node) {
- if (node == head.getPrev())
- return head.getNext();
- return node.getNext();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement