Beispiel #1
0
  public T remove(int index) {
    if (index < 0 || index > size - 1) {
      throw new IndexOutOfBoundsException();
    }
    T s = start.getData();
    if (index == 0) {
      start = start.getNext();
      if (index == size - 1) {
        end = start;
      }
    } else {
      LNode current = start;
      for (int i = 0; i < index - 1; i++) {
        current = current.getNext();
      }
      LNode temp = current.getNext();
      s = temp.getData();

      temp = temp.getNext();
      current.setNext(temp);
      if (index == size - 1) {
        end = current;
      }
    }
    size--;

    return s;
  }
Beispiel #2
0
 public String toString() {
   String total = "[ ";
   if (size != 0) {
     total += start.getData();
   }
   LNode tracker = start;
   for (int i = 0; i < size - 1; i++) {
     tracker = tracker.getNext();
     total += ", " + tracker.getData();
   }
   total += " ]";
   return total;
 }
Beispiel #3
0
 public T next() {
   if (!hasNext()) {
     throw new NoSuchElementException();
   }
   T temp = current.getData();
   current = current.getNext();
   return temp;
 }
Beispiel #4
0
  public T get(int index) {
    if (index < 0 || index > size - 1) {
      throw new IndexOutOfBoundsException();
    }

    LNode current = start;
    for (int i = 0; i < index; i++) {
      current = current.getNext();
    }
    return current.getData();
  }
Beispiel #5
0
  public String toString(boolean choice) {
    String total = "";
    if (choice) {
      String copy = "[ ";
      if (size != 0) {
        copy += start.getData();
      }

      LNode current = start;
      for (int i = 0; i < size - 1; i++) {
        current = current.getNext();
        copy += ", " + current.getData();
      }
      copy += " ]";
      total += "Start: " + (String) start.getData() + " End: " + (String) end.getData();

      total = copy + total;
    }
    return total;
  }
Beispiel #6
0
 public int indexOf(T value) {
   LNode current = start;
   int counter = 0;
   while (current.getData() != value && current.getNext() != null) {
     current = current.getNext();
     counter++;
   }
   if (current.getNext() == null) {
     return -1;
   }
   return counter;
 }
Beispiel #7
0
  public T set(int index, T indexValue) {
    if (index < 0 || index > size - 1) {
      throw new IndexOutOfBoundsException();
    }
    LNode current = start;
    // int tracker=0;
    for (int i = 0; i < index; i++) {
      current = current.getNext();
    }
    T save = current.getData();
    current.setData(indexValue);

    return save;
  }