-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathStackImpl.java
More file actions
66 lines (56 loc) · 1.19 KB
/
StackImpl.java
File metadata and controls
66 lines (56 loc) · 1.19 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
public class StackImpl {
int top = 0;
int MAX = 5;
int[] a = new int[MAX];
private boolean isFull() {
if(top == MAX) {
return true;
}
return false;
}
public void insert(int val) {
if(isFull()) {
System.out.println("Stack is full");
return;
}
a[top++] = val;
}
private boolean isEmpty() {
if(top == 0) {
return true;
}
return false;
}
public int remove() {
if(isEmpty()) {
System.out.println("Stack is Empty");
return Integer.MIN_VALUE;
}
return a[--top];
}
public int top() {
if(isEmpty()) {
System.out.println("Stack is Empty");
return Integer.MIN_VALUE;
}
return a[top - 1];
}
public static void main(String[] args) {
StackImpl a = new StackImpl();
a.insert(12);
a.insert(5);
a.insert(34);
a.insert(42);
a.insert(91);
a.insert(9);
System.out.println(a.remove());
a.insert(9);
System.out.println("Top:" + a.top());
System.out.println(a.remove());
System.out.println(a.remove());
System.out.println(a.remove());
System.out.println(a.remove());
System.out.println(a.remove());
System.out.println(a.remove());
}
}