-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperStack.java
More file actions
47 lines (41 loc) · 1.04 KB
/
SuperStack.java
File metadata and controls
47 lines (41 loc) · 1.04 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
import java.util.Scanner;
public class SuperStack {
static int[] stack = new int[10000];
static int top = -1;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = Integer.parseInt(scan.nextLine());
String split[][] = new String[n][];
for (int i = 0; i < n; i++) {
String s = scan.nextLine();
split[i] = s.split(" ");
}
for (int i = 0; i < n; i++) {
System.out.println(performStackOps(split[i]));
}
}
private static String performStackOps(String[] split) {
switch (split[0]) {
case "push":
if (top + 1 == Integer.MAX_VALUE)
return "Stack Overflow";
stack[++top] = Integer.parseInt(split[1]);
break;
case "pop":
if (top - 1 == -1)
return "Stack UnderFlow";
--top;
break;
case "inc":
int offset = top - Integer.parseInt(split[1]);
int inc = Integer.parseInt(split[2]);
for (; offset <= top; offset++) {
stack[offset] += inc;
}
break;
default:
return "Invalid Operation";
}
return top == -1 ? "EMPTY" : String.valueOf(stack[top]);
}
}