class GenericStackArray<T> { private T[] stackArray; private int top; private int maxSize; @SuppressWarnings("unchecked") public GenericStackArray(int size) { this.maxSize = size; this.stackArray = (T[]) new Object[maxSize]; this.top = -1; } public void push(T item) { if (top < maxSize - 1) { stackArray[++top] = item; } else { System.out.println("Stack Overflow"); } } public T pop() { if (top >= 0) { return stackArray[top--]; } else { System.out.println("Stack Underflow"); return null; } } public boolean isEmpty() { return top == -1; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter