-
Notifications
You must be signed in to change notification settings - Fork 2
/
1B.java
64 lines (64 loc) · 1.33 KB
/
1B.java
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
import java.util.*;
class arrayStack
{ int arr[];
int top, max;
arrayStack(int n)
{ max = n;
arr = new int[max];
top = -1; }
void push(int i) {
if (top == max - 1)
System.out.println("Stack Overflow");
else
arr[++top] = i;
}
void pop() {
if (top == -1) {
System.out.println("Stack Underflow"); }
else {
int element = arr[top--];
System.out.println("Popped Element: " + element); }
}
void display()
{ System.out.print("\nStack = ");
if (top == -1) {
System.out.print("Empty\n");
return; }
for (int i = top; i >= 0; i--)
System.out.print(arr[i] + " ");
System.out.println(); }
}
class Stack {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter Size of Integer Stack ");
int n = scan.nextInt();
boolean done = false;
arrayStack stk = new arrayStack(n);
char ch; do {
System.out.println("\nStack Operations");
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. display");
System.out.println("4. Exit");
int choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter integer element to push");
stk.push(scan.nextInt());
break;
case 2:
stk.pop();
break;
case 3:
stk.display();
break;
case 4:
done = true;
break;
default:
System.out.println("Wrong Entry \n ");
break;
}
} while (!done); }
}