-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
118 lines (96 loc) · 3.01 KB
/
Stack.java
File metadata and controls
118 lines (96 loc) · 3.01 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import java.io.*;
class Stack {
private int max;
private int[] arr;
private int top;
public Stack(int m) {
max = m;
arr = new int[max];
top = -1;
}
public void push(int j) {
arr[++top] = j;
/*
* top++; arr[top]=j;
*/
}
public int pop() {
return arr[top--];
}
public int peek() {
return arr[top];
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == max - 1;
}
public void Display() {
for (int i = 0; i <= top; i++) {
System.out.println(arr[i] + " ");
}
}
}
class Main {
public static void main(String[] args) throws IOException {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int s, m, x, i = 1;
System.out.println("Enter the size of stack");
m = Integer.parseInt(in.readLine());
Stack st = new Stack(m);
System.out.println("Choose Stack Operations \n ");
while (i == 1) {
System.out.println("1.PUSH \n 2.POP \n 3.PEEK \n 4.Display");
s = Integer.parseInt(in.readLine());
switch (s) {
case 1: {
if (st.isFull()) {
System.out.println("Over flows");
break;
} else {
System.out.println("Enter the element to be added");
x = Integer.parseInt(in.readLine());
st.push(x);
System.out.println("Do you want to continue ? (yes=1/no=0)");
i = Integer.parseInt(in.readLine());
}
break;
}
case 2: {
if (st.isEmpty()) {
System.out.println("Under Flow");
break;
} else {
x = st.pop();
System.out.println(x);
}
break;
}
case 3: {
if (st.isEmpty()) {
System.out.println("Under Flow");
break;
} else {
x = st.peek();
System.out.println(x);
}
break;
}
case 4: {
if (st.isEmpty()) {
System.out.println("Stack Empty");
break;
} else {
st.Display();
}
break;
}
default:
System.out.println("Invalid Input..");
break;
}
}
}
}