/** * Pushes the object on the stack. * * @param object */ public void push(T object) { StackNode<T> stackNode = new StackNode<T>(object); stackNode.setNext(startElement); startElement = stackNode; size++; }
/** * 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(); }
void printStack() { StackNode current; current = start; while (current != null) { System.out.println("->" + current.getValue()); current = current.getNext(); } }
void push(char value) { StackNode newNode = new StackNode(value, null); if (start == null) { start = newNode; } else { newNode.setNext(start); } start = newNode; }
public void push(int item) { StackNode node = new StackNode(item); if (top == null) { top = node; return; } node.next = top; top = node; }
public void print() { StackNode<T> currentNode = topNode; while (currentNode != null) { System.out.println(currentNode.getValue()); currentNode = currentNode.getLastNode(); } }
/** * 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; }
public void push(T value) { StackNode<T> stackNode = new StackNode<T>(value); if (topNode == null) { topNode = stackNode; } else { stackNode.setLastNode(topNode); topNode.setNextNode(stackNode); topNode = stackNode; } }
public int findHeight() { int height = 0; StackNode<T> currentNode = topNode; while (currentNode != null) { height++; currentNode = currentNode.getLastNode(); } return height; }
/** * 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(); }
void pop() { if (start == null) { System.out.println("Stack is empty"); } else { start = start.getNext(); } }
public T peek() { if (topNode != null) { return topNode.getValue(); } return null; }
public T pop() { if (topNode != null) { StackNode newTopNode = topNode.getLastNode(); T topNodeValue = topNode.getValue(); newTopNode.setNextNode(null); topNode.setLastNode(null); topNode = newTopNode; return topNodeValue; } return null; }
public T findMax() { StackNode<T> currentNode = topNode; T max = topNode.getValue(); while (currentNode != null) { T currentValue = currentNode.getValue(); if (currentValue > max) { max = currentValue; } currentNode = currentNode.getLastNode(); } return max; }
public T findMin(T currentMin, T max) { StackNode<T> currentNode = topNode; T min = max; while (currentNode != null) { T currentValue = currentNode.getValue(); if (currentMin == null) { if (currentValue < min) { min = currentValue; } } else { if (currentValue < min && currentValue > currentMin) { min = currentValue; } } currentNode = currentNode.getLastNode(); } return min; }
/** @param the element to push */ public void push(E e) { StackNode<E> node = new StackNode(e); node.next = top; top = node; }
char peep() { return start.getValue(); }