-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStackPavan055.java
More file actions
91 lines (84 loc) · 2.6 KB
/
Copy pathStackPavan055.java
File metadata and controls
91 lines (84 loc) · 2.6 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
import java.util.Scanner;
public class StackPavan055 {
int size = 100;
int[] sta = new int[size];
int tos = -1;
public void push(int data) {
if (full() == false) {
sta[++tos] = data;
} else {
System.out.println("The stack is full!");
}
}
public int pop() {
int e;
if (empty() == false) {
e = sta[tos--];
return e;
} else {
System.out.println("OOPS! Stack is empty, Illegal Pop Operation");
return -1;
}
}
public void display() {
System.out.println("Stack contents in LIFO order: ");
for (int i = tos; i >= 0; i--) {
System.out.println(sta[i]);
}
}
public boolean empty() {
if (tos == -1) {
return true;
} else {
return false;
}
}
public boolean full() {
if (tos == size - 1) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
System.out.println("A Stack of 100 integers is created for your use");
StackPavan055 st = new StackPavan055();
int opt;
char ch;
boolean flag;
Scanner sc = new Scanner(System.in);
do {
System.out.println("Enter your choice 1:Push 2:Pop 3:Display");
opt = sc.nextInt();
switch (opt) {
case 1:
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter the element to be pushed on to the stack");
int val = sc1.nextInt();
st.push(val);
break;
case 2:
int val1 = st.pop();
System.out.println("The element popped from top of the stack is " + val1);
break;
case 3:
st.display();
break;
default:
System.out.println("Invalid response!");
}
Scanner sc2 = new Scanner(System.in);
System.out.println("Would you like to continue?(Y/N)");
ch = Character.toUpperCase(sc2.next().charAt(0));
if (ch == 'Y') {
flag = true;
} else if (ch == 'N') {
flag = false;
System.out.println("Thank you for using the Stack application!!!");
} else {
System.out.println("Invalid response exiting!!!");
flag = false;
}
} while (flag);
}
}