-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.java
More file actions
59 lines (45 loc) · 1.19 KB
/
Copy pathStack.java
File metadata and controls
59 lines (45 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
package src;
// Muhammad Naveed
// Integer src.Stack
// https://www.github.com/uxlabspk/JavaCodes/
public class Stack {
private final int[] stackArray;
private int top;
Stack() {
stackArray = new int[10];
top = -1;
}
public boolean isEmpty() {
return (top == -1);
}
public void push(int value) {
stackArray[++top] = value;
}
public int pop() {
int popElement = stackArray[top];
if (isEmpty()) {
System.out.println("src.Stack is Empty.");
return 0;
} else {
stackArray[top--] = 0;
}
return popElement;
}
// Show is not the method of stack but, here we are just printing our stack.
public void show() {
for (int index = 0; index <= top; index++) {
System.out.println(stackArray[index]);
}
}
public static void main(String[] args) {
var stack = new Stack();
stack.push(4);
stack.push(5);
stack.push(2);
stack.push(1);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
stack.show();
}
}