forked from sharunrajeev/YourFirstContribution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackarray.java
More file actions
76 lines (66 loc) · 1.85 KB
/
Stackarray.java
File metadata and controls
76 lines (66 loc) · 1.85 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
import java.util.Scanner;
public class Stackarray {
int[] arr = new int[80];
int top;
Stackarray(int top){
this.top = top;
}
void push(int d) {
if (top == 80) {
System.out.println("Stack overflow!");
return;
} else {
arr[top] = d;
top++;
}
}
void pop() {
if (top == 0) {
System.out.println("Stsck underflow!");
return;
} else {
top--;
System.out.println("Item popped.");
}
}
void display() {
System.out.println("Stack is: ");
for (int i = top - 1; i > -1; i--) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int p = 50;
System.out.println("Enter the number of elements: ");
int top = sc.nextInt();
Stackarray ob=new Stackarray(top);
System.out.println("Enter the elements: ");
for (int i = 0; i < top; i++) {
ob.arr[i] = sc.nextInt();
}
while (p > 0) {
System.out.println("\n1.Push\n2.Pop\n3.Display\n4.Exit");
System.out.println("Enter your option: ");
int ch = sc.nextInt();
switch (ch) {
case 1:
System.out.println("Enter the value to push:");
int d=sc.nextInt();
ob.push(d);
break;
case 2:
ob.pop();;
break;
case 3:
ob.display();;
break;
case 4:
p = 0;
break;
default:
System.out.println("Invalid option!!!");
}
}
}
}