-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
58 lines (50 loc) · 947 Bytes
/
Stack.java
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
public class Stack {
private int size;
private int top;
private String[] array;
public Stack(int size) {
this.size = size;
this.array = new String[size];
this.top = -1;
}
public Boolean isEmpty() {
if (this.top == -1) {
return true;
}
return false;
}
public Boolean isFull() {
if (this.top == size - 1) {
return true;
}
return false;
}
public String pop() {
if (!this.isEmpty()) {
top--;
return array[top + 1];
}
return null;
}
public void push(String command) {
if (!this.isFull()) {
top++;
array[top] = command;
}
}
public String getTop() {
if (!this.isEmpty()) {
return array[top];
}
return null;
}
public void display() {
for (int i = 0; i < top + 1; i++) {
System.out.print(array[i] + " => ");
}
System.out.println();
}
public void setEmpty() {
this.top = -1;
}
}