-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement-stack-using-array
More file actions
57 lines (44 loc) · 1.27 KB
/
Copy pathimplement-stack-using-array
File metadata and controls
57 lines (44 loc) · 1.27 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
stack class
===========================================================================================================================================
package stackusingarray;
public class Stack {
int[] stack = new int[5];
int top = 0;
public void push(int item) {
if (stack.length == 0) {
System.out.println("No space availabe");
} else if (stack.length > 0) {
stack[top] = item;
top++;
} else {
System.out.print("number cant be added to the stack");
}
}
public void show() {
for (int i : stack) {
System.out.print(i+" ");
}
}
public void pop() {
if(stack.length>0){
System.out.println(stack[top]);
top--;
stack[top]=0;
top--;
}
}
}
==========================================================================================================================================
Runner class
=========================================================================================================================================
package stackusingarray;
import java.util.Scanner;
public class Runner {
public static void main(String[] args) {
Stack st= new Stack();
st.push(45);
st.show();
st.pop();
st.show();
}
}