public void removeSecondLast() { LLNode<T> tmp = new LLNode(); LLNode<T> prev = new LLNode(); tmp.setNext(head.getNext()); while (tmp.getNext() != tail) { prev.getNext(); } prev.setNext(prev.getNext()); tail = prev; }
public void removeBack() throws StackEmptyException { if (empty()) { throw new StackEmptyException("Stack is Empty"); } else { LLNode<T> tmp = new LLNode(); LLNode<T> prev = new LLNode(); tmp.setNext(head); while (tmp.getNext() != tail) { prev.getNext(); } prev.setNext(prev); tail = prev; } }
public String toString(T data) { String s = "[ "; while (head.getNext() != null) { s += (data + ", "); } return s; }
public void removeFront() throws StackEmptyException { if (empty()) { throw new StackEmptyException("Stack is Empty"); } else { head.setNext(head.getNext()); size--; } }
public void add(int index, String newVal) { if (index < 0 || index >= size()) throw new IndexOutOfBoundsException(); LLNode newNode = new LLNode(newVal, null); if (index == 0) add(newVal); else { LLNode tmp = _head; // create alias to head for (int i = 0; i < index - 1; i++) tmp = tmp.getNext(); newNode.setNext(tmp.getNext()); tmp.setNext(newNode); _size++; } }
public String toString() { String retStr = "HEAD->"; LLNode tmp = _head; // init tr while (tmp != null) { retStr += tmp.getCargo() + "->"; tmp = tmp.getNext(); } retStr += "NULL"; return retStr; }
public String get(int index) { if (index < 0 || index >= size()) throw new IndexOutOfBoundsException(); String retVal; LLNode tmp = _head; // create alias to head for (int i = 0; i < index; i++) tmp = tmp.getNext(); retVal = tmp.getCargo(); return retVal; }
public String remove(int index) { if (index < 0 || index >= size()) throw new IndexOutOfBoundsException(); String retVal; LLNode tmp = _head; // create alias to head if (index == 0) { retVal = _head.getCargo(); _head = _head.getNext(); } else { for (int i = 0; i < index - 1; i++) tmp = tmp.getNext(); retVal = tmp.getNext().getCargo(); tmp.setNext(tmp.getNext().getNext()); } _size--; return retVal; }