Beispiel #1
0
  @Override
  public void remove(int index) {
    if (index < size()) {
      // check if it is the first element to remove
      if (index == 0) {
        head = head.getNextNode();
        if (head == null) {
          tail = null;
        }

      } else {
        Node<E> prev = head;
        for (int i = 0; i < index - 1; i++) {
          prev = prev.getNextNode();
        }
        prev.setNextNode(prev.getNextNode().getNextNode());

        if (index == size() - 1) {
          tail = prev;
        }
      }

      size--;
    } else throw new RuntimeException("Index out of bound.");
  }
Beispiel #2
0
  @Override
  public void add(int index, E element) {
    if (index < size()) {
      // create the new reference.node
      Node<E> newNode = new Node<E>(element, null);

      // check if we want to add it in the beginning
      if (index == 0) {
        newNode.setNextNode(head);
        head = newNode;
      } else {
        Node<E> prev = head;
        for (int i = 0; i < index - 1; i++) {
          prev = prev.getNextNode();
        }
        newNode.setNextNode(prev.getNextNode());
        prev.setNextNode(newNode);
      }
      size++;
    } else throw new RuntimeException("Index out of bound.");
  }
Beispiel #3
0
  @Override
  public void add(E element) {
    Node<E> newNode = new Node<E>(element, null);

    // check if the reference.list is empty
    if (head != null) {
      tail.setNextNode(newNode);
    } else {
      head = newNode;
    }
    tail = newNode;
    size++;
  }