用队列实现栈

用两个队列实现栈的思想图形化

借助队列的结构和方法

复制代码
typedef int QDataType;
//队列结点的结构
typedef struct QueueNode {
	QDataType data;
	struct QueueNode* next;
}QueueNode;
//队列的结构
typedef struct Queue {
	QueueNode* phead;  //指向队头
	QueueNode* ptail;  //指向队尾
}Queue;
//队列的初始化
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
}
//销毁队列
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;
}

//入队----队尾
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 = newnode;
	}
}
//队列判空
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->phead == NULL;
}
//出队
void QueuePop(Queue* pq)
{
	assert(pq && !QueueEmpty(pq));
	QueueNode* del = pq->phead;
	if (pq->phead == pq->ptail)
	{
		free(del);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		pq->phead = pq->phead->next;
		free(del);
	}
}

//取队头元素
QDataType QueueFront(Queue* pq)
{
	assert(pq && !QueueEmpty(pq));
	return pq->phead->data;
}
//取队尾元素
QDataType QueueBack(Queue* pq)
{
	assert(pq && !QueueEmpty(pq));
	return pq->ptail->data;
}
//队列中有效元素个数
int QueueSize(Queue* pq)
{
	int size = 0;
	QueueNode* pcur = pq->phead;
	while (pcur)
	{
		++size;
		pcur = pcur->next;
	}
	return 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) {
	Queue* emp = &obj->q1;
	Queue* noemp = &obj->q2;
	if (!QueueEmpty(&obj->q1))
	{
		noemp = &obj->q1;
		emp = &obj->q2;
	}
	while (QueueSize(noemp) > 1)
	{
		QueuePush(emp, QueueFront(noemp));
		QueuePop(noemp);
	}
	int top = QueueFront(noemp);
	QueuePop(noemp);
	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;
}
相关推荐
地平线开发者3 小时前
【模型轻量化专题】衡量模型轻量性的指标
算法
学习星球6 小时前
OFDM技术精讲:正交性推导、循环前缀原理与80行Python链路仿真(附实测数据)
算法·面试·前端框架
alphaTao12 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
Interview Aid11213 小时前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
顶点多余1 天前
那些在算法中适合巩固的知识点---1
java·前端·算法
AI情绪识别开源1 天前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
罗西的思考1 天前
【Agentic RL / 强化学习框架】Molt 设计解读
人工智能·算法·机器学习
hahaha60161 天前
HLS高层次综合设计技巧--C++类和模板
图像处理·人工智能·算法·计算机视觉
多弗朗皮卡丘1 天前
算法详解4:买卖股票的最佳时机系列(上)
算法
维克兜率天1 天前
【维克】动量指标家族:RSI、ROC、CCI、Momentum全面解析
python·算法