-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestStackArray.java
More file actions
71 lines (57 loc) · 2.25 KB
/
TestStackArray.java
File metadata and controls
71 lines (57 loc) · 2.25 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
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.*;
public class TestStackArray {
public static void main(String[] args) {
Scanner scann = new Scanner(System.in);
String choice = "";
System.out.println("Enter size");
int size = scann.nextInt();
stackArray SA = new stackArray(size);
scann.nextLine();
do {
System.out.println("------Welcome to Stack Menu---------------------------");
System.out.println(" Press [1] to push");
System.out.println(" Press [2] to pop");
System.out.println(" Press [3] to peek");
System.out.println(" Press [4] to display stack");
System.out.println(" Press [5] to Exit");
System.out.println("----------------------------------------------------------");
System.out.print(" Enter your choice: ");
choice = scann.nextLine();
switch (choice) {
case "1":
System.out.print(" Enter value to push to stack: ");
String value = scann.nextLine(); // Get user input for the value
if (SA.push(value)) {
System.out.println("Value pushed: " + value);
} else {
System.out.println("Stack is full. Cannot push.");
}
break;
case "2":
if (SA.pop()) {
System.out.println("Value popped.");
} else {
System.out.println("Stack is empty. Cannot pop.");
}
break;
case "3":
String topValue = SA.peek();
if (topValue != null) {
System.out.println("Current top value: " + topValue);
} else {
System.out.println("Stack is empty.");
}
break;
case "4":
SA.displayStack();
break;
case "5":
System.out.println(" Exiting Program...");
break;
default:
System.out.println(" Invalid choice. Please try again.");
break;
}
} while (!choice.equals("5"));
}
}