|
6 | 6 | /**
|
7 | 7 | * This class implements a Stack using an ArrayList.
|
8 | 8 | *
|
9 |
| - * <p> |
10 |
| - * A stack is exactly what it sounds like. An element gets added to the top of |
11 |
| - * the stack and only the element on the top may be removed. |
12 |
| - * |
13 |
| - * <p> |
14 |
| - * This is an ArrayList Implementation of a stack, where size is not a problem |
15 |
| - * we can extend the stack as much as we want. |
| 9 | + * @param <T> the type of elements in this stack |
16 | 10 | */
|
17 |
| -public class StackArrayList { |
18 |
| - |
19 |
| - /** |
20 |
| - * Driver Code |
21 |
| - */ |
22 |
| - public static void main(String[] args) { |
23 |
| - StackArrayList stack = new StackArrayList(); |
24 |
| - assert stack.isEmpty(); |
25 |
| - |
26 |
| - for (int i = 1; i <= 5; ++i) { |
27 |
| - stack.push(i); |
28 |
| - assert stack.size() == i; |
29 |
| - } |
30 |
| - |
31 |
| - assert stack.size() == 5; |
32 |
| - assert stack.peek() == 5 && stack.pop() == 5 && stack.peek() == 4; |
| 11 | +public class StackArrayList<T> implements Stack<T> { |
33 | 12 |
|
34 |
| - /* pop elements at the top of this stack one by one */ |
35 |
| - while (!stack.isEmpty()) { |
36 |
| - stack.pop(); |
37 |
| - } |
38 |
| - assert stack.isEmpty(); |
39 |
| - |
40 |
| - try { |
41 |
| - stack.pop(); |
42 |
| - assert false; |
43 |
| - /* this should not happen */ |
44 |
| - } catch (EmptyStackException e) { |
45 |
| - assert true; |
46 |
| - /* this should happen */ |
47 |
| - } |
48 |
| - } |
| 13 | + private final ArrayList<T> stack; |
49 | 14 |
|
50 |
| - /** |
51 |
| - * ArrayList representation of the stack |
52 |
| - */ |
53 |
| - private ArrayList<Integer> stack; |
54 |
| - |
55 |
| - /** |
56 |
| - * Constructor |
57 |
| - */ |
58 | 15 | public StackArrayList() {
|
59 | 16 | stack = new ArrayList<>();
|
60 | 17 | }
|
61 | 18 |
|
62 |
| - /** |
63 |
| - * Adds value to the end of list which is the top for stack |
64 |
| - * |
65 |
| - * @param value value to be added |
66 |
| - */ |
67 |
| - public void push(int value) { |
| 19 | + @Override |
| 20 | + public void push(T value) { |
68 | 21 | stack.add(value);
|
69 | 22 | }
|
70 | 23 |
|
71 |
| - /** |
72 |
| - * Removes the element at the top of this stack and returns |
73 |
| - * |
74 |
| - * @return Element popped |
75 |
| - * @throws EmptyStackException if the stack is empty. |
76 |
| - */ |
77 |
| - public int pop() { |
| 24 | + @Override |
| 25 | + public T pop() { |
78 | 26 | if (isEmpty()) {
|
79 | 27 | throw new EmptyStackException();
|
80 | 28 | }
|
| 29 | + return stack.removeLast(); |
| 30 | + } |
81 | 31 |
|
82 |
| - /* remove the element on the top of the stack */ |
83 |
| - return stack.remove(stack.size() - 1); |
| 32 | + @Override |
| 33 | + public T peek() { |
| 34 | + if (isEmpty()) { |
| 35 | + throw new EmptyStackException(); |
| 36 | + } |
| 37 | + return stack.getLast(); |
84 | 38 | }
|
85 | 39 |
|
86 |
| - /** |
87 |
| - * Test if the stack is empty. |
88 |
| - * |
89 |
| - * @return {@code true} if this stack is empty, {@code false} otherwise. |
90 |
| - */ |
| 40 | + @Override |
91 | 41 | public boolean isEmpty() {
|
92 | 42 | return stack.isEmpty();
|
93 | 43 | }
|
94 | 44 |
|
95 |
| - /** |
96 |
| - * Return the element at the top of this stack without removing it from the |
97 |
| - * stack. |
98 |
| - * |
99 |
| - * @return the element at the top of this stack. |
100 |
| - */ |
101 |
| - public int peek() { |
102 |
| - if (isEmpty()) { |
103 |
| - throw new EmptyStackException(); |
104 |
| - } |
105 |
| - return stack.get(stack.size() - 1); |
| 45 | + @Override |
| 46 | + public void makeEmpty() { |
| 47 | + stack.clear(); |
106 | 48 | }
|
107 | 49 |
|
108 |
| - /** |
109 |
| - * Return size of this stack. |
110 |
| - * |
111 |
| - * @return size of this stack. |
112 |
| - */ |
| 50 | + @Override |
113 | 51 | public int size() {
|
114 | 52 | return stack.size();
|
115 | 53 | }
|
|
0 commit comments