Esempio n. 1
0
  /**
   * Returns an string representation of the elements in the stack satisfying the given predicate.
   * {@code predicate}
   *
   * @param predicate
   * @return String representation of the elements
   */
  public String printStack(Predicate<T> predicate) {
    StringBuilder builder = new StringBuilder();

    for (StackNode<T> tempElement = startElement;
        tempElement != null;
        tempElement = tempElement.getNext()) {
      if (predicate.apply(tempElement.getData())) {
        builder.append(tempElement.getData().toString());
        builder.append("\n");
      }
    }

    return builder.toString();
  }
Esempio n. 2
0
  /**
   * Removes the topmost element of the stock.
   *
   * @return Topmost element in the stack
   */
  public T pop() {
    // Store first node in temp and retrieve new node
    StackNode<T> tempElement = startElement;
    startElement = tempElement.getNext();

    size--;
    return tempElement.getData();
  }
Esempio n. 3
0
  /**
   * Searches for an object in the stack. If this object exists return true, otherwise return false.
   *
   * @param object Object of type T to insert in stack
   * @return True if element is found
   */
  public boolean peek(T object) {
    for (StackNode<T> tempElement = startElement;
        tempElement.getNext() != null;
        tempElement = tempElement.getNext()) {
      if (object.equals(tempElement.getData())) {
        return true;
      }
    }

    return false;
  }