[LeetCode]232.Implement Queue using Stacks

简介:

题目

Implement the following operations of a queue using stacks.

push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.

Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
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.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

思路

两个栈模拟队列

代码

/*---------------------------------------
*   日期:2015-08-01
*   作者:SJF0115
*   题目: 232.Implement Queue using Stacks
*   网址:https://leetcode.com/problems/implement-queue-using-stacks/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

class Queue {
public:
    // Push element x to the back of queue.
    void push(int x) {
        firStack.push(x);
    }

    // Removes the element from in front of queue.
    void pop(void) {
        if(!secStack.empty()){
            secStack.pop();
            return;
        }//if
        if(firStack.empty()){
            return;
        }//if
        while(!firStack.empty()){
            secStack.push(firStack.top());
            firStack.pop();
        }//while
        secStack.pop();
    }

    // Get the front element.
    int peek(void) {
        if(!secStack.empty()){
            return secStack.top();
        }//if
        //no pop or peek operations will be called on an empty queue
        if(firStack.empty()){
            return -1;
        }//if
        while(!firStack.empty()){
            secStack.push(firStack.top());
            firStack.pop();
        }//while
        return secStack.top();
    }

    // Return whether the queue is empty.
    bool empty(void) {
        return firStack.empty() && secStack.empty();
    }
private:
    stack<int> firStack;
    stack<int> secStack;
};

int main(){
    Queue q;
    for(int i = 0;i < 5;++i){
        q.push(i);
    }//for
    while(!q.empty()){
        cout<<q.peek()<<" ";
        q.pop();
    }//while
    q.push(8);
    cout<<q.peek()<<endl;
    q.pop();
    cout<<q.peek()<<endl;
    return 0;
}
目录
相关文章
|
存储
LeetCode 232. Implement Queue using Stacks
使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部。 pop() -- 从队列首部移除元素。 peek() -- 返回队列首部的元素。 empty() -- 返回队列是否为空。
192 0
LeetCode 232. Implement Queue using Stacks
LeetCode 225. Implement Stack using Queues
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈; pop() -- 移除栈顶元素; top() -- 获取栈顶元素; empty() -- 返回栈是否为空
234 0
LeetCode 225. Implement Stack using Queues
LeetCode 225:用队列实现栈 Implement Stack using Queues
题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement the following operations of a stack using queues.
1222 0
LeetCode 232:用栈实现队列 Implement Queue using Stacks
题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部。 pop() -- 从队列首部移除元素。 peek() -- 返回队列首部的元素。 empty() -- 返回队列是否为空。
776 0
|
算法 Java C语言
LeetCode 28:实现strStr() Implement strStr()
公众号:爱写bug(ID:icodebugs) 作者:爱写bug 实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。
837 0

热门文章

最新文章