232 - Implement Queue using Stacks

本文介绍了一种利用栈来实现队列的方法,并提供了一个具体的C++实现案例。该方法通过两个栈来模拟队列的基本操作,包括入队、出队、获取队首元素及判断队列是否为空。

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 toppeek/pop from topsize, 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).

实现如下:
class Queue {
public:
         stack<int> A, B;
        void push(int x) {
                A.push(x);
        }

        void pop(void) {
                while(!A.empty()) {
                        B.push(A.top());
                        A.pop();
                }
                B.pop();
                while(!B.empty()) {
                        A.push(B.top());
                        B.pop();
                }
        }

        int peek(void) {
                if( !A.empty() ) {
                        while(!A.empty()) {
                                B.push(A.top());
                                A.pop();
                        }
                        int p = B.top();
                        while(!B.empty()) {
                                A.push(B.top());
                                B.pop();
                        }
                        return p;
                }
                return 0;
        }

        bool empty(void) {
                return A.empty();
        }
};

int main() {
        Queue queue;
        queue.push(1);
        queue.push(2);
        queue.push(3);
        while(!queue.empty()) {
                cout << queue.peek();
                queue.pop();
        }
        //queue.pop();
        cout << queue.peek() << endl;
        if(queue.empty() == true)
                cout << "empty" << endl;
        else
                cout << "not empty" << endl;
        return 0;
}


typedef struct { int* stk; int stkSize; int stkCapacity; } Stack; Stack* stackCreate(int cpacity) { Stack* ret = malloc(sizeof(Stack)); ret->stk = malloc(sizeof(int) * cpacity); ret->stkSize = 0; ret->stkCapacity = cpacity; return ret; } void stackPush(Stack* obj, int x) { obj->stk[obj->stkSize++] = x; } void stackPop(Stack* obj) { obj->stkSize--; } int stackTop(Stack* obj) { return obj->stk[obj->stkSize - 1]; } bool stackEmpty(Stack* obj) { return obj->stkSize == 0; } void stackFree(Stack* obj) { free(obj->stk); } typedef struct { Stack* inStack; Stack* outStack; } MyQueue; MyQueue* myQueueCreate() { MyQueue* ret = malloc(sizeof(MyQueue)); ret->inStack = stackCreate(100); ret->outStack = stackCreate(100); return ret; } void in2out(MyQueue* obj) { while (!stackEmpty(obj->inStack)) { stackPush(obj->outStack, stackTop(obj->inStack)); stackPop(obj->inStack); } } void myQueuePush(MyQueue* obj, int x) { stackPush(obj->inStack, x); } int myQueuePop(MyQueue* obj) { if (stackEmpty(obj->outStack)) { in2out(obj); } int x = stackTop(obj->outStack); stackPop(obj->outStack); return x; } int myQueuePeek(MyQueue* obj) { if (stackEmpty(obj->outStack)) { in2out(obj); } return stackTop(obj->outStack); } bool myQueueEmpty(MyQueue* obj) { return stackEmpty(obj->inStack) && stackEmpty(obj->outStack); } void myQueueFree(MyQueue* obj) { stackFree(obj->inStack); stackFree(obj->outStack); } 作者:御三五 🥇 链接:https://leetcode.cn/problems/implement-queue-using-stacks/solutions/656774/tu-jie-guan-fang-tui-jian-ti-jie-yong-zh-4hru/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
最新发布
06-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值