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

    return s;
  }
Exemple #4
0
 public void removeFront() throws StackEmptyException {
   if (empty()) {
     throw new StackEmptyException("Stack is Empty");
   } else {
     head.setNext(head.getNext());
     size--;
   }
 }
Exemple #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++;
    }
  }
Exemple #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;
 }
Exemple #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;
  }
Exemple #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;
  }