-
Notifications
You must be signed in to change notification settings - Fork 160
/
implement-queue-using-stacks.md
27 lines (21 loc) · 1.07 KB
/
implement-queue-using-stacks.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<p>Implement the following operations of a queue using stacks.</p>
<ul>
<li>push(x) -- Push element x to the back of queue.</li>
<li>pop() -- Removes the element from in front of queue.</li>
<li>peek() -- Get the front element.</li>
<li>empty() -- Return whether the queue is empty.</li>
</ul>
<p><b>Example:</b></p>
<pre>
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false</pre>
<p><b>Notes:</b></p>
<ul>
<li>You must use <i>only</i> standard operations of a stack -- which means only <code>push to top</code>, <code>peek/pop from top</code>, <code>size</code>, and <code>is empty</code> operations are valid.</li>
<li>Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.</li>
<li>You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).</li>
</ul>