Esempio n. 1
0
 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;
 }
Esempio n. 2
0
 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;
   }
 }
Esempio n. 3
0
  public String toString(T data) {
    String s = "[ ";
    while (head.getNext() != null) {
      s += (data + ", ");
    }

    return s;
  }
Esempio n. 4
0
 public void removeFront() throws StackEmptyException {
   if (empty()) {
     throw new StackEmptyException("Stack is Empty");
   } else {
     head.setNext(head.getNext());
     size--;
   }
 }
Esempio n. 5
0
  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++;
    }
  }
Esempio n. 6
0
 public String toString() {
   String retStr = "HEAD->";
   LLNode tmp = _head; // init tr
   while (tmp != null) {
     retStr += tmp.getCargo() + "->";
     tmp = tmp.getNext();
   }
   retStr += "NULL";
   return retStr;
 }
Esempio n. 7
0
  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;
  }
Esempio n. 8
0
  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;
  }