-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.java
53 lines (42 loc) · 1.01 KB
/
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
public class Stack {
final private int len = 5;
private int[] stack = new int[len];
private int stack_pointer;
Stack() {
stack_pointer = -1;
}
protected void push(int val) {
if (isFull()) {
System.out.println("Stack is full");
} else {
stack_pointer+=1;
stack[stack_pointer] = val;
}
}
protected void pop() {
if (isEmpty()) {
System.out.println("Stack is empty");
} else {
stack_pointer -= 1;
}
}
protected void peek() {
if (isEmpty()) {
System.out.println("Stack is empty");
} else {
System.out.println(stack[stack_pointer]);
}
}
protected boolean isFull() {
if (stack_pointer == len - 1) {
return true;
}
return false;
}
protected boolean isEmpty() {
if (stack_pointer == -1) {
return true;
}
return false;
}
}