|
3 | 3 | /**
|
4 | 4 | * This class implements a Stack using a regular array.
|
5 | 5 | *
|
6 |
| - * <p> |
7 |
| - * A stack is exactly what it sounds like. An element gets added to the top of |
8 |
| - * the stack and only the element on the top may be removed. This is an example |
9 |
| - * of an array implementation of a Stack. So an element can only be |
10 |
| - * added/removed from the end of the array. In theory stack have no fixed size, |
11 |
| - * but with an array implementation it does. |
| 6 | + * @param <T> the type of elements in this stack |
12 | 7 | */
|
13 |
| -public class StackArray { |
| 8 | +public class StackArray<T> implements Stack<T> { |
14 | 9 |
|
15 |
| - /** |
16 |
| - * Driver Code |
17 |
| - */ |
18 |
| - public static void main(String[] args) { |
19 |
| - // Declare a stack of maximum size 4 |
20 |
| - StackArray myStackArray = new StackArray(4); |
21 |
| - |
22 |
| - assert myStackArray.isEmpty(); |
23 |
| - assert !myStackArray.isFull(); |
24 |
| - |
25 |
| - // Populate the stack |
26 |
| - myStackArray.push(5); |
27 |
| - myStackArray.push(8); |
28 |
| - myStackArray.push(2); |
29 |
| - myStackArray.push(9); |
30 |
| - |
31 |
| - assert !myStackArray.isEmpty(); |
32 |
| - assert myStackArray.isFull(); |
33 |
| - assert myStackArray.peek() == 9; |
34 |
| - assert myStackArray.pop() == 9; |
35 |
| - assert myStackArray.peek() == 2; |
36 |
| - assert myStackArray.size() == 3; |
37 |
| - } |
38 |
| - |
39 |
| - /** |
40 |
| - * Default initial capacity. |
41 |
| - */ |
42 | 10 | private static final int DEFAULT_CAPACITY = 10;
|
43 | 11 |
|
44 |
| - /** |
45 |
| - * The max size of the Stack |
46 |
| - */ |
47 | 12 | private int maxSize;
|
48 |
| - |
49 |
| - /** |
50 |
| - * The array representation of the Stack |
51 |
| - */ |
52 |
| - private int[] stackArray; |
53 |
| - |
54 |
| - /** |
55 |
| - * The top of the stack |
56 |
| - */ |
| 13 | + private T[] stackArray; |
57 | 14 | private int top;
|
58 | 15 |
|
59 |
| - /** |
60 |
| - * init Stack with DEFAULT_CAPACITY |
61 |
| - */ |
| 16 | + @SuppressWarnings("unchecked") |
62 | 17 | public StackArray() {
|
63 | 18 | this(DEFAULT_CAPACITY);
|
64 | 19 | }
|
65 | 20 |
|
66 |
| - /** |
67 |
| - * Constructor |
68 |
| - * |
69 |
| - * @param size Size of the Stack |
70 |
| - */ |
| 21 | + @SuppressWarnings("unchecked") |
71 | 22 | public StackArray(int size) {
|
72 |
| - maxSize = size; |
73 |
| - stackArray = new int[maxSize]; |
74 |
| - top = -1; |
| 23 | + if (size <= 0) { |
| 24 | + throw new IllegalArgumentException("Stack size must be greater than 0"); |
| 25 | + } |
| 26 | + this.maxSize = size; |
| 27 | + this.stackArray = (T[]) new Object[size]; |
| 28 | + this.top = -1; |
75 | 29 | }
|
76 | 30 |
|
77 |
| - /** |
78 |
| - * Adds an element to the top of the stack |
79 |
| - * |
80 |
| - * @param value The element added |
81 |
| - */ |
82 |
| - public void push(int value) { |
83 |
| - if (!isFull()) { // Checks for a full stack |
84 |
| - top++; |
85 |
| - stackArray[top] = value; |
86 |
| - } else { |
| 31 | + @Override |
| 32 | + public void push(T value) { |
| 33 | + if (isFull()) { |
87 | 34 | resize(maxSize * 2);
|
88 |
| - push(value); // don't forget push after resizing |
89 | 35 | }
|
| 36 | + stackArray[++top] = value; |
90 | 37 | }
|
91 | 38 |
|
92 |
| - /** |
93 |
| - * Removes the top element of the stack and returns the value you've removed |
94 |
| - * |
95 |
| - * @return value popped off the Stack |
96 |
| - */ |
97 |
| - public int pop() { |
98 |
| - if (!isEmpty()) { // Checks for an empty stack |
99 |
| - return stackArray[top--]; |
| 39 | + @Override |
| 40 | + public T pop() { |
| 41 | + if (isEmpty()) { |
| 42 | + throw new IllegalStateException("Stack is empty, cannot pop element"); |
100 | 43 | }
|
101 |
| - |
102 |
| - if (top < maxSize / 4) { |
| 44 | + T value = stackArray[top--]; |
| 45 | + if (top + 1 < maxSize / 4 && maxSize > DEFAULT_CAPACITY) { |
103 | 46 | resize(maxSize / 2);
|
104 |
| - return pop(); // don't forget pop after resizing |
105 |
| - } else { |
106 |
| - System.out.println("The stack is already empty"); |
107 |
| - return -1; |
108 | 47 | }
|
| 48 | + return value; |
109 | 49 | }
|
110 | 50 |
|
111 |
| - /** |
112 |
| - * Returns the element at the top of the stack |
113 |
| - * |
114 |
| - * @return element at the top of the stack |
115 |
| - */ |
116 |
| - public int peek() { |
117 |
| - if (!isEmpty()) { // Checks for an empty stack |
118 |
| - return stackArray[top]; |
119 |
| - } else { |
120 |
| - System.out.println("The stack is empty, cant peek"); |
121 |
| - return -1; |
| 51 | + @Override |
| 52 | + public T peek() { |
| 53 | + if (isEmpty()) { |
| 54 | + throw new IllegalStateException("Stack is empty, cannot peek element"); |
122 | 55 | }
|
| 56 | + return stackArray[top]; |
123 | 57 | }
|
124 | 58 |
|
125 | 59 | private void resize(int newSize) {
|
126 |
| - int[] transferArray = new int[newSize]; |
127 |
| - |
128 |
| - for (int i = 0; i < stackArray.length; i++) { |
129 |
| - transferArray[i] = stackArray[i]; |
130 |
| - } |
131 |
| - // This reference change might be nice in here |
132 |
| - stackArray = transferArray; |
| 60 | + @SuppressWarnings("unchecked") T[] newArray = (T[]) new Object[newSize]; |
| 61 | + System.arraycopy(stackArray, 0, newArray, 0, top + 1); |
| 62 | + stackArray = newArray; |
133 | 63 | maxSize = newSize;
|
134 | 64 | }
|
135 | 65 |
|
136 |
| - /** |
137 |
| - * Returns true if the stack is empty |
138 |
| - * |
139 |
| - * @return true if the stack is empty |
140 |
| - */ |
141 |
| - public boolean isEmpty() { |
142 |
| - return (top == -1); |
| 66 | + public boolean isFull() { |
| 67 | + return top + 1 == maxSize; |
143 | 68 | }
|
144 | 69 |
|
145 |
| - /** |
146 |
| - * Returns true if the stack is full |
147 |
| - * |
148 |
| - * @return true if the stack is full |
149 |
| - */ |
150 |
| - public boolean isFull() { |
151 |
| - return (top + 1 == maxSize); |
| 70 | + @Override |
| 71 | + public boolean isEmpty() { |
| 72 | + return top == -1; |
152 | 73 | }
|
153 | 74 |
|
154 |
| - /** |
155 |
| - * Deletes everything in the Stack |
156 |
| - * |
157 |
| - * <p> |
158 |
| - * Doesn't delete elements in the array but if you call push method after |
159 |
| - * calling makeEmpty it will overwrite previous values |
160 |
| - */ |
161 |
| - public void makeEmpty() { // Doesn't delete elements in the array but if you call |
| 75 | + @Override public void makeEmpty() { // Doesn't delete elements in the array but if you call |
162 | 76 | top = -1; // push method after calling makeEmpty it will overwrite previous values
|
163 | 77 | }
|
164 | 78 |
|
165 |
| - /** |
166 |
| - * Return size of stack |
167 |
| - * |
168 |
| - * @return size of stack |
169 |
| - */ |
| 79 | + @Override |
170 | 80 | public int size() {
|
171 | 81 | return top + 1;
|
172 | 82 | }
|
|
0 commit comments