We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e78bcb8 commit bb2b2e1Copy full SHA for bb2b2e1
EASY/src/easy/ImplementStackUsingQueues.java
@@ -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