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

【数据结构】如何用队列实现栈?图文详解(LeetCode)

LeetCode链接:225. 用队列实现栈 - 力扣(LeetCode)


本文默认读者已经掌握栈与队列的基本知识

或者先看我的另一篇博客:【数据结构】栈与队列_字节连结的博客-CSDN博客

做题思路

由于我们使用的是C语言,不能直接使用队列的操作,

所以做这道题得先把我们之前实现的队列复制过来:

//C语言模拟实现队列//链式结构:表示队列
typedef int QDataType;
typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;//队列的结构
typedef struct Queue
{QNode* head;QNode* tail;int size;
}Que;//初始化队列
void QueueInit(Que* pq);
//销毁队列
void QueueDestroy(Que* pq);
//队尾入队列
void QueuePush(Que* pq, QDataType x);
//队头出队列
void QueuePop(Que* pq);
//获取队列头部元素
QDataType QueueFront(Que* pq);
//获取队列队尾元素
QDataType QueueBack(Que* pq);
//检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Que* pq);
//获取队列中有效元素个数
int QueueSize(Que* pq);void QueueInit(Que* pq)
{assert(pq);pq->head = pq->tail = NULL;pq->size = 0;
}void QueueDestroy(Que* pq)
{assert(pq);QNode* cur = pq->head;while (cur){QNode* next = cur->next;free(cur);cur = next;}pq->head = pq->tail = NULL;pq->size = 0;
}void QueuePush(Que* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(-1);}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 QueuePop(Que* pq)
{assert(pq);assert(!QueueEmpty(pq));if (pq->head->next == NULL){free(pq->head);pq->head = pq->tail = NULL;}else{QNode* next = pq->head->next;free(pq->head);pq->head = next;}pq->size--;
}QDataType QueueFront(Que* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->head->data;
}QDataType QueueBack(Que* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}bool QueueEmpty(Que* pq)
{assert(pq);return pq->head == NULL;
}int QueueSize(Que* pq)
{assert(pq);return pq->size;
}

复制完成后进入正题:

答:用两个队列捯数据的方式来实现后入先出的栈

图文解析:

代码:

//用两个队列实现栈
typedef struct
{Que q1;//队列1Que q2;//队列2
} MyStack;//开辟空间并初始化
MyStack* myStackCreate()
{MyStack* pst = (MyStack*)malloc(sizeof(MyStack));QueueInit(&pst->q1);QueueInit(&pst->q2);return pst;
}//将元素x压入栈顶
void myStackPush(MyStack* obj, int x)
{if (!QueueEmpty(&obj->q1)){QueuePush(&obj->q1, x);}else{QueuePush(&obj->q2, x);}
}//移除并返回栈顶元素
int myStackPop(MyStack* obj)
{Que* empty = &obj->q1;Que* nonEmpty = &obj->q2;if (!QueueEmpty(&obj->q1)){nonEmpty = &obj->q1;empty = &obj->q2;}//前size-1个导入空队列while (QueueSize(nonEmpty) > 1){QueuePush(empty, QueueFront(nonEmpty));QueuePop(nonEmpty);}//用局部变量记录栈顶元素,方便返回int top = QueueFront(nonEmpty);QueuePop(nonEmpty);return top;
}//返回栈顶元素
int myStackTop(MyStack* obj)
{if (!QueueEmpty(&obj->q1)){return QueueBack(&obj->q1);}else{return QueueBack(&obj->q2);}
}//如果栈是空的,返回true;否则,返回false
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/131566.html

相关文章:

  • Linux 虚拟机Ubuntu22.04版本通过远程连接连接不上,输入ifconfig只能看到127.0.0.1的解决办法
  • C语言刷题训练DAY.9
  • CTFHub php://input
  • React Native expo项目修改应用程序名称
  • unity 之Transform组件(汇总)
  • 基于Opencv的虚拟拖拽项目
  • 基于单片机DHT11温湿度NRF2401无线通信控制系统
  • AutoSAR配置与实践(基础篇)2.5 RTE对数据一致性的管理
  • ASP.NET WEB API通过SugarSql连接MySQL数据库
  • 08-微信小程序视图层
  • [机器学习]特征工程:特征降维
  • 12. Docker可视化工具
  • css层叠关系
  • 【Unity实战篇 】| 如何在小游戏中快速接入一个新手引导教程
  • Lookup Singularity
  • idea 本地版本控制 local history
  • 【Freertos基础入门】深入浅出freertos互斥量
  • 皮爷咖啡基于亚马逊云科技的数据架构,加速数据治理进程
  • C++ string类详解
  • 深入浅出Pytorch函数——torch.nn.init.ones_
  • 一、docker及mysql基本语法
  • 【计算机网络】13、ARP 包:广播自己的 mac 地址和 ip
  • 通过微软Azure调用GPT的接口API-兼容平替OpenAI官方的注意事项
  • 回归预测 | MATLAB实现BO-SVM贝叶斯优化支持向量机多输入单输出回归预测(多指标,多图)
  • GAN!生成对抗网络GAN全维度介绍与实战
  • 自动驾驶仿真:基于Carsim开发的加速度请求模型
  • .netcore grpc客户端工厂及依赖注入使用
  • C语言入门_Day7 逻辑运算
  • 什么是Eureka?以及Eureka注册服务的搭建
  • Docker安装并配置镜像加速器,镜像、容器的基本操作