-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1.java
More file actions
53 lines (51 loc) · 1.59 KB
/
Day1.java
File metadata and controls
53 lines (51 loc) · 1.59 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
package Algorithm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.io.IOException;
public class Day1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] Stack = new int[N];
int top = -1;
for (int i = 0; i < N; i++) {
String cmd = br.readLine();
String[] parts = cmd.split(" ");
switch (parts[0]) {
case "push":
int M = Integer.parseInt(parts[1]);
top++;
Stack[top] = M;
break;
case "pop":
if (top == -1) {
System.out.println(-1);
} else {
System.out.println(Stack[top--]);
}
break;
case "size":
System.out.println(top+1);
break;
case "empty":
if(top == -1) {
System.out.println(1);
}else {
System.out.println(0);
}
break;
case "top":
if (top == -1) {
System.out.println(-1);
} else {
System.out.println(Stack[top]);
}
break;
default:
break;
}
}
}
}