-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImplement Stack.java
More file actions
48 lines (41 loc) · 1.26 KB
/
Copy pathImplement Stack.java
File metadata and controls
48 lines (41 loc) · 1.26 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
// https://www.hackerrank.com/contests/smart-interviews/challenges/si-implement-stack
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
int[] a = new int[t];
int top = 0;
while (t-- > 0) {
String s = br.readLine();
String[] res = s.split(" ");
Stack < Integer > st = new Stack < > ();
if (res[0].equals("push")) {
int x = Integer.parseInt(res[1]);
Push(a, top++, x);
} else {
if (isempty(a, top)) {
System.out.println("Empty");
} else {
Pop(a, top);
top--;
System.out.println(Peek(a, top));
}
}
}
}
static void Push(int[] a, int top, int ele) {
a[top++] = ele;
}
static void Pop(int[] a, int top) {
a[top] = 0;
top--;
}
static int Peek(int[] a, int top) {
return a[top];
}
static boolean isempty(int[] a, int top) {
return top == 0;
}
}