Skip to content

Commit d56755b

Browse files
solves impliment queue using stacks
1 parent 678f89d commit d56755b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/MyQueue.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
public class MyQueue {
5+
6+
Queue<Integer> queue = new LinkedList<>();
7+
8+
/** Push element x to the back of queue. */
9+
public void push(int x) {
10+
queue.add(x);
11+
}
12+
13+
/** Removes the element from in front of queue and returns that element. */
14+
public int pop() {
15+
return queue.poll();
16+
}
17+
18+
/** Get the front element. */
19+
public int peek() {
20+
return queue.peek();
21+
}
22+
23+
/** Returns whether the queue is empty. */
24+
public boolean empty() {
25+
return queue.isEmpty();
26+
}
27+
}

0 commit comments

Comments
 (0)