Example #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;
  }
Example #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();
   }
 }
  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;
    }
  }
Example #4
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;
  }
  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;
  }
  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;
  }
Example #7
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;
 }
Example #8
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();
   }
 }