class Stack<E> {
private final int size;
private int top;
private E[] elements;
public Stack() {
this(10);
}
public Stack(int s) {
size = s > 0 ? s : 10;
top = -1;
elements = (E[]) new Object[size]; // create array
}
public void push(E pushValue) {
if (top == size - 1) // if stack is full
throw new FullStackException(String.format("Stack is full, cannot push %s", pushValue));
elements[++top] = pushValue; // place pushValue on Stack
}
public E pop() {
if (top == -1) // if stack is empty
throw new EmptyStackException("Stack is empty, cannot pop");
return elements[top--]; // remove and return top element of Stack
}
}
class EmptyStackException extends RuntimeException {
public EmptyStackException() {
this("Stack is empty");
}
public EmptyStackException(String exception) {
super(exception);
}
}
class FullStackException extends RuntimeException {
public FullStackException() {
this("Stack is full");
}
public FullStackException(String exception) {
super(exception);
}
}
public class MainClass {
private static Double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };
private static Integer[] integerElements = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
// generic method pushes elements onto stack
public static <T> void testPush(String name, Stack<T> stack, T[] elements) {
try {
System.out.printf("\nPushing elements onto %s\n", name);
for (T element : elements) {
System.out.printf("%s ", element);
stack.push(element);
}
} catch (FullStackException fullStackException) {
System.out.println();
fullStackException.printStackTrace();
}
}
// generic method testPop pops elements from stack
public static <T> void testPop(String name, Stack<T> stack) {
try {
System.out.printf("\nPopping elements from %s\n", name);
T popValue;
while (true) {
popValue = stack.pop();
System.out.printf("%s ", popValue);
}
} catch (EmptyStackException emptyStackException) {
System.out.println();
emptyStackException.printStackTrace();
}
}
public static void main(String args[]) {
Stack rawTypeStack1 = new Stack(5);
Stack rawTypeStack2 = new Stack<Double>(5);
Stack<Integer> integerStack = new Stack(10);
testPush("rawTypeStack1", rawTypeStack1, doubleElements);
testPop("rawTypeStack1", rawTypeStack1);
testPush("rawTypeStack2", rawTypeStack2, doubleElements);
testPop("rawTypeStack2", rawTypeStack2);
testPush("integerStack", integerStack, integerElements);
testPop("integerStack", integerStack);
}
}
|