🎬 鸽芷咕:个人主页 🔥个人专栏:《Linux深造日志》《C++干货基地》
⛺️生活的理想,就是为了理想的生活!
🌈hello! 各位铁铁们大家好啊,栈和队列我们都学过了那么试试用队列实现栈你会嘛?。 ⛳️本篇文章就来给大家来篇如何用队列来实现栈的全部解析让你彻底拿捏队列。 📚本期文章收录在《数据结构&算法》,大家有兴趣可以看看呐! ⛺️ 欢迎铁汁们 ✔️ 点赞 👍 收藏 ⭐留言 📝!
我们先来总结一下队列的特点 先进先出
,队列的特点 是 后进先出
:
我们是不是可以具体这样想,队列既然是先进先出,栈是后进先出,的意识就是我们需要每次获取的栈区数据是队列的最后一个:
这思路就很简单如果使用俩个指针来管理队列,那么插入就非常简单了,只需要考虑俩种情况:
大家看如何我们使用俩个指针来管理队列,来实现栈的话那么来删除就非常简单了为什么这样说呢?
最后一个元素
用来删除就好了好了以上就是队列实现栈的核心思想了,只要核心思想掌握了,那么其他就是小菜了。下面我们就来快速实现一下吧:
这里是队列的实现代码,由于C语言库里面并没有队列这个数据结构我们就只能手动实现了:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QueDataType;
typedef struct QueueType {
QueDataType data;
struct QueueType* next;
}QueueNode;
typedef struct Queue {
struct QueueType* head;
struct QueueType* tail;
int size;
}Que;
//初始化队列
void QueueInit(Que* ps);
//销毁队列
void QueueDestroy(Que* ps);
//插入数据
void QueuePush(Que* ps, QueDataType x);
//删除数据
void QueuePop(Que* ps);
//获取头数据
QueDataType QueueFront(Que* ps);
//获取尾数据
QueDataType QueueBack(Que* ps);
//获取队列长度
int QueueSize(Que* ps);
//判断是否为空
bool QueueEmpty(Que* ps);
//初始化队列
void QueueInit(Que* ps)
{
assert(ps);
ps->head = NULL;
ps->tail = NULL;
ps->size = 0;
}
//销毁队列
void QueueDestroy(Que* ps)
{
assert(ps);
QueueNode* cur = ps->head;
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
ps->head = ps->tail = NULL;
ps->size = 0;
}
//插入数据
void QueuePush(Que* ps, QueDataType x)
{
assert(ps);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (ps->head == NULL)
{
ps->head = ps->tail = newnode;
}
else
{
ps->tail->next = newnode;
ps->tail = newnode;
}
ps->size++;
}
//删除数据
void QueuePop(Que* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QueueNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
pq->size--;
}
//获取头数据
QueDataType QueueFront(Que* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
return ps->head->data;
}
//获取尾数据
QueDataType QueueBack(Que* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
return ps->tail->data;
}
//获取队列长度
int QueueSize(Que* ps)
{
assert(ps);
return ps->size;
}
//判断是否为空
bool QueueEmpty(Que* ps)
{
assert(ps);
return ps->head == NULL;
}
好了思路我们理清楚了接下来就是看看具体队列实现栈的代码到底如何实现:
malloc
申请空间就好了📚 代码演示:
typedef struct {
Que q1;
Que q2;
} MyStack;
MyStack* myStackCreate() {
MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
QueueInit(&obj->q1);
QueueInit(&obj->q2);
return obj;
}
插入的话我们思想已经都知道,下面就是考验你的代码能力了:
📚 代码演示:
void myStackPush(MyStack* obj, int x) {
assert(obj);
if(!QueueEmpty(&obj->q1))
{
QueuePush(&obj->q1,x);
}
else
{
QueuePush(&obj->q2,x);
}
}
删除的思想很简单,每次搬运一下队列就好了:
📚 代码演示:
int myStackPop(MyStack* obj) {
assert(obj);
Que* empty = &obj->q1;
Que* noempty = &obj->q2;
if(!QueueEmpty(empty))
{
empty = &obj->q2;
noempty = &obj->q1;
}
while(QueueSize(noempty) > 1)
{
QueuePush(empty, QueueFront(noempty));
QueuePop(noempty);
}
int top = QueueFront(noempty);
QueuePop(noempty);
return top;
}
这个就简单了只要栈中管理俩个队列的的都为空那当然都没有数据了:
QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2)
📚 代码演示:
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}
既然我们使用了俩个队列来实现栈,并且只有一个队列存放数据。
📚 代码演示:
int myStackTop(MyStack* obj) {
if(!QueueEmpty(&obj->q1))
{
return QueueBack(&obj->q1);
}
else
{
return QueueBack(&obj->q2);
}
}
老样子了,先把栈区里面的 malloc 里面的空间释放了,然后再释放栈就好了。
📚 代码演示:
void myStackFree(MyStack* obj) {
QueueDestroy(&obj->q1);
QueueDestroy(&obj->q2);
free(obj);
}
📚 代码演示:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int QueDataType;
typedef struct QueueType {
QueDataType data;
struct QueueType* next;
}QueueNode;
typedef struct Queue {
struct QueueType* head;
struct QueueType* tail;
int size;
}Que;
//初始化队列
void QueueInit(Que* ps);
//销毁队列
void QueueDestroy(Que* ps);
//插入数据
void QueuePush(Que* ps, QueDataType x);
//删除数据
void QueuePop(Que* ps);
//获取头数据
QueDataType QueueFront(Que* ps);
//获取尾数据
QueDataType QueueBack(Que* ps);
//获取队列长度
int QueueSize(Que* ps);
//判断是否为空
bool QueueEmpty(Que* ps);
//初始化队列
void QueueInit(Que* ps)
{
assert(ps);
ps->head = NULL;
ps->tail = NULL;
ps->size = 0;
}
//销毁队列
void QueueDestroy(Que* ps)
{
assert(ps);
QueueNode* cur = ps->head;
while (cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
ps->head = ps->tail = NULL;
ps->size = 0;
}
//插入数据
void QueuePush(Que* ps, QueDataType x)
{
assert(ps);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (ps->head == NULL)
{
ps->head = ps->tail = newnode;
}
else
{
ps->tail->next = newnode;
ps->tail = newnode;
}
ps->size++;
}
//删除数据
void QueuePop(Que* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QueueNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
pq->size--;
}
//获取头数据
QueDataType QueueFront(Que* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
return ps->head->data;
}
//获取尾数据
QueDataType QueueBack(Que* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
return ps->tail->data;
}
//获取队列长度
int QueueSize(Que* ps)
{
assert(ps);
return ps->size;
}
//判断是否为空
bool QueueEmpty(Que* ps)
{
assert(ps);
return ps->head == NULL;
}
typedef struct {
Que q1;
Que q2;
} MyStack;
MyStack* myStackCreate() {
MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
QueueInit(&obj->q1);
QueueInit(&obj->q2);
return obj;
}
void myStackPush(MyStack* obj, int x) {
assert(obj);
if(!QueueEmpty(&obj->q1))
{
QueuePush(&obj->q1,x);
}
else
{
QueuePush(&obj->q2,x);
}
}
int myStackPop(MyStack* obj) {
assert(obj);
Que* empty = &obj->q1;
Que* noempty = &obj->q2;
if(!QueueEmpty(empty))
{
empty = &obj->q2;
noempty = &obj->q1;
}
while(QueueSize(noempty) > 1)
{
QueuePush(empty, QueueFront(noempty));
QueuePop(noempty);
}
int top = QueueFront(noempty);
QueuePop(noempty);
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);
*/
☁️ 好了以上就是队列实现栈的全部细节与过程来,大家不要忘记练习哦!
看到这里了还不给博主扣个:
⛳️ 点赞
☀️收藏
⭐️ 关注
!
💛 💙 💜 ❤️ 💚💓 💗 💕 💞 💘 💖
拜托拜托这个真的很重要!
你们的点赞就是博主更新最大的动力!
有问题可以评论或者私信呢秒回哦。