Ejemplo n.º 1
0
  public void remove(Object inputData) {
    ListNode currentNode = (ListNode) this.firstNode;

    if (this.size == 0) {
      return;
    }

    boolean wasDeleted = false;

    /* Are we deleting the first node? */
    if (inputData.equals(currentNode.getData())) {

      /* Only one node in list, be careful! */
      if (currentNode.getNext() == null) {
        ((ListNode) this.firstNode).setData(null);
        this.firstNode = new ListNode();
        this.lastNode = (ListNode) this.firstNode;
        this.size--;
        return;
      }

      currentNode.setData(null);
      currentNode = currentNode.getNext();
      this.firstNode = currentNode;
      this.size--;
      return;
    }

    while (true) {
      /* If end of list, stop */
      if (currentNode == null) {
        wasDeleted = false;
        break;
      }

      /* Check if the data of the next is what we're looking for */
      ListNode nextNode = currentNode.getNext();
      if (nextNode != null) {
        if (inputData.equals(nextNode.getData())) {

          /* Found the right one, loop around the node */
          ListNode nextNextNode = nextNode.getNext();
          currentNode.setNext(nextNextNode);

          nextNode = null;
          wasDeleted = true;
          break;
        }
      }

      currentNode = currentNode.getNext();
    }

    if (wasDeleted) {
      this.size--;
    }
  }
 public static void printList(ListNode head) {
   ListNode current = head;
   while (current != null) {
     System.out.printf("[%d]", current.getData());
     current = current.getNext();
   }
   System.out.println("");
 }
Ejemplo n.º 3
0
  public void add(Object inputData) {
    ListNode node = new ListNode(inputData);

    /* Make sure we cater for the case where the list is empty */
    if (firstNode.getData() == null) {
      this.firstNode = node;
      this.lastNode = node;
    } else {
      this.lastNode.setNext(node);
      this.lastNode = node;
    }

    this.size++;
  }
Ejemplo n.º 4
0
  public Object elementAt(int inputPosition) {

    if (inputPosition >= this.size || inputPosition < 0) {
      return null;
    }

    ListNode currentNode = (ListNode) this.firstNode;

    for (int position = 0; position < inputPosition; position++) {
      currentNode = currentNode.getNext();
    }

    return currentNode.getData();
  }
Ejemplo n.º 5
0
  public String toString() {
    ListNode currentNode = (ListNode) this.firstNode;
    StringBuffer buffer = new StringBuffer();

    buffer.append("{");
    for (int i = 0; currentNode != null; i++) {
      if (i > 0) {
        buffer.append(",");
      }
      Object dataObject = currentNode.getData();

      buffer.append(dataObject == null ? "" : dataObject);
      currentNode = currentNode.getNext();
    }
    buffer.append("}");
    return buffer.toString();
  }
Ejemplo n.º 6
0
  public int indexOf(Object inputData) {
    ListNode currentNode = (ListNode) this.firstNode;
    int position = 0;
    boolean found = false;

    for (; ; position++) {
      if (currentNode == null) {
        break;
      }

      if (inputData.equals(currentNode.getData())) {
        found = true;
        break;
      }

      currentNode = currentNode.getNext();
    }

    if (!found) {
      position = -1;
    }

    return position;
  }