-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.java
More file actions
51 lines (42 loc) · 970 Bytes
/
StackArray.java
File metadata and controls
51 lines (42 loc) · 970 Bytes
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
public class StackArray<T> {
// Attributes
private ArrayDynamic<T> stack;
private int top;
private int capacity;
// Constructors
public StackArray() {
this.capacity = 10;
this.stack = new ArrayDynamic<T>(this.capacity);
this.top = -1;
}
public StackArray(int capacity) {
this.capacity = capacity;
this.stack = new ArrayDynamic<T>(this.capacity);
this.top = -1;
}
// Methods
public boolean isEmpty() {
return top == -1;
}
public int size() {
return top + 1;
}
public void push(T data) {
stack.set(++top, data);
}
public T pop() {
if (isEmpty()) {
return null;
}
return stack.remove(top--);
}
public T peek() {
if (isEmpty()) {
return null;
}
return stack.get(top);
}
public String toString() {
return stack.toString();
}
}