@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; } }
/** * 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; }
/** * 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 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; } } } }