File tree Expand file tree Collapse file tree 1 file changed +5
-9
lines changed
src/main/java/com/williamfiset/algorithms/datastructures/stack Expand file tree Collapse file tree 1 file changed +5
-9
lines changed Original file line number Diff line number Diff line change 5
5
6
6
/** @author liujingkun */
7
7
public class ArrayStack <T > implements Stack <T > {
8
+ private int size ;
8
9
private int capacity ;
9
10
private Object [] data ;
10
- private int size ;
11
11
12
12
public ArrayStack () {
13
13
capacity = 16 ;
14
- size = 0 ;
15
14
data = new Object [capacity ];
16
15
}
17
16
@@ -28,17 +27,14 @@ public boolean isEmpty() {
28
27
@ Override
29
28
public void push (T elem ) {
30
29
if (size == capacity ) {
31
- adjustCap ();
30
+ increaseCapacity ();
32
31
}
33
32
data [size ++] = elem ;
34
33
}
35
34
36
- // adjust the capacity to store more element
37
- private void adjustCap () {
38
- if (capacity == Integer .MAX_VALUE ) {
39
- throw new OutOfMemoryError ();
40
- }
41
- capacity += capacity ;
35
+ // Increase the capacity to store more elements.
36
+ private void increaseCapacity () {
37
+ capacity *= 2 ;
42
38
data = Arrays .copyOf (data , capacity );
43
39
}
44
40
You can’t perform that action at this time.
0 commit comments