Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.

Commit 22d8bb3

Browse files
authored
Create 4-Solution-PD.java
1 parent a0bdee9 commit 22d8bb3

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Stacks and Queues/4-Solution-PD.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
public class MyQueue {
3+
4+
private Stack s1;
5+
private Stack s2;
6+
7+
public MyQueue() {
8+
s1 = new Stack();
9+
s2 = new Stack();
10+
}
11+
12+
public void add(int x) {
13+
s1.push(x);
14+
}
15+
16+
public int size() {
17+
return s1.size();
18+
}
19+
20+
public int capacity() {
21+
return s1.capacity();
22+
}
23+
24+
public int remove() {
25+
if(s2.isEmpty()) {
26+
while(!s1.isEmpty()) {
27+
s2.push(s1.pop());
28+
}
29+
}
30+
31+
int temp = -1;
32+
33+
if(!s2.isEmpty()) {
34+
temp = s2.pop();
35+
}
36+
37+
return temp;
38+
}
39+
40+
public int top() {
41+
if(s2.isEmpty()) {
42+
while(!s1.isEmpty()) {
43+
s2.push(s1.pop());
44+
}
45+
}
46+
47+
int temp = -1;
48+
49+
if(!s2.isEmpty()) {
50+
temp = s2.peek();
51+
}
52+
53+
return temp;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)