-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStackArray.java
65 lines (60 loc) · 1.16 KB
/
StackArray.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
package practise;
import java.util.*;
public class StackArray {
Scanner sc = new Scanner(System.in);
int size = 5;
int [] stackArray = new int[size];
int top = -1;
public void push(int data) {
if(isFull()) System.out.println("STACK IS FULL");
else {
top++;
stackArray[top]=data;
}
}
public int pop() {
if(isEmpty()) System.out.println("STACK IS EMPTY");
else {
int data = stackArray[top];
stackArray[top]=0;
top--;
return data;
}
return -1;
}
public int peek() {
return stackArray[top];
}
public int size() {
return top;
}
public boolean isEmpty() {
return top<0;
}
public boolean isFull() {
return size<top;
}
public void show() {
for(int i=size-1; i>=0; i--) {
System.out.println(stackArray[i]);
}
}
public static void main(String[] args) {
StackArray myStack = new StackArray();
myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);
myStack.push(50);
System.out.println(myStack.peek());
myStack.pop();
System.out.println(myStack.peek());
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
System.out.println();
myStack.show();
}
}