Skip to content

Commit c07fac8

Browse files
authored
Create QueueUsingTwoStacks.java
1 parent 647e956 commit c07fac8

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

QueueUsingTwoStacks.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
3+
*/
4+
5+
import java.io.*;
6+
import java.util.*;
7+
8+
public class QueueUsingTwoStacks
9+
{
10+
public static void main(String[] args)
11+
{
12+
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
13+
Scanner count = new Scanner(System.in);
14+
int numberOfOperations = count.nextInt();
15+
16+
Stack<Integer> stack1 = new Stack<>();
17+
Stack<Integer> stack2 = new Stack<>();
18+
19+
for(int i = 0; i < numberOfOperations; i++)
20+
{
21+
int typeOfOperation = count.nextInt();
22+
23+
if(typeOfOperation == 1)
24+
{
25+
int addToQu = count.nextInt();
26+
stack1.push(addToQu);
27+
}
28+
29+
else if(typeOfOperation == 2)
30+
{
31+
if(stack2.isEmpty())
32+
{
33+
while(!stack1.isEmpty())
34+
{
35+
stack2.push(stack1.pop());
36+
}
37+
}
38+
39+
stack2.pop();
40+
}
41+
42+
else
43+
{
44+
if(typeOfOperation == 3 && stack2.isEmpty())
45+
{
46+
while(!stack1.isEmpty())
47+
{
48+
stack2.push(stack1.pop());
49+
}
50+
}
51+
52+
System.out.println(stack2.peek());
53+
}
54+
}
55+
56+
count.close();
57+
}
58+
}

0 commit comments

Comments
 (0)