-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasyStack.java
More file actions
44 lines (33 loc) · 933 Bytes
/
EasyStack.java
File metadata and controls
44 lines (33 loc) · 933 Bytes
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
package test;
import java.util.*;
import java.io.*;
class EasyStack{
public static int getInt(String s) {
return Integer.parseInt(s);
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf= new BufferedReader (new InputStreamReader(System.in));
Stack stack = new Stack();
bf.readLine();
String line = null;
while((line = bf.readLine())!= null && line.length() > 0) {
int n = 0,e = 0;
if(line.length() == 1)
n = getInt(line);
else {
n = Character.getNumericValue(line.charAt(0));
e = getInt(line.substring(2,line.length()));
}
if(n == 1) stack.push(e);
else if(n == 2) {
if(!stack.empty()) stack.pop();
}
else{
if(!stack.empty())
System.out.println(stack.peek());
else
System.out.println("Empty!");
}
}
}
}