Skip to content

Latest commit

 

History

History
73 lines (36 loc) · 1.41 KB

File metadata and controls

73 lines (36 loc) · 1.41 KB

中文文档

Description

Implement the following operations of a queue using stacks.

    <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>
    

Example:

MyQueue queue = new MyQueue();



queue.push(1);

queue.push(2);  

queue.peek();  // returns 1

queue.pop();   // returns 1

queue.empty(); // returns false

Notes:

    <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>
    

Solutions

Python3

Java

...