Пример #1
0
  public T remove(int index) {
    if (index >= size || index < 0) {
      throw new IndexOutOfBoundsException();
    } else if (index == 0) {
      T removed = start.getValue();
      start = start.getNext();
      size--;
      return removed;
    } else {

      LNode current = start;
      int h;
      T removed = current.getValue();
      for (h = 0; h < index - 1; h++) {
        current = current.getNext();
      }
      if (index == size - 1) {
        end = current;
      }
      removed = current.getNext().getValue();

      current.setNext(current.getNext().getNext());
      size--;
      return removed;
    }
  }
Пример #2
0
 public String toString(boolean t) {
   if (t) {
     String s = "Head: " + start.getValue();
     LNode current = start;
     while (current.getNext() != null) {
       current = current.getNext();
     }
     s += "\tTail: " + current.getValue();
     return s;
   }
   return "";
 }
Пример #3
0
 public String toString(boolean extra) {
   if (extra) {
     return toString()
         + " size:"
         + getSize()
         + " head:"
         + start.getValue()
         + " tail:"
         + end.getValue();
   } else {
     return toString();
   }
 }
Пример #4
0
 public String toString() {
   String s = "[";
   LNode current = start;
   while (current != null) {
     if (current.getNext() == null) {
       s += current.getValue();
     } else {
       s += (current.getValue() + ",");
     }
     current = current.getNext();
   }
   s += "]";
   return s;
 }
Пример #5
0
 public T next() {
   T ans;
   if (hasNext()) {
     ans = next.getValue();
     next = next.getNext();
     return ans;
   } else {
     throw new NoSuchElementException();
   }
 }
Пример #6
0
 public T next() {
   if (!hasNext()) {
     throw new NoSuchElementException();
   }
   if (current == null) {
     current = start;
   } else {
     current = current.getNext();
   }
   return current.getValue();
 }
Пример #7
0
 public int indexOf(T value) {
   LNode current = start;
   for (int i = 0; i < size; i++) {
     if (current.getValue().equals(value)) {
       return i;
     } else {
       current = current.getNext();
     }
   }
   return -1;
 }
Пример #8
0
 public T set(int index, T newValue) {
   if (index >= size || index < 0) {
     throw new IndexOutOfBoundsException();
   }
   LNode current = start;
   for (int i = 0; i < index; i++) {
     current = current.getNext();
   }
   T oldValue = current.getValue();
   current.setValue(newValue);
   return oldValue;
 }
Пример #9
0
 public String toString() {
   String ans = "[";
   LNode temp = start;
   while (temp != null) {
     ans += temp.getValue();
     if (temp.getNext() != null) {
       ans += ", ";
     }
     temp = temp.getNext();
   }
   return ans + "]";
 }
Пример #10
0
 public int indexOf(T value) {
   LNode temp = start;
   int index = 0;
   while (index < getSize()) {
     if (temp.getValue().equals(value)) {
       return index;
     } else {
       temp = temp.getNext();
       index++;
     }
   }
   return -1;
 }
Пример #11
0
 public T get(int index) {
   if (index >= size || index < 0) {
     throw new IndexOutOfBoundsException();
   }
   LNode current = start;
   for (int i = 0; i < size; i++) {
     if (i == index) {
       return current.getValue();
     } else {
       current = current.getNext();
     }
   }
   return null;
 }
Пример #12
0
 public T get(int pos) {
   if (size == 0) {
     throw new NoSuchElementException();
   }
   if (pos < 0 || pos >= getSize()) {
     throw new IndexOutOfBoundsException();
   }
   int index = 0;
   LNode temp = start;
   while (index != pos) {
     index++;
     temp = temp.getNext();
   }
   return temp.getValue();
 }
Пример #13
0
 public T remove(int pos) {
   int index = 0;
   LNode temp = start;
   T removed;
   if (size == 0) {
     throw new NoSuchElementException();
   } else if (pos >= 0 && pos < getSize()) {
     while (index < pos - 1) {
       temp = temp.getNext();
       index++;
     }
     if (pos == 0) {
       removed = start.getValue();
       if (getSize() == 1) {
         start = new LNode(item);
         end = start;
       } else {
         // new
         start.getNext().setPrevious(null);
         start = start.getNext();
       }
       size--;
     } else {
       if (pos == getSize() - 1) {
         end = temp;
       }
       removed = temp.getNext().getValue();
       // new
       temp.getNext().getNext().setPrevious(temp);
       temp.setNext(temp.getNext().getNext());
       size--;
     }
     return removed;
   } else {
     throw new IndexOutOfBoundsException();
   }
 }