-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.java
More file actions
94 lines (77 loc) · 2.37 KB
/
StackArray.java
File metadata and controls
94 lines (77 loc) · 2.37 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
public class StackArray {
int[] stack;
int top;
int size;
// Constructor
StackArray(int size) {
this.size = size;
stack = new int[size];
top = -1;
}
// PUSH operation (split form)
void push(int value) {
if (top == size - 1) {
System.out.println("Stack Overflow");
} else {
top++; // move top
stack[top] = value; // insert element
System.out.println(value + " pushed into stack");
}
}
// POP operation (split form)
void pop() {
if (top == -1) {
System.out.println("Stack Underflow");
} else {
int removed = stack[top]; // read top element
top--; // move top down
System.out.println(removed + " popped from stack");
}
}
// TRAVERSE operation
void traverse() {
if (top == -1) {
System.out.println("Stack is empty");
} else {
System.out.println("---- Stack ----");
for (int i = top; i >= 0; i--) {
System.out.println(stack[i]);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter stack size: ");
int size = sc.nextInt();
StackArray st = new StackArray(size);
while (true) {
System.out.println("\n--- MENU ---");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Traverse");
System.out.println("4. Exit");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter element to push: ");
int value = sc.nextInt();
st.push(value);
break;
case 2:
st.pop();
break;
case 3:
st.traverse();
break;
case 4:
System.out.println("Exiting...");
sc.close();
return;
default:
System.out.println("Invalid choice");
}
}
}
}