public void add(int n) { Node node = new Node(n); Node temp = head; if (numItems == 0 || node.getNum() < temp.getNum()) { node.setNext(temp); temp.setPrev(node); head = node; numItems++; } else { for (int i = 0; i <= numItems; i++) { if (node.getNum() == temp.getNum()) { temp.getPrev().setNext(node); node.setPrev(temp.getPrev()); node.setNext(temp); temp.setPrev(node); numItems++; break; } else if (node.getNum() <= temp.getNum() && temp.getNext() == null) { temp.setNext(node); node.setPrev(temp); node.setNext(null); numItems++; break; } else { temp = temp.getNext(); } } } }
/** * Moves the iterator one step backwards in the list. When it hits the end it will not move past * the start of the list. * * @return true if the iterator did not hit the start of the list, false otherwise */ public boolean moveToPrev() { boolean result = false; if (current != null) { if (current.getPrev() != null) { current = current.getPrev(); result = true; } } return result; }
public boolean delete(E d) { Node<E> n = current; if (current == null) { return false; } while (n.getNext() != current) { if (n.getData() == d) { n.getPrev().setNext(n.getNext()); n.getNext().setPrev(n.getPrev()); return true; } n = n.getNext(); } return false; }
public void insert(E d) { Node<E> n = new Node<E>(d); if (current == null) { current = n; } else if (current.getNext() == null) { n.setPrev(current); n.setNext(current); current.setPrev(n); current.setNext(n); current = n; } else { n.setNext(current); n.setPrev( current.getPrev()); // doesnt need to be in condition because if it is null, its null if (current.getPrev() != null) current.getPrev().setNext(n); current.setPrev(n); current = n; } }
@Override public E remove(IPosition<E> p) throws IllegalArgumentException { Node<E> node = validate(p); E oldValue = node.getElement(); Node<E> prevNode = node.getPrev(); Node<E> nextNode = node.getNext(); prevNode.setNext(nextNode); nextNode.setPrev(prevNode); node.setElement(null); node.setNext(null); node.setPrev(null); size--; return oldValue; }
public Item removeLast() { if (isEmpty()) { throw new NoSuchElementException(); } Node removedNode = last; last = last.getPrev(); if (last != null) { last.setNext(null); } else { first = null; } removedNode.setPrev(null); count--; return removedNode.getItem(); }
public void remove(int n) { Node node = new Node(n); Node temp = head; if (node.getNum() == temp.getNum()) { temp.getNext().setPrev(null); head = temp.getNext(); numItems--; } else { for (int i = 0; i < numItems; i++) { if (node.getNum() == temp.getNum()) { temp.getPrev().setNext(temp.getNext()); temp.getNext().setPrev(temp.getPrev()); numItems--; break; } else if (node.getNum() == temp.getNum() && temp.getNext() == null) { temp.getPrev().setNext(null); numItems--; break; } else { temp = temp.getNext(); } } } }
/** * return a COPY of solution. This should be : [x1,y1,x2,y2,x3,y3...] the coordinates of the * solution from start to end. Precondition : one of the solveXXX methods has already been called * (solveBFS OR solveDFS OR solveAStar) (otherwise an empty array is returned) Postcondition: the * correct solution is in the returned array */ public int[] solutionCoordinates() { MyQueue<Integer> listOfInts = new MyQueue<Integer>(); Node current = placesToGo.next(); while (current != null) { listOfInts.enqueue(current.getValue()[0]); listOfInts.enqueue(current.getValue()[1]); current = current.getPrev(); } solution = new int[listOfInts.size()]; for (int i = 0; i < solution.length; i++) { solution[i] = listOfInts.dequeue(); } return solution; }
public void back() { if (current.getPrev() != null) current = current.getPrev(); }
@Override public IPosition<E> before(IPosition<E> p) throws IllegalArgumentException { Node<E> node = validate(p); return node.getPrev(); }
@Override public IPosition<E> addLast(E e) { return addBetween(e, trailer.getPrev(), trailer); }
@Override public IPosition<E> last() { return trailer.getPrev(); }
@Override public IPosition<E> addBefore(IPosition<E> p, E e) throws IllegalArgumentException { Node<E> node = validate(p); return addBetween(e, node.getPrev(), node); }