Skip to content

Latest commit

 

History

History
85 lines (36 loc) · 1.39 KB

File metadata and controls

85 lines (36 loc) · 1.39 KB

中文文档

Description

Implement the following operations of a stack using queues.

    <li>push(x) -- Push element x onto stack.</li>
    
    <li>pop() -- Removes the element on top of the stack.</li>
    
    <li>top() -- Get the top element.</li>
    
    <li>empty() -- Return whether the stack is empty.</li>
    

Example:

MyStack stack = new MyStack();



stack.push(1);

stack.push(2);  

stack.top();   // returns 2

stack.pop();   // returns 2

stack.empty(); // returns false

Notes:

    <li>You must use <i>only</i> standard operations of a queue -- which means only <code>push to back</code>, <code>peek/pop from front</code>, <code>size</code>, and <code>is empty</code> operations are valid.</li>
    
    <li>Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.</li>
    
    <li>You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).</li>
    

Solutions

Python3

Java

...