LeetCode-232:用栈实现队列

题目描述

232.用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

说明:

你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。

示例1:

输入:
[“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

题解

用栈的先进后出来实现队列的先进先出,显然,一个栈是不够用的,因此我们需要两个栈来完成此操作,我们可以用一个栈专门来实现队列的入队操作,将要入队的所有元素存入一个栈中,等到出队的时候,将该栈中的元素全部入栈到另一个空栈中,直到剩下最后一个元素,再将其弹出即可。这里要注意的是题目要求返回队列开头的元素,和出队操作几乎是相同的,唯一不同的是非空栈将全部元素入栈到空栈中,不留元素,然后再将入了元素的非空栈的栈顶元素返回即可。

题解代码

typedef int STDataType;

typedef struct Stack
{
    STDataType* a;
    int top;
    int capacity;
}Stack;

void StackInit(Stack* pst)
{
    assert(pst);

    pst->a = (STDataType*)malloc(sizeof(STDataType)*100);
    pst->top = 0;
    pst->capacity = 100;
}
void StackDestory(Stack* pst)
{
    assert(pst);
    free(pst->a);
    pst->a = NULL;
    pst->top = pst->capacity = 0;
}
void StackPush(Stack* pst,STDataType x)
{
    assert(pst);
    //说明栈已经满了
    if(pst->capacity == pst->top){
        STDataType* tmp =(STDataType*) realloc(pst,pst->capacity*2*sizeof(STDataType));
        if(tmp == NULL)
        {
            exit(-1);
        }
        pst->capacity *= 2;
        pst->a = tmp;
    }
    pst->a[pst->top] = x;
    pst->top++;
}
void StackPop(Stack* pst)
{
    assert(pst);

    assert(!StackEmpty(pst));
    --pst->top;
}
                        
                     
STDataType StackTop(Stack* pst)
{                          
    assert(pst);    

    assert(!StackEmpty(pst));
                         
    return pst->a[pst->top - 1];
}
                         
//NULL返回1,非空返回0
int StackEmpty(Stack* pst)
{                            
    assert(pst);
    return pst->top == 0 ? 1 : 0;
}  



typedef struct {
    //s1为进队列
    //s2为出队列
    Stack s1;
    Stack s2;
} MyQueue;

/** Initialize your data structure here. */

MyQueue* myQueueCreate() {
    MyQueue* q = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&q->s1);
    StackInit(&q->s2);
    return q;
}

/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
    StackPush(&obj->s1,x);
}

/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
    Stack* empty = &obj->s2;
    Stack* nonEmpty = &obj->s1;

    if(StackEmpty(empty))
    {
        //将非空的栈中的元素移动至空的栈中,直至剩下一个元素
        while(!StackEmpty(nonEmpty))
        {
            StackPush(empty,StackTop(nonEmpty));
            StackPop(nonEmpty);
        }
    }
    int ret = StackTop(empty);
    StackPop(empty);
    return ret;

}

/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
    Stack* empty = &obj->s2;
    Stack* nonEmpty = &obj->s1;

    if(StackEmpty(empty))
    {
        //将非空的栈中的元素移动至空的栈中,直至剩下一个元素
        while(!StackEmpty(nonEmpty))
        {
            StackPush(empty,StackTop(nonEmpty));
            StackPop(nonEmpty);
        }
    }
    return StackTop(empty);
}

/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->s1) && StackEmpty(&obj->s2);
}

void myQueueFree(MyQueue* obj) {
    StackDestory(&obj->s1);
    StackDestory(&obj->s2);

}

/**
 * Your MyQueue struct will be instantiated and called as such:
 * MyQueue* obj = myQueueCreate();
 * myQueuePush(obj, x);
 
 * int param_2 = myQueuePop(obj);
 
 * int param_3 = myQueuePeek(obj);
 
 * bool param_4 = myQueueEmpty(obj);
 
 * myQueueFree(obj);
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值