-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathStackApp.java
executable file
·71 lines (60 loc) · 1.44 KB
/
StackApp.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
59
60
61
62
63
64
65
66
67
68
69
70
class Stack {
private int[] a;
private int ele;
private int size;
public Stack(int size) {
a = new int[size];
this.size = size;
ele = -1;
}
public void push(int val) {
if (isFull()) {
System.out.println("Stack is Full");
} else {
ele++;
a[ele] = val;
}
}
public int pop() {
if (isEmpty()) {
System.out.println("Stack is Empty");
return -1;
} else {
int val = a[ele];
ele--;
return val;
}
}
public boolean isEmpty() {
if(ele == -1) return true;
else return false;
}
public boolean isFull() {
if(ele == size - 1) return true;
return false;
}
public void display() {
for(int i=0; i<=ele; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}
class StackApp {
public static void main(String[] args) {
Stack theStack = new Stack(10);
theStack.push(20);
theStack.push(40);
theStack.push(60);
theStack.push(80);
while( !theStack.isEmpty() ) {
int value = theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}
/*
A stack allows access to only one data item: the last item inserted.
If you remove this item, you can access the next-to-last item inserted, and so on.
*/