Skip to content

Commit bb2b2e1

Browse files
EASY/src/easy/ImplementStackUsingQueues.java
1 parent e78bcb8 commit bb2b2e1

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package easy;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
public class ImplementStackUsingQueues {
7+
class MyStack {
8+
9+
Queue<Integer> q = new LinkedList();
10+
11+
// Push element x onto stack.
12+
public void push(int x) {
13+
q.offer(x);
14+
for(int i = 1; i < q.size(); i++){
15+
q.offer(q.remove());
16+
}
17+
}
18+
19+
// Removes the element on top of the stack.
20+
public void pop() {
21+
q.poll();
22+
}
23+
24+
// Get the top element.
25+
public int top() {
26+
return q.peek();
27+
}
28+
29+
// Return whether the stack is empty.
30+
public boolean empty() {
31+
return q.isEmpty();
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)