-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackJ.java
71 lines (62 loc) · 2.07 KB
/
StackJ.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
import java.util.*;
class Stack {
Scanner s = new Scanner(System.in);
int size = 5;
int[] stackArray = new int[size]; // Correct array initialization
int top = -1;
void push() {
if (top == size - 1)
System.out.println("Stack Overflow");
else {
System.out.println("Enter the item to be pushed:");
int item = s.nextInt();
stackArray[++top] = item; // Corrected the variable name and assignment
}
}
void pop() {
if (top == -1)
System.out.println("Stack Underflow");
else {
int item = stackArray[top--]; // Pop item and decrement top
System.out.println("Popped item: " + item); // Optional: print popped item
}
}
void print() {
System.out.println("Stack elements are: ");
for (int i = top; i >= 0; i--)
System.out.println(stackArray[i]);
}
}
public class Stackj {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Stack st = new Stack(); // Corrected initialization
int c;
do {
System.out.println("Choose an option");
System.out.println("1->PUSH\t2->POP\t3->PRINT\t4->EXIT");
c = s.nextInt();
switch (c) {
case 1:
System.out.println("You have chosen push operation");
st.push();
break;
case 2:
System.out.println("You have chosen pop operation");
st.pop();
break;
case 3:
System.out.println("You have chosen print operation");
st.print();
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Wrong choice!! Try again");
break;
}
} while (c != 4);
s.close(); // Close scanner to avoid resource leak
}
}