当前位置: 首页 > news >正文

用两个队列实现栈

这里写目录标题

  • 用两个队列实现栈
    • 题目描述
    • 思路:
    • 结构逻辑图如下
    • 完整解析代码

用两个队列实现栈

leetcode

题目描述

在这里插入图片描述
在这里插入图片描述


思路:

准备两个队列,第一个队列依次出队到只剩一个数据时停止,将已出队的数据依次入队到第二个队列,将第一个队列仅剩的一个数据出队即实现了栈的出栈。入栈时哪个队列不为空则在哪个队列入队。
在这里插入图片描述

结构逻辑图如下


在这里插入图片描述


完整解析代码

typedef int QDataType;
typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Queue;void QueueInit(Queue* pq)
{assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}
void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->phead;while (cur) {QNode* next = cur->next;free(cur);cur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL) {perror("mallloc fail\n");return;}newnode->data = x;newnode->next = NULL;if (pq->ptail == NULL) {assert(pq->phead == NULL);pq->phead = pq->ptail = newnode;}else {pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0;
}
void QueuePop(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));if (pq->phead->next == NULL) {free(pq->phead);pq->phead = pq->ptail = NULL;}else {QNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}
QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->phead->data;
}
QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->ptail->data;
}
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}//------以下为OJ提供-------typedef struct {Queue q1;Queue q2;
} MyStack;MyStack* myStackCreate() {MyStack* obj = (MyStack*)malloc(sizeof(MyStack));if (obj == NULL) {perror("malloc fail");return NULL;}QueueInit(&obj->q1);QueueInit(&obj->q2);return obj;
}void myStackPush(MyStack* obj, int x) {if (!QueueEmpty(&obj->q1)) {QueuePush(&obj->q1, x);}else {QueuePush(&obj->q2, x);}
}int myStackPop(MyStack* obj) {Queue* pEmptyQ = &obj->q1;Queue* pNonEmptyQ = &obj->q2;if (!QueueEmpty(&obj->q1)) {pEmptyQ = &obj->q2;pNonEmptyQ = &obj->q1;}while (QueueSize(pNonEmptyQ) > 1) {QueuePush(pEmptyQ, QueueFront(pNonEmptyQ));QueuePop(pNonEmptyQ);}int top = QueueFront(pNonEmptyQ);QueuePop(pNonEmptyQ);return top;
}int myStackTop(MyStack* obj) {if (!QueueEmpty(&obj->q1)) {return QueueBack(&obj->q1);}else {return QueueBack(&obj->q2);}
}bool myStackEmpty(MyStack* obj) {return QueueEmpty(&obj->q1) &&QueueEmpty(&obj->q2);
}void myStackFree(MyStack* obj) {QueueDestroy(&obj->q1);QueueDestroy(&obj->q2);free(obj);
}
http://www.lryc.cn/news/294481.html

相关文章:

  • 【数据分享】1929-2023年全球站点的逐年降雪深度数据(Shp\Excel\免费获取)
  • Windows11安装运行Linux(Ubuntu)
  • 钉钉群机器人-发送群消息
  • OceanBase 4.2.2 GA 发布,全新特性快速预览!
  • IP代理類型詳解 | 基於網路協議、匿名性、IP來源
  • uniapp中使用EelementPlus
  • Swift Vapor 教程(查询数据、插入数据)
  • QT自用,勿点
  • 计组学习笔记2024/2/5
  • Redis(三)(实战篇)
  • MacOS系统电脑远程桌面控制windows系统电脑【内网穿透】
  • Verilog实现2进制码与BCD码的互相转换
  • Codeforces Round 901 (Div. 2) C. Jellyfish and Green Apple (思维)
  • K8s 集群可观测性-数据分流最佳实践
  • muduo库的模拟实现——工具部分
  • SpringBoot接入微信公众号【服务号】
  • 2023 英特尔On技术创新大会直播 |探索视觉AI的无限可能
  • 安卓视图基础
  • 电路设计(10)——超温报警电路的proteus仿真
  • gerrit(1) | gerrit 简介
  • 计算机视觉实战项目3(图像分类+目标检测+目标跟踪+姿态识别+车道线识别+车牌识别+无人机检测+A*路径规划+单目测距与测速+行人车辆计数等)
  • redis(5)
  • Postgresql体系结构
  • 【Rust】——rust前言与安装rust
  • 基于SpringBoot的家电销售展示网页的设计与实现
  • 【Qt】—— 项⽬⽂件解析
  • 【Linux】静态库和动态库
  • LeetCode 0292.Nim 游戏:脑筋急转弯
  • ctfshow-web1~10-WP
  • 集合问题(并查集)