leetcode---用队列实现栈

一、问题描述

使用队列实现栈的下列操作:

       push(x) -- 元素 x 入栈
       pop() -- 移除栈顶元素
       top() -- 获取栈顶元素
       empty() -- 返回栈是否为空

注意:你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-stack-using-queues

二、问题分析

1.栈的特点:栈只能在栈顶进行入栈和出栈操作,同时遵循“先入后出”原则。

2.队列的特点:队列的一端用于入队、另一端用于出队;遵循“先进先出”;

3.队列实现栈的思路分析

两个队列,一个队列保持空另一个队列保持非空,当入栈时向非空的队列中直接入队元素,当出栈时将非空队列中的元素出最后一个外其余全部出队并入控队中。

三、代码实现

typedef struct QueueNode
{
    struct QueueNode*next;
    int val;
}QueueNode; 
typedef struct Queue
{
    QueueNode* front;
    QueueNode* rear;
}Queue;
//初始化队列
void QueueInit(Queue* q)
{
    q->front = NULL;
    q->rear = NULL;
}
//判断队空
int QueueEmpty(Queue* q)
{
    return !q->front;
}
//入队
void QueuePush(Queue* q,int x)
{
    QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
    newNode->val = x;
    newNode->next = NULL;
    if(QueueEmpty(q))
    {
        q->front = newNode;
        q->rear = newNode;
    }
    else
    {
        q->rear->next = newNode;
        q->rear = q->rear->next;
    }
}
//获取队列元素个数
int QueueSize(Queue* q)
{
    int size = 0;
    QueueNode* curr = q->front;
    while(curr)
    {
        size++;
        curr = curr->next;
    }
    return size;
}
//出队
void QueuePop(Queue* q)
{
    if(!QueueEmpty(q))
    {
        QueueNode* front = q->front;
        q->front = q->front->next;
        free(front);
    }
}
//获取对尾元素
int QueueRear(Queue* q)
{
    if(!QueueEmpty(q))
    {
        return q->rear->val;
    }
    return -1;
}
//销毁队列
void QueueDstory(Queue* q)
{
    QueueNode* curr = q->front;
    while(curr)
    {
        q->front = q->front->next;
        free(curr);
        curr = q->front;
    }
}

typedef struct {
    Queue q1;
    Queue q2;
} MyStack;

/** Initialize your data structure here. */

MyStack* myStackCreate() {
    MyStack* st = (MyStack*)malloc(sizeof(MyStack));
    QueueInit(&st->q1);
    QueueInit(&st->q2);
    return st;
}

/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
    //往非空队列中入队元素,实现入栈
    if(!QueueEmpty(&obj->q1))
    {
        QueuePush(&obj->q1,x);
    }
    else
    {
        QueuePush(&obj->q2,x);
    }
}

/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
    Queue* emptyQueue = &obj->q1;
    Queue* nonEmptyQueue = &obj->q2;
    if(QueueEmpty(&obj->q2))
    {
        emptyQueue = &obj->q2;
        nonEmptyQueue = &obj->q1;
    }
    //让非空队列中的出最后一个元素的所有元素出队并进入空队列中
    while(QueueSize(nonEmptyQueue) > 1)
    {
        //队头元素入队
        QueuePush(emptyQueue,nonEmptyQueue->front->val);
        //队头元素出队,迭代
        QueuePop(nonEmptyQueue);
    }
    int top = nonEmptyQueue->front->val;
    QueuePop(nonEmptyQueue);
    return top;
}

/** Get the top element. */
int myStackTop(MyStack* obj) {
    if(!QueueEmpty(&obj->q1))
    {
        return QueueRear(&obj->q1);
    }
    return QueueRear(&obj->q2);
}

/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
    if(QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2))
    {
        return true;
    }
    return false;
}

void myStackFree(MyStack* obj) {
    QueueDstory(&obj->q1);
    QueueDstory(&obj->q2);
    free(obj);
}

/**
 * Your MyStack struct will be instantiated and called as such:
 * MyStack* obj = myStackCreate();
 * myStackPush(obj, x);
 
 * int param_2 = myStackPop(obj);
 
 * int param_3 = myStackTop(obj);
 
 * bool param_4 = myStackEmpty(obj);
 
 * myStackFree(obj);
*/

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂嘚程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值