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

数据结构(10)栈和队列算法题

一、用队列实现栈

1、题目描述

https://leetcode.cn/problems/implement-stack-using-queues

 2、算法分析

入栈:往不为空的队列中插入数据。

出栈:把不为空的队列中前size-1个数据挪到另一个队列中,再将最后一个数据出队。

取栈顶元素:取不为空队列中队尾结点的数据。

3、参考代码

typedef int QDataType;
//队列结点的结构
typedef struct QueueNode
{QDataType data;struct QueueNode* next;
}QueueNode;
//队列的结构
typedef struct Queue
{QueueNode* phead;QueueNode* ptail;int size; //队列中有效数据个数
}Queue;
//初始化
void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}
//销毁队列
void QueueDestroy(Queue* pq)
{assert(pq);QueueNode* pcur = pq->phead;while (pcur){QueueNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}
//入队——队尾
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));if (newnode == NULL){perror("malloc fail!");exit(1);}newnode->data = x;newnode->next = NULL;//队列为空if (pq->phead == NULL){pq->phead = pq->ptail = newnode;}else{//队列非空pq->ptail->next = newnode;pq->ptail = pq->ptail->next;}pq->size++;
}
//队列判空
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == NULL;
}
//出队——队头
void QueuePop(Queue* pq)
{assert(!QueueEmpty(pq));//只有一个结点,phead和ptail都要置为空if (pq->phead == pq->ptail){free(pq->phead);pq->phead = pq->ptail = NULL;}else{QueueNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}
//取队头数据
QDataType QueueFront(Queue* pq)
{assert(!QueueEmpty(pq));return pq->phead->data;
}
//取队尾数据
QDataType QueueBack(Queue* pq)
{assert(!QueueEmpty(pq));return pq->ptail->data;
}
//队列有效元素个数
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}/////////////////////////以上是队列结构和方法的实现/////////////////////
typedef struct 
{Queue q1;Queue q2;
} MyStack;MyStack* myStackCreate() 
{MyStack* pst = (MyStack*)malloc(sizeof(MyStack));QueueInit(&pst->q1);QueueInit(&pst->q2);return pst;
}void myStackPush(MyStack* obj, int x) 
{//往不为空的队列中插入数据if(!QueueEmpty(&obj->q1)){QueuePush(&obj->q1, x);}else{QueuePush(&obj->q2, x);}
}int myStackPop(MyStack* obj) 
{//将不为空的队列中前size-1个数据挪到另一个队列中//再将最后一个数据出队列Queue* emp = &obj->q1;Queue* nonEmp = &obj->q2;if(QueueEmpty(&obj->q2)){nonEmp = &obj->q1;emp = &obj->q2;}while(QueueSize(nonEmp) > 1){int front = QueueFront(nonEmp);QueuePush(emp, front);QueuePop(nonEmp);}int top = QueueFront(nonEmp);QueuePop(nonEmp);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);obj = NULL;
}/*** 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);
*/

二、用栈实现队列

1、题目描述

https://leetcode.cn/problems/implement-queue-using-stacks

2、算法分析 

入队:往pushST中插入数据。

出队:如果popST不为空,直接出数据;否则将popST中的数据导入到popST中再出数据。

取队头元素:逻辑同出队操作,但是这里只取数据,不删除数据。

3、参考代码

//定义栈的结构
typedef int STDataType;
typedef struct Stack
{STDataType* arr;int top;       //指向栈顶的位置int capacity;  //栈的容量
}ST;
//初始化
void StackInit(ST* ps)
{ps->arr = NULL;ps->top = ps->capacity = 0;
}
//入栈——栈顶
void StackPush(ST* ps, STDataType x)
{assert(ps);if (ps->top == ps->capacity){//增容int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));if (tmp == NULL){perror("realloc fail!");exit(1);}ps->arr = tmp;ps->capacity = newCapacity;}ps->arr[ps->top++] = x;
}
//栈是否为空
bool StackEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}
//出栈——栈顶
void StackPop(ST* ps)
{assert(!StackEmpty(ps));ps->top--;
}
//取栈顶元素
STDataType StackTop(ST* ps)
{assert(!StackEmpty(ps));return ps->arr[ps->top - 1];
}
//获取栈中有效元素个数
int StackSize(ST* ps)
{return ps->top;
}
//销毁
void StackDestroy(ST* ps)
{if (ps->arr)free(ps->arr);ps->arr = NULL;ps->capacity = ps->top = 0;
}
/////////////////////以上是栈的结构和实现///////////////////////typedef struct 
{ST pushST;ST popST;
} MyQueue;MyQueue* myQueueCreate() 
{MyQueue* pq = (MyQueue*)malloc(sizeof(MyQueue));StackInit(&pq->pushST);StackInit(&pq->popST);return pq;
}void myQueuePush(MyQueue* obj, int x) 
{//往pushST中插入数据StackPush(&obj->pushST, x);
}
// 检查popST是否为空
// 1)不为空,直接出popST的栈顶
// 2)为空,pushST中的数据导入到popST中,再出popST栈顶
int myQueuePop(MyQueue* obj) 
{if(StackEmpty(&obj->popST)){//导数据while(!StackEmpty(&obj->pushST)){int data = StackTop(&obj->pushST);StackPush(&obj->popST, data);StackPop(&obj->pushST);}}int top = StackTop(&obj->popST);StackPop(&obj->popST);return top;
}int myQueuePeek(MyQueue* obj) 
{if(StackEmpty(&obj->popST)){//导数据while(!StackEmpty(&obj->pushST)){int data = StackTop(&obj->pushST);StackPush(&obj->popST, data);StackPop(&obj->pushST);}}int top = StackTop(&obj->popST);return top;
}bool myQueueEmpty(MyQueue* obj) 
{return StackEmpty(&obj->pushST) && StackEmpty(&obj->popST);
}void myQueueFree(MyQueue* obj) 
{StackDestroy(&obj->pushST);StackDestroy(&obj->popST);free(obj);obj = NULL;
}/*** 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);
*/

http://www.dtcms.com/a/309180.html

相关文章:

  • 25电赛e题杂乱环境稳定识别矩形框(附源码)
  • 浏览器环境segmentit实现中文分词
  • 精通分类:解析Scikit-learn中的KNN、朴素贝叶斯与决策树(含随机森林)
  • LLM Prompt与开源模型资源(2)提示工程关键技术
  • 工程化(二):为什么你的下一个项目应该使用Monorepo?(pnpm / Lerna实战)
  • 位运算-面试题01.01.判定字符是否唯一-力扣(LeetCode)
  • 【unity小技巧】封装unity适合2D3D进行鼠标射线检测,获取鼠标位置信息检测工具类
  • 8.1每日一题
  • (线段树)SP2916 GSS5 / nfls #2899 查询最大子段和 题解
  • STL进阶典题整理 2025.7.30-2025.8.1
  • 关于继承的一些知识(C++)
  • react-native在mac的m2芯片下,pod install安装glog的时候报错
  • bmcweb工作流程
  • 【科研绘图系列】R语言绘制环状分组显著性柱状堆积图
  • Spring AI 系列之三十 - Spring AI Alibaba-其它模型
  • CSS font-weight:500不生效
  • Git 命令使用指南:从入门到进阶
  • 动态规划(数位统计dp 状态压缩dp 树形dp 记忆化搜索) from y总
  • 【C语言】字符函数与字符串函数详解
  • http请求访问响应慢问题解决的基本思路
  • 基于python大数据的招聘数据可视化及推荐系统
  • natapp的报错Tunnel StatusReconnecting...
  • STM32芯片简述
  • 使用GPU和NPU视频生成的优劣对比
  • 人工智能与金融:金融服务的重塑
  • Linux9 root密码修改
  • armbian 启用nginx并设置访问密码
  • CTF实战:用Sqlmap破解表单输入型SQL注入题(输入账号密码/usernamepassword)
  • SpringBoot AI应用实战:从图像识别到预测分析
  • 【通用视觉框架】基于OpenCvSharp+WPF+YOLO开发的仿VisionMaster的通用视觉框架软件,全套源码,开箱即用