-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA-lab3(b)-stackWithUSER-INPUT
More file actions
72 lines (64 loc) · 1.84 KB
/
DSA-lab3(b)-stackWithUSER-INPUT
File metadata and controls
72 lines (64 loc) · 1.84 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
import java.util.Scanner;
public class Main {
public int max;
public int[] arr;
public int tos = -1;
public Main(int max) {
this.max = max;
arr = new int[max];
}
public void push(int data) {
if (tos == max - 1) {
System.out.println("Overflow");
return;
}
tos++;
arr[tos] = data;
System.out.println("Push: " + data);
}
public void pop() {
if (tos == -1) {
System.out.println("Underflow");
return;
}
int popd = arr[tos];
tos--;
System.out.println("Pop: " + popd);
}
public void peep() {
if (tos == -1) {
System.out.println("Stack is empty");
return;
}
System.out.println("Top element (peep) " + arr[tos]);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter max size of stack: ");
int maxSize = sc.nextInt();
Main stack=new Main(maxSize);
while (true) {
System.out.println("\nChoose operation:");
System.out.println("1 - Push");
System.out.println("2 - Pop");
System.out.println("3 - Peep");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value to push: ");
int val = sc.nextInt();
stack.push(val);
break;
case 2:
stack.pop();
break;
case 3:
stack.peep();
sc.close();
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}
}