-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack.java
71 lines (55 loc) · 1.18 KB
/
Stack.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
71
// Muhammad Naveed
// Integer Stack
// https://www.github.com/uxlabspk/JavaCodes/
public class Stack
{
private 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("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);
stack.pop();
stack.pop();
stack.pop();
stack.show();
}
}