【C++】每日一练(用队列实现栈)
本篇博客给大家带来的是用C++语言来解答用队列实现栈!
🐟🐟文章专栏:每日一练
🚀🚀若有问题评论区下讨论,我会及时回答
❤❤欢迎大家点赞、收藏、分享!
今日思想:不服输的少年啊,请你再努力一下!
题目描述:
思路:创建两个队列来实现栈,栈的特点是先进后出,队列的特点是先进先出,那么假如一个队列存储1234,那么先出来的也是1234,这时我们把前3个数据(123)放到另外一个队列中去,这时候4就可以先出来,然后再把存储(123)数据的队列前2个数据(12)放到另外一个队列中去,那么3就可以出来,以此反复就可以实现栈。
注意:不懂栈和队列的可以先看看这两篇博客,或许对你有帮助。【C++】数据结构 队列的实现-CSDN博客
【C++】数据结构 栈的实现-CSDN博客
代码实例:
//定义结点的结构
typedef int QDataType;
typedef struct QueueNode
{
QDataType data;
struct QueueNode* next;
}QueueNode;
//定义队列的结构
typedef struct Queue
{
QueueNode* phead;//队头
QueueNode* ptail;//队尾
int size;//有效数据个数
}Queue;
//队列的初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->phead = pq->ptail = NULL;
pq->size=0;
}
//队列的销毁
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;
//队列为空,newnode是队头也是队尾
if (pq->phead == NULL)
{
pq->phead = pq->ptail = newnode;
}
else
{
//队列非空,直接插入队尾
pq->ptail->next = newnode;
pq->ptail = pq->ptail->next;
}
pq->size++;
}
//判断队列是否为空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->phead == 0;
}
//出队——队头
void QueuePop(Queue* pq)
{
assert(!QueueEmpty(pq));
//只有一个结点的情况
if (pq->phead == pq->ptail)
{
free(pq->phead);
pq->phead = pq->ptail = NULL;
}
else
{
QueueNode* next = pq->phead->next;
free(pq->phead);
pq->phead = next;
}
pq->size--;
}
//取队头数据
QDataType QueueFront(Queue* pq)
{
assert(!QueueEmpty(pq));
return pq->phead->data;
}
//取队尾数据
QDataType QueueBack(Queue* pq)
{
assert(!QueueEmpty(pq));
return pq->ptail->data;
}
//队列有效元素个数
int QueueSize(Queue* pq)
{
return pq->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) {
//找不为空的队列,将不为空队列中前size-1个数据导入空队列中
Queue* emp=&obj->q1;
Queue* noneEmp=&obj->q2;
if(QueueEmpty(&obj->q2))
{
emp=&obj->q2;
noneEmp=&obj->q1;
}
while(QueueSize(noneEmp)>1)
{
//把noneEmp中的队头数据导入到空队列中
QueuePush(emp,QueueFront(noneEmp));
QueuePop(noneEmp);
}
int top=QueueFront(noneEmp);
QueuePop(noneEmp);
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;
}
/**
* 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);
*/