@Override public E previous() { if (!hasPrevious()) { throw new NoSuchElementException("No such element."); } else { prevItem = head; lastReturned = prevItem.getPrev(); prevItem = prevItem.getPrev(); head = prevItem; index--; return lastReturned.getData(); } }
@Override public E next() { if (!hasNext()) { throw new NoSuchElementException("No such element."); } else { nextItem = head; lastReturned = nextItem; nextItem = nextItem.getNext(); head = nextItem; index++; return lastReturned.getData(); } }
@Override public void set(E theData) { // does not work, sets data opposite where it should be if (lastReturned == null) { throw new IllegalStateException(); } else { lastReturned.setData(theData); } }
/** * Adds an item to the end of the list. * * @param data The Data that is to be stored in the node. */ public void add(E data) { DoubleLinkedNode<E> newNode = new DoubleLinkedNode<E>(data); if (this.head == null) { newNode.setNext(newNode); newNode.setPrev(newNode); this.head = newNode; size++; // if list is empty create a new node and insert } else { DoubleLinkedNode<E> temp = this.head.getPrev(); this.head.getPrev().setNext(newNode); this.head.setPrev(newNode); newNode.setPrev(temp); newNode.setNext(this.head); size++; } }
@Override public void add(E theData) { // adds but the old head moves to end of list DoubleLinkedNode<E> newNode = new DoubleLinkedNode<E>(theData); if (size == 0) { newNode.setPrev(newNode); newNode.setNext(newNode); // nextItem = newNode; // prevItem = newNode; head = newNode; size++; index++; lastReturned = null; } else if (size != 0) { nextItem = head; prevItem = head.getPrev(); newNode.setNext(nextItem); newNode.setPrev(prevItem); nextItem.setPrev(newNode); prevItem.setNext(newNode); head = nextItem; size++; index++; lastReturned = null; } }
@Override public String toString() { String str = "["; int index = 0; DoubleLinkedNode<E> curr = head; if (size == 0) { return "There is no one here to kill."; } else { while (index < size) { str += curr.getData(); curr = curr.getNext(); index++; if (index < size) { str += ", "; } } str += "]"; } return str; }
/** * Returns the data stored at the specified index. * * @param index The index determines the node whose data is returned. * @return Returns the data of the node at the index. */ public E get(int index) { // returns the data stored at the specified index int currIndex = 0; DoubleLinkedNode<E> currNode = this.head; E temp = null; if (index == 0) { // zero case temp = currNode.getData(); } else if (index > 0) { // positive while (currIndex != size) { if (currIndex != index % size) { currIndex++; currNode = currNode.getNext(); } else { temp = currNode.getData(); break; } } } else if (index < 0) { // negative while (currIndex != -size) { if (currIndex != index % size) { currIndex--; currNode = currNode.getPrev(); } else { temp = currNode.getData(); break; } } } return temp; }
@Override public void remove() { // removes first time but acts weird a second if (lastReturned == null) { throw new IllegalStateException(); } DoubleLinkedNode<E> tempPrev = lastReturned.getPrev(); DoubleLinkedNode<E> tempNext = lastReturned.getNext(); tempPrev.setNext(tempNext); tempNext.setPrev(tempPrev); size--; if (nextItem == lastReturned) { nextItem = tempNext; } else { index--; lastReturned = null; } }
/** * Which adds an item at the specified index. * * @param index The index in which the new Node is added. * @param data The data which is to be stored in the node. */ public void add(int index, E data) { int currIndex = 0; DoubleLinkedNode<E> currNode = this.head; DoubleLinkedNode<E> nextNode = this.head.getNext(); DoubleLinkedNode<E> prevNode = this.head.getPrev(); DoubleLinkedNode<E> newNode = new DoubleLinkedNode<E>(data); if (index == 0) { prevNode.setNext(newNode); currNode.setPrev(newNode); newNode.setPrev(prevNode); newNode.setNext(currNode); this.head = newNode; size++; } else if (index > 0) { while (currIndex != size) { if (currIndex != index % size) { currIndex++; currNode = currNode.getNext(); nextNode = nextNode.getNext(); prevNode = prevNode.getNext(); } else { newNode.setPrev(prevNode); newNode.setNext(currNode); prevNode.setNext(newNode); currNode.setPrev(newNode); currNode = newNode; size++; break; } } } else if (index < 0) { while (currIndex != -size) { if (currIndex != index % size) { currIndex--; currNode = currNode.getPrev(); prevNode = prevNode.getPrev(); nextNode = nextNode.getPrev(); } else { newNode.setNext(nextNode); newNode.setPrev(currNode); currNode.setNext(newNode); nextNode.setPrev(newNode); currNode = newNode; size++; break; } } } }
/** * Which removes and returns an item from the list. * * @param index Removes the node at the current index. * @return Returns the data of the removed node. */ public E remove(int index) { // which removes and returns an item from the list int currIndex = 0; DoubleLinkedNode<E> currNode = this.head; DoubleLinkedNode<E> nextNode = this.head.getNext(); DoubleLinkedNode<E> prevNode = this.head.getPrev(); E temp = null; if (index == 0) { temp = currNode.getData(); prevNode.setNext(currNode.getNext()); nextNode.setPrev(currNode.getPrev()); this.head = nextNode; size--; } else if (index > 0) { // positive while (currIndex != size) { if (currIndex != index % size) { currIndex++; currNode = currNode.getNext(); nextNode = nextNode.getNext(); prevNode = prevNode.getNext(); } else { temp = currNode.getData(); prevNode.setNext(currNode.getNext()); nextNode.setPrev(currNode.getPrev()); currNode = nextNode; size--; break; } } } else if (index < 0) { // negative while (currIndex != -size) { if (currIndex != index % size) { currIndex--; currNode = currNode.getPrev(); prevNode = prevNode.getPrev(); nextNode = nextNode.getPrev(); } else { temp = currNode.getData(); prevNode.setNext(currNode.getNext()); nextNode.setPrev(currNode.getPrev()); currNode = prevNode; size--; break; } } } return temp; }