예제 #1
0
  public boolean add(int index, T value) {
    if (index < 0 || index > size) {
      throw new IndexOutOfBoundsException();
    }

    if (index == size) {
      add(value);
    } else {
      if (index == 0) {
        LNode ad = new LNode(value);
        ad.setNext(start);
        start = ad;
        if (start == null) {
          end = ad;
        }
      } else {
        LNode current = start;
        for (int i = 0; i < index - 1; i++) {
          current = current.getNext();
        }
        LNode ad = new LNode(value);
        LNode move = current.getNext();
        ad.setNext(move);
        current.setNext(ad);

        if (index == size) {
          end = current.getNext();
        }
      }
      size++;
    }

    return true;
  }
예제 #2
0
 public boolean add(int pos, T value) {
   LNode newNode = new LNode(value);
   LNode temp = start;
   LNode after;
   int index = 0;
   if (pos >= 0 && pos <= getSize()) {
     if (getSize() == 0 || pos == getSize()) {
       add(value);
     } else {
       while (index < pos - 1) {
         temp = temp.getNext();
         index++;
       }
       if (pos == 0) {
         after = temp;
         start = newNode;
         // new
         after.setPrevious(start);
         start.setNext(after);
         //		    start.getNext().setPrevious();
       } else {
         after = temp.getNext();
         // new
         after.setPrevious(newNode);
         newNode.setPrevious(temp);
         temp.setNext(newNode);
         newNode.setNext(after);
       }
       size++;
     }
     return true;
   } else {
     throw new IndexOutOfBoundsException();
   }
 }
예제 #3
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;
  }
예제 #4
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;
    }
  }
예제 #5
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 + "]";
 }
예제 #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;
 }
예제 #7
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 "";
 }
예제 #8
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;
 }
예제 #9
0
 public T next() {
   if (!hasNext()) {
     throw new NoSuchElementException();
   }
   T temp = current.getData();
   current = current.getNext();
   return temp;
 }
예제 #10
0
 public T next() {
   T ans;
   if (hasNext()) {
     ans = next.getValue();
     next = next.getNext();
     return ans;
   } else {
     throw new NoSuchElementException();
   }
 }
예제 #11
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();
  }
예제 #12
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;
 }
예제 #13
0
 public T next() {
   if (!hasNext()) {
     throw new NoSuchElementException();
   }
   if (current == null) {
     current = start;
   } else {
     current = current.getNext();
   }
   return current.getValue();
 }
예제 #14
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;
 }
예제 #15
0
  public boolean add(T value) {

    if (size == 0) {
      start = new LNode(value);
      end = start;
    } else {
      end.setNext(new LNode(value));
      end = end.getNext();
    }
    size++;
    return true;
  }
예제 #16
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;
 }
예제 #17
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;
 }
예제 #18
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;
 }
예제 #19
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;
  }
예제 #20
0
  public boolean add(int index, T value) {

    if (index > size || index < 0) {
      throw new IndexOutOfBoundsException();
    } else if (index == 0) {
      LNode current = start;
      start = new LNode(value);
      start.setNext(current);
      size++;
    } else if (index == size) {
      add(value);
    } else {
      LNode current = start;
      for (int i = 0; i < index - 1; i++) {
        current = current.getNext();
      }
      LNode tmp = current.getNext();
      current.setNext(new LNode(value));
      current.getNext().setNext(tmp);
      size++;
    }
    return true;
  }
예제 #21
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();
 }
예제 #22
0
 public void set(int pos, T value) {
   int index = 0;
   LNode temp = start;
   if (size == 0) {
     throw new NoSuchElementException();
   }
   if (pos < 0 || pos >= getSize()) {
     throw new IndexOutOfBoundsException();
   }
   while (index != pos) {
     index++;
     temp = temp.getNext();
   }
   temp.setValue(value);
 }
예제 #23
0
 public boolean add(T value) {
   if (getSize() == 0) {
     start = new LNode(value);
     end = start;
     size++;
   } else {
     // new
     LNode newNode = new LNode(value);
     newNode.setPrevious(end);
     end.setNext(newNode);
     //	    end.getNext().setPrevious(end);
     end = end.getNext();
     size++;
   }
   return true;
 }
예제 #24
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;
  }
예제 #25
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();
   }
 }
예제 #26
0
 public boolean hasNext() {
   if (current == null) {
     return true;
   }
   return current.getNext() != null;
 }