【力扣-225】用队列实现栈

🖊作者 : D. Star.
📘专栏 : 数据结构
😆今日小tips : 有些家里可能会有两个同样名字的 WiFi,而其中一个名字的最后有个 5G,那么,这个 5G 和常说的 5G 信号是一回事吗?并不是,无线路由器上的 5G 指的是 WiFi 的工作频段,手机上的 5G 指的是第五代蜂窝网络技术标准。与普通的 2.4G 频段相比,无线路由器上的 5G 频段的频宽更宽,如果宽带够快,那么就可以有更好的网络体验。但是 5G 频段的覆盖范围要比 2.4G 频段小一些,穿墙能力也要弱一些。

请添加图片描述

🔎题目链接:

【力扣-225】用队列实现栈

🔎题目:

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。

示例:

输入:
[“MyStack”, “push”, “push”, “top”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
----解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False

🔎分块解释:

✔队列函数:

typedef int QDataType;

typedef struct QueueNode
{
    QDataType data;
    struct QueueNode* next;
}QNode;

typedef struct Queue
{
    QNode* head;
    QNode* tail;
    int size;
}Queue;

//初始化
void InitQueue(Queue* pq)
{
    assert(pq);

    pq->head = NULL;
    pq->tail = NULL;
    pq->size = 0;
}

//销毁
void DestroyQueue(Queue* pq)
{
    assert(pq);
    QNode* cur = pq->head;
    while (cur)
    {
        QNode* del = cur;
        cur = cur->next;
        free(del);
        del = NULL;
    }
    pq->head = pq->tail = NULL;
    pq->size = 0;
}

//判断是否为空
bool EmptyQueue(Queue* pq)
{
    assert(pq);

    return pq->head == NULL && pq->tail == NULL;
}

//入队
void PushQueue(Queue* pq, QDataType x)
{
    assert(pq);
    QNode* newNode = (QNode*)malloc(sizeof(QNode));
    if (newNode == NULL)
    {
        perror("malloc fail");
        exit(-1);
    }
    else
    {
        newNode->data = x;
        newNode->next = NULL;
    }

    if (pq->tail == NULL)
    {
        pq->head = pq->tail = newNode;
    }
    else
    {
        pq->tail->next = newNode;
        pq->tail = newNode;
    }
    pq->size++;
}

//出队
void PopQueue(Queue* pq)
{
    assert(pq);
    assert(!EmptyQueue(pq));

    if (pq->head != pq->tail)
    {
        QNode* del = pq->head;
        pq->head = pq->head->next;
        free(del);
    }
    else
    {
        free(pq->head);
        pq->head = pq->tail = NULL;
    }

    pq->size--;
}

//取头
QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(!EmptyQueue(pq));

    return pq->head->data;
}

//取尾
QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(!EmptyQueue(pq));

    return pq->tail->data;
}

//队列的长度
int SizeQueue(Queue* pq)
{
    assert(pq);
    return pq->size;
}

✔用队列实现栈:

typedef struct {
    Queue q1;
    Queue q2;
} MyStack;
MyStack* myStackCreate() {
    MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
    if (obj == NULL)
    {
        perror("malloc fail");
        exit(-1);
    }
    InitQueue(&obj->q1);
    InitQueue(&obj->q2);

    return obj;
}

//将元素 x 压入栈顶
void myStackPush(MyStack* obj, int x) {
    assert(obj);
    //法一:
    Queue* empty = &obj->q1;
    Queue* noempty = &obj->q2;
    if (!EmptyQueue(&obj->q1)) {
        empty = &obj->q2;
        noempty = &obj->q1;
    }

    PushQueue(noempty, x);

    //法二:
        // if(!EmptyQueue(&obj->q1))
        // {
        //    PushQueue(&obj->q1,x);
        // }
        // else
        // {
        //    PushQueue(&obj->q2,x);
        // }
}

//移除并返回栈顶元素。
int myStackPop(MyStack* obj) {
    assert(obj);

    Queue* empty = &obj->q1;
    Queue* noempty = &obj->q2;
    if (!EmptyQueue(&obj->q1)) {
        empty = &obj->q2;
        noempty = &obj->q1;
    }

    //将不为空的n-1个移到为空中
    while (noempty->size > 1) {
        PushQueue(empty, QueueFront(noempty));
        PopQueue(noempty);
    }
    int ret = QueueFront(noempty);
    PopQueue(noempty);

    return ret;
}

//返回栈顶元素。
int myStackTop(MyStack* obj) {
    assert(obj);
    //法一:
        // //判断哪一个队列为空
        // Queue* empty = &obj->q1;
        // Queue* noempty = &obj->q2;
        // if(!EmptyQueue(&obj->q1))
        // {
        //     empty = &obj->q2;
        //     noempty = &obj->q1;
        // }

        // //将不为空的n-1个移到为空中
        // while(noempty->size >1){
        //     PushQueue(empty,QueueFront(noempty));
        //     PopQueue(noempty);
        // }
        // int ret = QueueFront(noempty);
        // PushQueue(empty,ret);
        // PopQueue(noempty);

        // return ret;
        
    //法二:
    if (!EmptyQueue(&obj->q1))
    {
        return QueueBack(&obj->q1);
    }
    else
    {
        return QueueBack(&obj->q2);
    }
}

bool myStackEmpty(MyStack* obj) {
    assert(obj);
    return EmptyQueue(&obj->q1) && EmptyQueue(&obj->q2);
}

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

🔎代码详情:

typedef int QDataType;

typedef struct QueueNode
{
    QDataType data;
    struct QueueNode* next;
}QNode;

typedef struct Queue
{
    QNode* head;
    QNode* tail;
    int size;
}Queue;

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

//初始化
void InitQueue(Queue* pq)
{
    assert(pq);

    pq->head = NULL;
    pq->tail = NULL;
    pq->size = 0;
}

//销毁
void DestroyQueue(Queue* pq)
{
    assert(pq);
    QNode* cur = pq->head;
    while (cur)
    {
        QNode* del = cur;
        cur = cur->next;
        free(del);
        del = NULL;
    }
    pq->head = pq->tail = NULL;
    pq->size = 0;
}

//判断是否为空
bool EmptyQueue(Queue* pq)
{
    assert(pq);

    return pq->head == NULL && pq->tail == NULL;
}

//入队
void PushQueue(Queue* pq, QDataType x)
{
    assert(pq);
    QNode* newNode = (QNode*)malloc(sizeof(QNode));
    if (newNode == NULL)
    {
        perror("malloc fail");
        exit(-1);
    }
    else
    {
        newNode->data = x;
        newNode->next = NULL;
    }

    if (pq->tail == NULL)
    {
        pq->head = pq->tail = newNode;
    }
    else
    {
        pq->tail->next = newNode;
        pq->tail = newNode;
    }
    pq->size++;
}

//出队
void PopQueue(Queue* pq)
{
    assert(pq);
    assert(!EmptyQueue(pq));

    if (pq->head != pq->tail)
    {
        QNode* del = pq->head;
        pq->head = pq->head->next;
        free(del);
    }
    else
    {
        free(pq->head);
        pq->head = pq->tail = NULL;
    }

    pq->size--;
}

//取头
QDataType QueueFront(Queue* pq)
{
    assert(pq);
    assert(!EmptyQueue(pq));

    return pq->head->data;
}

//取尾
QDataType QueueBack(Queue* pq)
{
    assert(pq);
    assert(!EmptyQueue(pq));

    return pq->tail->data;
}

//队列的长度
int SizeQueue(Queue* pq)
{
    assert(pq);
    return pq->size;
}

MyStack* myStackCreate() {
    MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
    if (obj == NULL)
    {
        perror("malloc fail");
        exit(-1);
    }
    InitQueue(&obj->q1);
    InitQueue(&obj->q2);

    return obj;
}

//将元素 x 压入栈顶
void myStackPush(MyStack* obj, int x) {
    assert(obj);
    //法一:
    Queue* empty = &obj->q1;
    Queue* noempty = &obj->q2;
    if (!EmptyQueue(&obj->q1)) {
        empty = &obj->q2;
        noempty = &obj->q1;
    }

    PushQueue(noempty, x);

    //法二:
        // if(!EmptyQueue(&obj->q1))
        // {
        //    PushQueue(&obj->q1,x);
        // }
        // else
        // {
        //    PushQueue(&obj->q2,x);
        // }
}

//移除并返回栈顶元素。
int myStackPop(MyStack* obj) {
    assert(obj);

    Queue* empty = &obj->q1;
    Queue* noempty = &obj->q2;
    if (!EmptyQueue(&obj->q1)) {
        empty = &obj->q2;
        noempty = &obj->q1;
    }

    //将不为空的n-1个移到为空中
    while (noempty->size > 1) {
        PushQueue(empty, QueueFront(noempty));
        PopQueue(noempty);
    }
    int ret = QueueFront(noempty);
    PopQueue(noempty);

    return ret;
}

//返回栈顶元素。
int myStackTop(MyStack* obj) {
    assert(obj);
    //法一:
        // //判断哪一个队列为空
        // Queue* empty = &obj->q1;
        // Queue* noempty = &obj->q2;
        // if(!EmptyQueue(&obj->q1))
        // {
        //     empty = &obj->q2;
        //     noempty = &obj->q1;
        // }

        // //将不为空的n-1个移到为空中
        // while(noempty->size >1){
        //     PushQueue(empty,QueueFront(noempty));
        //     PopQueue(noempty);
        // }
        // int ret = QueueFront(noempty);
        // PushQueue(empty,ret);
        // PopQueue(noempty);

        // return ret;
        
    //法二:
    if (!EmptyQueue(&obj->q1))
    {
        return QueueBack(&obj->q1);
    }
    else
    {
        return QueueBack(&obj->q2);
    }
}

bool myStackEmpty(MyStack* obj) {
    assert(obj);
    return EmptyQueue(&obj->q1) && EmptyQueue(&obj->q2);
}

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

感谢家人的阅读,若有不准确的地方 欢迎在评论区指正!

家人们,点个请添加图片描述再走呗~

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

D. Star.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值