当前位置:网站首页>队列与堆的相互实现(纯c实现)
队列与堆的相互实现(纯c实现)
2022-07-23 05:44:00 【潜水少年请求出战】
链接: 用队列实现栈
解题思路:
此题可以用两个队列去实现一个栈,每次始终保持一个队列为空,
入栈操作相当于给非空队列进行入队操作
出栈操作相当于非空队列的队尾元素出队,此时需要把非空队列除最后一个元素之外的其余元素入队到空队列,然后出队最后一个队尾元素
代码:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct
{
QNode* head;
QNode* tail;
}Queue;
//初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}
//销毁
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
//放数据
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
//开辟空间
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
printf("malloc fail\n");
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;
}
}
//判断空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
//删除
void QueuePop(Queue* 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;
}
}
//取头
QDataType QueueFront(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->head->data;
}
//取尾
QDataType QueueBack(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
return pq->tail->data;
}
//长度
int QueueSize(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
int count = 0;
while (cur)
{
++count;
cur = cur->next;
}
return count;
}
typedef struct
{
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate()
{
MyStack* obj=(MyStack*)malloc(sizeof(MyStack));
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* empyQ=&obj->q1;
Queue* nonEmptyQ=&obj->q2;
if(!QueueEmpty(&obj->q1))
{
empyQ=&obj->q2;
nonEmptyQ=&obj->q1;
}
//移到另一个数列,删除返回栈顶元素
while(QueueSize(nonEmptyQ)>1)
{
QueuePush(empyQ, QueueFront(nonEmptyQ));
QueuePop(nonEmptyQ);
}
int top= QueueFront(nonEmptyQ);
QueuePop(nonEmptyQ);
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);
}
/** * 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); */
链接: 用栈实现队列
解题思路:
此题可以用两个栈实现,一个栈进行入队操作,另一个栈进行出队操作
出队操作: 当出队的栈不为空是,直接进行出栈操作,如果为空,需要把入队的栈元素全部导入到出队的栈,然后再进行出栈操作
代码:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDataType;
typedef struct STack
{
STDataType* data;
int top;
int capacity;
}ST;
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->top = ps->capacity = 0;
ps->data = NULL;
}
//插入
void StackPush(ST* ps, STDataType x)
{
assert(ps);
//判断是否扩容
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity ? (ps->capacity) * 2 : 3;
STDataType* newdata = (STDataType*)realloc(ps->data, sizeof(STDataType) * newcapacity);
if (newdata == NULL)
{
printf("newdata::file");
exit(-1);
}
ps->data = newdata;
ps->capacity = newcapacity;
}
ps->data[ps->top] = x;
ps->top += 1;
}
//判断空
bool StackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//删除
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top -= 1;
}
//取出
STDataType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->data[ps->top - 1];
}
//销毁
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->data);
ps->data = NULL;
ps->capacity=ps->top = 0;
}
//长度
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
typedef struct
{
ST pushst;
ST popst;
} MyQueue;
MyQueue* myQueueCreate()
{
MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));
StackInit(&obj->pushst);
StackInit(&obj->popst);
return obj;
}
void myQueuePush(MyQueue* obj, int x)
{
StackPush(&obj->pushst, x);
}
int myQueuePop(MyQueue* obj)
{
if(StackEmpty(&obj->popst))
{
//如果pop栈为空,把push栈倒pop
while(!StackEmpty(&obj->pushst))
{
StackPush(&obj->popst, StackTop(&obj->pushst));
StackPop(&obj->pushst);
}
}
int front=StackTop(&obj->popst);
StackPop(&obj->popst);
return front;
}
int myQueuePeek(MyQueue* obj)
{
if(StackEmpty(&obj->popst))
{
//如果pop栈为空,把push栈倒pop
while(!StackEmpty(&obj->pushst))
{
StackPush(&obj->popst, StackTop(&obj->pushst));
StackPop(&obj->pushst);
}
}
return StackTop(&obj->popst);
}
bool myQueueEmpty(MyQueue* obj)
{
return StackEmpty(&obj->popst) && StackEmpty(&obj->pushst);
}
void myQueueFree(MyQueue* obj)
{
StackDestroy(&obj->pushst);
StackDestroy(&obj->popst);
free(obj);
}
/** * 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); */
边栏推荐
- 单片机学习笔记4--GPIO(基于百问网STM32F103系列教程)
- 谈谈转动惯量
- 单片机学习笔记6--中断系统(基于百问网STM32F103系列教程)
- [AUTOSAR com 1. introduction to communication protocol stack]
- [CAN总线的物理层 ]1.CAN/CANFD采样的点的内容分享
- 博客搭建三:评论系统选择
- [AUTOSAR cantp 1. learn the network layer protocol of UDS diagnosis]
- Baidu Shen Shuo: focus on the scene, deeply cultivate the industry, and bring practical results to enterprise Digitalization
- 钢结构基本原理全面详细总结
- 利用or-tools来求解带容量限制的路径规划问题(CVRP)
猜你喜欢
Blog Building III: comment system selection

二叉树的实现-c

A comprehensive and detailed summary of the basic principles of steel structure
![[AUTOSAR CP general 1. how to read AUTOSAR official documents]](/img/3a/8521278a4bd21bb269603a52f07b0e.png)
[AUTOSAR CP general 1. how to read AUTOSAR official documents]

Hardware knowledge 1 -- Schematic diagram and interface type (based on Baiwen hardware operation Daquan video tutorial)

Analyze the pre integration of vio with less rigorous but logical mathematical theory

单片机学习笔记5--STM32时钟系统(基于百问网STM32F103系列教程)

高分子物理考研概念及要点、考点总结
Blog building I: Framework selection

单片机学习笔记9--常见的通信方式(基于百问网STM32F103系列教程)
随机推荐
Tips for using textviewdidchange of uitextview
Tencent cloud client command line tool tccli main process analysis
Using pycaret: low code, automated machine learning framework to solve classification problems
【AUTOSAR COM 3.信号的收发流程TX/RX】
【AUTOSAR COM 1.通信协议栈介绍】
嵌入式从入门到精通(入土)——超详细知识点分享1
NLP natural language processing - Introduction to machine learning and natural language processing (2)
钢结构基本原理题库
单片机学习笔记9--串口通信(基于百问网STM32F103系列教程)
Examen des principes fondamentaux de la structure en acier
AWK 程序设计语言
Summary of problems encountered during app audit
ARM架构与编程7--异常与中断(基于百问网ARM架构与编程教程视频)
常见排序--归并排序(递归和非递归)+计数排序
Find the saddle point of the matrix and its corresponding subscript.
输入三角形边长,求面积
[AUTOSAR com 3. signal sending and receiving process tx/rx]
Analysis of 100 questions and answers in Higher Algebra
ARM架构与编程3--按键控制LED(基于百问网ARM架构与编程教程视频)
Using or tools to solve path planning problem (VRP)