File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed
Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments