-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
70 lines (55 loc) · 1.29 KB
/
ArrayStack.java
File metadata and controls
70 lines (55 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package Stack;
import org.omg.CORBA.Object;
import java.util.EmptyStackException;
public class ArrayStack<T> implements _Stack<T> {
//container
private T[] arr;
//length
private int size;
//top
private int index = -1;
private int init = 100;
public ArrayStack(int capacity) {
this.arr = (T[]) new Object[capacity];
}
public ArrayStack() {
this.arr = (T[]) new Object[this.init];
}
public int getSize() {
return this.size;
}
public void getLargeStack(int capacity) {
if (capacity < size)
return;
T[] old = this.arr;
arr = (T[]) new Object[capacity];
for (int i = 0; i < size; i++) {
arr[i] = old[i];
}
}
@Override
public boolean isEmpty() {
return this.index == -1;
}
@Override
public void push(T data) {
if (arr.length == size) {
getLargeStack(size*2 );
}
arr[index++] = data;
size++;
}
@Override
public T peek() {
if (isEmpty())
throw new NullPointerException();
return arr[index];
}
@Override
public T pop() {
if (isEmpty())
throw new NullPointerException();
size--;
return arr[index--];
}
}