package DataStructures.Stacks; import java.util.ArrayList; import java.util.EmptyStackException; /** * This class implements a Stack using an ArrayList. *
* A stack is exactly what it sounds like. An element gets added to the top of * the stack and only the element on the top may be removed. *
* This is an ArrayList Implementation of a stack, where size is not
* a problem we can extend the stack as much as we want.
*/
public class StackArrayList {
/**
* Driver Code
*/
public static void main(String[] args) {
StackArrayList stack = new StackArrayList();
assert stack.isEmpty();
for (int i = 1; i <= 5; ++i) {
stack.push(i);
assert stack.size() == i;
}
assert stack.size() == 5;
assert stack.peek() == 5 && stack.pop() == 5 && stack.peek() == 4;
/* pop elements at the top of this stack one by one */
while (!stack.isEmpty()) {
stack.pop();
}
assert stack.isEmpty();
try {
stack.pop();
assert false; /* this should not happen */
} catch (EmptyStackException e) {
assert true; /* this should happen */
}
}
/**
* ArrayList representation of the stack
*/
private ArrayList