当前位置:网站首页>队列与堆的相互实现(纯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); */
边栏推荐
- Blog Building III: comment system selection
- ARM架构与编程1--LED闪烁(基于百问网ARM架构与编程教程视频)
- Object based - two classic classes
- [talent column] can't you use Apache dolphin scheduler? It takes a month to write the most comprehensive introductory teaching [2]
- [physical layer of CAN bus] 1. Content sharing of can/canfd sampling points
- Uni native plug-in development -- Youmeng one click login
- 对字符串函数的使用和理解(2)
- 常见排序--归并排序(递归和非递归)+计数排序
- 常见的排序方法—选择排序
- Installation and use of APP automated testing tool appium
猜你喜欢

《高分子合成工艺》简答题答案

Examen des principes fondamentaux de la structure en acier

硬件知识2--协议类(基于百问网硬件操作大全视频教程)
![[AUTOSAR candrive 1. learn the function and structure of candrive]](/img/f6/662512bddab70e75367d212029fc23.png)
[AUTOSAR candrive 1. learn the function and structure of candrive]

常见排序--归并排序(递归和非递归)+计数排序
![[talent column] can't you use Apache dolphin scheduler? It takes a month to write the most comprehensive introductory teaching [2]](/img/34/abc6ef91d0b735f713f76da5a4b537.png)
[talent column] can't you use Apache dolphin scheduler? It takes a month to write the most comprehensive introductory teaching [2]

Uni native plug-in development -- Youmeng one click login
博客搭建三:评论系统选择

常见的排序—交换排序

【AUTOSAR COM 1.通信协议栈介绍】
随机推荐
C语言中,对柔性数组的理解
ARM架构与编程7--异常与中断(基于百问网ARM架构与编程教程视频)
Analyze the pre integration of vio with less rigorous but logical mathematical theory
二叉树的实现-c
高电压技术基础知识
硬件知识2--协议类(基于百问网硬件操作大全视频教程)
ARM架构与编程6--重定位(基于百问网ARM架构与编程教程视频)
LVGL8.1版本笔记
Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 1
(1)ASIO
ARM架构与编程1--LED闪烁(基于百问网ARM架构与编程教程视频)
Installation and use of APP automated testing tool appium
AWK 程序设计语言
ARM架构与编程3--按键控制LED(基于百问网ARM架构与编程教程视频)
Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 2
博客搭建一:框架选择
K-nucleotide frequencies (KNF) or k-mer frequencies
Data analysis of time series (III): decomposition of classical time series
[learning summary]
Questions and answers of basic principles of steel structure