File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
232-implement-queue-using-stacks Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ class MyQueue {
2
+ private Stack <Integer > s1 ;
3
+ private Stack <Integer > s2 ;
4
+
5
+ public MyQueue () {
6
+ s1 = new Stack <>();
7
+ s2 = new Stack <>();
8
+ }
9
+
10
+ public void push (int x ) {
11
+ while (!s1 .isEmpty ()) {
12
+ s2 .push (s1 .pop ());
13
+ }
14
+ s1 .push (x );
15
+ while (!s2 .isEmpty ()) {
16
+ s1 .push (s2 .pop ());
17
+ }
18
+ }
19
+
20
+ public int pop () {
21
+ return s1 .pop ();
22
+ }
23
+
24
+ public int peek () {
25
+ return s1 .peek ();
26
+ }
27
+
28
+ public boolean empty () {
29
+ return s1 .isEmpty ();
30
+ }
31
+ }
32
+
33
+
34
+ /**
35
+ * Your MyQueue object will be instantiated and called as such:
36
+ * MyQueue obj = new MyQueue();
37
+ * obj.push(x);
38
+ * int param_2 = obj.pop();
39
+ * int param_3 = obj.peek();
40
+ * boolean param_4 = obj.empty();
41
+ */
You can’t perform that action at this time.
0 commit comments