当前位置: 首页 > 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.lryc.cn/news/606960.html

相关文章:

  • 4-verilog简单状态机
  • Linux 硬盘分区管理
  • FEVER数据集:事实验证任务的大规模基准与评估框架
  • spring boot 启动报错---java: 无法访问org.springframework.boot.SpringApplication 错误的类文件
  • Pycaita二次开发基础代码解析:几何体重命名与参数提取技术
  • 【Java面试题】缓存穿透
  • 梯度下降的基本原理
  • Oracle EBS ERP开发 — 抛出异常EXCEPTION书写规范
  • Vue3 setup、ref和reactive函数
  • ReAct模式深度解析:构建具备推理能力的AI智能体架构
  • 【Linux】System V - 责任链模式与消息队列
  • 机密计算与AI融合:安全与智能的共生架构
  • 动态爱心树
  • Linux(CentOS 7.9) 卸载、安装MySql 5.7详细步骤教程,包括密码设置、字符集设置等
  • 鸿蒙系统PC安装指南
  • 无人机避让路径规划模块运行方式
  • 图论-最短路Floyd算法
  • SpringBoot与Rust实战指南
  • VS Code中配置使用slint(Rust)的一个小例子
  • Java学习第九十六部分——Eureka
  • 基于CNN卷积神经网络图像识别28个识别合集-视频介绍下自取
  • k8s之DevicePlugin
  • 运维端口管理闭环:从暴露面测绘到自动化封禁!
  • 自动驾驶的未来:多模态传感器钻机
  • 【通用视觉框架】基于OpenCvSharp+WPF+YOLO开发的仿VisionMaster的通用视觉框架软件,全套源码,开箱即用
  • CTF实战:用Sqlmap破解表单输入型SQL注入题(输入账号密码/usernamepassword)
  • 音频获取长度
  • armbian 启用nginx并设置访问密码
  • gpu instancer crowd 插件大规模渲染
  • 《操作系统真象还原》 第五章 保护模式进阶