-
Notifications
You must be signed in to change notification settings - Fork 0
/
Implement stack using array
63 lines (51 loc) · 1.23 KB
/
Implement stack using array
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
//Implement stack using array
import java.util.*;
import java.io.*;
import java.lang.*;
class GfG {
public static void main(String args[])throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t>0) {
MyStack obj = new MyStack();
int Q = Integer.parseInt(read.readLine());
int k = 0;
String str[] = read.readLine().trim().split(" ");
while(Q>0) {
int QueryType = 0;
QueryType = Integer.parseInt(str[k++]);
if(QueryType == 1) {
int a = Integer.parseInt(str[k++]);
obj.push(a);
}
else if(QueryType == 2) {
System.out.print(obj.pop()+" ");
}
Q--;
}
System.out.println("");
t--;
}
}
}
class MyStack {
int top;
int arr[] = new int[1000];
MyStack() {
top = -1;
}
void push(int a) {
if(top == arr.length - 1) {
System.out.println("Overflow");
}
else {
arr[++top] = a;
}
}
int pop() {
if(top == -1) {
return -1;
}
return arr[top--];
}
}