【leetcode】用队列实现栈

大家好,我是苏貝,本篇博客带大家刷题,如果你觉得我写的还不错的话,可以给我一个赞👍吗,感谢❤️


点击查看题目



思路:

在做此题之前,我们先要实现队列,这在上个博客中已经写过,在这就不在赘述,下面是实现队列的代码
点击进入博客:【数据结构】实现队列

c 复制代码
#include<stdio.h>
#include<assert.h>
#include<stdbool.h>


typedef int QDataType;

typedef struct QueueNode
{
	struct Queue* next;
	QDataType val;
}QNode;

typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

//初始化
void QueueInit(Queue* pq)
{
	assert(pq);

	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}


//销毁
void QueueDestroy(Queue* pq)
{
	assert(pq);

	while (pq->phead)
	{
		QNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	pq->ptail = NULL;
	pq->size = 0;
}


//队尾插入
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

	//创建一个新节点
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->val = x;
	newnode->next = NULL;

	//插入
	if (pq->phead == NULL)
	{
		pq->ptail = pq->phead = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}


//队头删除
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->phead);

	Queue* next = pq->phead->next;
	free(pq->phead);
	pq->phead = next;

	if (pq->phead == NULL)
	{
		pq->ptail = NULL;
	}
	pq->size--;
}


//显示第一个节点的值
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->phead);

	return pq->phead->val;
}


//显示最后一个节点的值
QDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->phead);

	return pq->ptail->val;
}


//是否为空
bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->phead == NULL;
}


//队列的大小
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

现在我们来写本题的代码

c 复制代码
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* emptyq=&obj->q1;
    Queue* notemptyq=&obj->q2;
    if(!QueueEmpty(&obj->q1))
    {
        emptyq=&obj->q2;
        notemptyq=&obj->q1;
    }

    while(QueueSize(notemptyq)>1)
    {
        QueuePush(emptyq,QueueFront(notemptyq));
        QueuePop(notemptyq);
    }
    int top=QueueFront(notemptyq);
    QueuePop(notemptyq);
    return top;
}

int myStackTop(MyStack* obj) {
    Queue* emptyq=&obj->q1;
    Queue* notemptyq=&obj->q2;
    if(!QueueEmpty(&obj->q1))
    {
        emptyq=&obj->q2;
        notemptyq=&obj->q1;
    }

    return QueueBack(notemptyq);
}

bool myStackEmpty(MyStack* obj) {
    return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
}

void myStackFree(MyStack* obj) {
    QueueDestroy(&obj->q1);
    QueueDestroy(&obj->q2);
    free(obj);
}

好了,那么本篇博客就到此结束了,如果你觉得本篇博客对你有些帮助,可以给个大大的赞👍吗,感谢看到这里,我们下篇博客见❤️

相关推荐
CUMT_DJ7 小时前
matlab计算算法的运行时间
开发语言·算法·matlab
KyollBM10 小时前
每日羊题 (质数筛 + 数学 | 构造 + 位运算)
开发语言·c++·算法
青草地溪水旁11 小时前
linux信号(14)——SIGALRM:从“手机闹钟”看SIGALRM:进程的非阻塞定时神器
linux·信号机制
心灵宝贝11 小时前
libopenssl-1_0_0-devel-1.0.2p RPM 包安装教程(openSUSE/SLES x86_64)
linux·服务器·数据库
BullSmall12 小时前
linux zgrep命令介绍
linux·运维
Univin12 小时前
C++(10.5)
开发语言·c++·算法
Asmalin12 小时前
【代码随想录day 35】 力扣 01背包问题 一维
算法·leetcode·职场和发展
剪一朵云爱着12 小时前
力扣2779. 数组的最大美丽值
算法·leetcode·排序算法
qq_4286396113 小时前
虚幻基础:组件间的联动方式
c++·算法·虚幻
深瞳智检13 小时前
YOLO算法原理详解系列 第002期-YOLOv2 算法原理详解
人工智能·算法·yolo·目标检测·计算机视觉·目标跟踪