Skip to content

Commit 4148be7

Browse files
authored
Update QueueUsingTwoStacks.java
1 parent 96502e2 commit 4148be7

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

QueueUsingTwoStacks.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,56 @@ The first line contains an integer q (the number of queries).
2323
import java.io.*;
2424
import java.util.*;
2525

26-
public class QueueUsingTwoStacks
26+
public class Solution
2727
{
2828
public static void main(String[] args)
2929
{
3030
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
31-
Scanner count = new Scanner(System.in);
32-
int numberOfOperations = count.nextInt();
31+
Scanner scan = new Scanner(System.in);
32+
int numberOfOperations = scan.nextInt();
3333

34-
Stack<Integer> stack1 = new Stack<>();
35-
Stack<Integer> stack2 = new Stack<>();
34+
Stack<Integer> quStack1 = new Stack<>();
35+
Stack<Integer> quStack2 = new Stack<>();
3636

3737
for(int i = 0; i < numberOfOperations; i++)
3838
{
39-
int typeOfOperation = count.nextInt();
40-
39+
int typeOfOperation = scan.nextInt();
4140
if(typeOfOperation == 1)
4241
{
43-
int addToQu = count.nextInt();
44-
stack1.push(addToQu);
42+
int x = scan.nextInt();
43+
quStack1.push(x);
4544
}
4645

4746
else if(typeOfOperation == 2)
4847
{
49-
if(stack2.isEmpty())
48+
if(quStack2.isEmpty())
5049
{
51-
while(!stack1.isEmpty())
50+
while(!quStack1.isEmpty())
5251
{
53-
stack2.push(stack1.pop());
52+
quStack2.push(quStack1.pop());
5453
}
5554
}
5655

57-
stack2.pop();
56+
quStack2.pop();
5857
}
5958

6059
else
6160
{
62-
if(typeOfOperation == 3 && stack2.isEmpty())
61+
if(typeOfOperation == 3)
6362
{
64-
while(!stack1.isEmpty())
63+
if(quStack2.isEmpty())
6564
{
66-
stack2.push(stack1.pop());
65+
while(!quStack1.isEmpty())
66+
{
67+
quStack2.push(quStack1.pop());
68+
}
6769
}
6870
}
6971

70-
System.out.println(stack2.peek());
72+
System.out.println(quStack2.peek());
7173
}
7274
}
7375

74-
count.close();
76+
scan.close();
7577
}
7678
}

0 commit comments

Comments
 (0)