-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4_queue_using_two_stacks.java
More file actions
55 lines (47 loc) · 1.55 KB
/
day4_queue_using_two_stacks.java
File metadata and controls
55 lines (47 loc) · 1.55 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
54
55
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int noElements = in.nextInt();
Stack<Integer> one = new Stack<Integer>();
Stack<Integer> two = new Stack<Integer>();
int command;
for (int i = 0 ; i < noElements ; i++) {
command = in.nextInt();
if (command == 1)
{
one.push(in.nextInt());
}
else if (command == 2)
{
if(two.isEmpty())
{
while(!one.isEmpty())
{
two.push(one.pop());
}
}
if (!two.isEmpty()){
two.pop();
}
}
else if (command == 3)
{
if(two.isEmpty())
{
while(!one.isEmpty())
{
two.push(one.pop());
}
System.out.println(two.peek());
}
else
{
System.out.println(two.peek());
}
}
}
}
}