Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.EmptyStackException;
- import java.util.Vector;
- public class VectorStack<T> implements Stack<T> {
- private Vector<T> stack;
- private static final int DEFAULT_CAPACITY=15;
- public VectorStack(int initialCapacity) {
- stack = new Vector<T>(initialCapacity);
- }
- public VectorStack() {
- this(DEFAULT_CAPACITY);
- }
- /**
- * Adds something to the top of the stack.
- *
- * @param obj - the object to be added.
- */
- @java.lang.Override
- public T push(T obj) {
- stack.add(obj);
- return obj;
- }
- /**
- * Returns a reference to the top of the stack.
- * Does not modify the stack.
- *
- * @return a reference to the top of the stack.
- */
- @java.lang.Override
- public T peek() {
- if (stack.isEmpty()) {
- throw new EmptyStackException();
- }
- return stack.get(stack.size() - 1);
- }
- /**
- * Removes the top element from the stack.
- *
- * @return a reference to the element that was on the top of the stack.
- */
- @java.lang.Override
- public T pop() {
- if (stack.isEmpty()) {
- throw new EmptyStackException();
- }
- T ret = stack.remove(stack.size() - 1);
- return ret;
- }
- /**
- * Determines if the stack is empty.
- *
- * @return true if the stack is empty.
- */
- @java.lang.Override
- public boolean empty() {
- return stack.isEmpty();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement