-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack using array.java
More file actions
62 lines (60 loc) · 1.04 KB
/
Stack using array.java
File metadata and controls
62 lines (60 loc) · 1.04 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
public class Main {
public static void main(String[] args) {
Stack st = new Stack(5);
st.push(23);
st.push(12);
st.push(87);
st.push(98);
st.push(76);
st.push(55);
st.show();
System.out.println(" " + st.peek());
System.out.println();
st.pop();
st.show();
System.out.println(" " + st.peek());
System.out.println();
st.pop();
st.show();
System.out.println(" "+st.peek());
}
}
class Stack {
int top = -1;
int[] arr;
int len;
public Stack(int size) {
len = size;
arr = new int[size];
}
public boolean push(int data) {
if (top == len - 1) {
System.out.println("Stack is overflow");
return false;
} else {
top = top + 1;
arr[top] = data;
return true;
}
}
public boolean pop() {
if (top == -1) {
System.out.println("Stack is empty !");
return false;
}
arr[top] = 0;
top--;
return true;
}
public boolean isEmpty() {
return top < 0;
}
public int peek() {
return arr[top];
}
public void show() {
for (int i = top; i >= 0; i--) {
System.out.println("|" + arr[i] + "|");
}
}
}