Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. import java.util.EmptyStackException;
  2. import java.util.Vector;
  3.  
  4. public class VectorStack<T> implements Stack<T> {
  5. private Vector<T> stack;
  6. private static final int DEFAULT_CAPACITY=15;
  7.  
  8. public VectorStack(int initialCapacity) {
  9. stack = new Vector<T>(initialCapacity);
  10. }
  11.  
  12. public VectorStack() {
  13. this(DEFAULT_CAPACITY);
  14. }
  15. /**
  16. * Adds something to the top of the stack.
  17. *
  18. * @param obj - the object to be added.
  19. */
  20. @java.lang.Override
  21. public T push(T obj) {
  22. stack.add(obj);
  23. return obj;
  24. }
  25.  
  26. /**
  27. * Returns a reference to the top of the stack.
  28. * Does not modify the stack.
  29. *
  30. * @return a reference to the top of the stack.
  31. */
  32. @java.lang.Override
  33. public T peek() {
  34. if (stack.isEmpty()) {
  35. throw new EmptyStackException();
  36. }
  37. return stack.get(stack.size() - 1);
  38. }
  39.  
  40. /**
  41. * Removes the top element from the stack.
  42. *
  43. * @return a reference to the element that was on the top of the stack.
  44. */
  45. @java.lang.Override
  46. public T pop() {
  47. if (stack.isEmpty()) {
  48. throw new EmptyStackException();
  49.  
  50.  
  51. }
  52. T ret = stack.remove(stack.size() - 1);
  53. return ret;
  54.  
  55. }
  56.  
  57. /**
  58. * Determines if the stack is empty.
  59. *
  60. * @return true if the stack is empty.
  61. */
  62. @java.lang.Override
  63. public boolean empty() {
  64. return stack.isEmpty();
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement