-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstackUsingArrays.java
More file actions
50 lines (44 loc) · 1.06 KB
/
stackUsingArrays.java
File metadata and controls
50 lines (44 loc) · 1.06 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
public class StackDemo {
private static final int capacity = 3;
int arr[] = new int[capacity];
int top = -1;
public void push(int pushedElement) {
if (top < capacity - 1) {
top++;
arr[top] = pushedElement;
System.out.println("Element " + pushedElement
+ " is pushed to Stack !");
printElements();
} else {
System.out.println("Stack Overflow !");
}
}
public void pop() {
if (top >= 0) {
top--;
System.out.println("Pop operation done !");
} else {
System.out.println("Stack Underflow !");
}
}
public void printElements() {
if (top >= 0) {
System.out.println("Elements in stack :");
for (int i = 0; i <= top; i++) {
System.out.println(arr[i]);
}
}
}
public static void main(String[] args) {
StackDemo stackDemo = new StackDemo();
stackDemo.pop();
stackDemo.push(23);
stackDemo.push(2);
stackDemo.push(73);
stackDemo.push(21);
stackDemo.pop();
stackDemo.pop();
stackDemo.pop();
stackDemo.pop();
}
}