- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
Latest commit
36 lines (29 loc) · 752 Bytes
/
Copy pathArrayStack.java
File metadata and controls
36 lines (29 loc) · 752 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
publicclassArrayStack {
privatestaticfinalintMAX = 100;
privateinttop;
privateint[] stack = newint[MAX];
publicArrayStack() {
this.top = -1;
}
publicintpush(intn) {
if (top >= (MAX - 1)) {
return -1; // stack overflow
} else {
stack[++top] = n; // updating top position & pushing onto stack
returnn;
}
}
publicintpop() {
if (!isEmpty()) {
returnstack[top--];
} elsereturn -1; // stack underflow
}
publicintpeek() {
if (!isEmpty())
returnstack[top];
elsereturn -1; // stack underflow
}
publicbooleanisEmpty() {
returntop == -1;
}
}