-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical20.java
More file actions
52 lines (49 loc) · 1.8 KB
/
practical20.java
File metadata and controls
52 lines (49 loc) · 1.8 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
//Write a program to implement the operation of stack using array
import java.util.Stack;
import java.util.Scanner;
public class practical20 {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
Scanner scanner = new Scanner(System.in);
int choice, value;
do {
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Display");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value to push: ");
value = scanner.nextInt();
stack.push(value);
break;
case 2:
if (!stack.isEmpty()) {
System.out.println("Popped value: " + stack.pop());
} else {
System.out.println("Stack is empty.");
}
break;
case 3:
if (!stack.isEmpty()) {
System.out.println("Top value: " + stack.peek());
} else {
System.out.println("Stack is empty.");
}
break;
case 4:
System.out.println("Stack elements: " + stack);
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
scanner.close();
}
}