栈和队列实现的代码在链接中的博客https://blog.csdn.net/Damn_Yang/article/details/83928852
两个栈实现一个队列
思路:
入队时,直接压入stack1中
出队时,判断stack2是否为空,如果stack2为空,则将stack1中的元素倒入stack2中,否则直接弹出stack2中的元素
代码实现(C)
Topic.h
#pragma once
#include "Queue.h"
#include "Stack.h"
// 两个栈实现一个队列
typedef struct QueueByTwoStack
{
Stack s1;
Stack s2;
}QueueByTwoStack;
void QueueByTwoStackInit(QueueByTwoStack*qts);
void QueueByTwoStackDestory(QueueByTwoStack*qts);
void QueueByTwoStackPush(QueueByTwoStack*qts, DataType x);
void QueueByTwoStackPop(QueueByTwoStack*qts);
DataType QueueByTwoStackFront(QueueByTwoStack*qts);
int QueueByTwoStackEmpty(QueueByTwoStack*qts);
int QueueByTwoStackSize(QueueByTwoStack*qts);
void TestQueueByTwoStack();
Topic.c
#include"Topic.h"
void QueueByTwoStackInit(QueueByTwoStack*qts)
{
assert(qts);
StackInit(&qts->s1);
StackInit(&qts->s2);
}
void QueueByTwoStackDestory(QueueByTwoStack*qts)
{
assert(qts);
StackDestory(&qts->s1);
StackDestory(&qts->s2);
}
void QueueByTwoStackPush(QueueByTwoStack*qts, DataType x)
{
StackPush(&qts->s1,x);
}
//如果s2有数据直接出
//如果s2没有数据,则将s1的数据倒过来
void QueueByTwoStackPop(QueueByTwoStack*qts)
{
if (StackEmpty(&qts->s2))
{
while (StackEmpty(&qts->s1))
{
StackPush(&qts->s2, StackTop(&qts->s1));
StackPop(&qts->s1);
}
}
StackPop(&qts->s2);
}
DataType QueueByTwoStackFront(QueueByTwoStack*qts)
{
assert(qts);
if (StackEmpty(&qts->s2))
{
while (StackEmpty(&qts->s1))
{
StackPush(&qts->s2, StackTop(&qts->s1));
StackPop(&qts->s1);
}
}
return StackTop(&qts->s2);
}
int QueueByTwoStackEmpty(QueueByTwoStack*qts)
{
assert(qts);
return StackEmpty(&qts->s1) | StackEmpty(&qts->s2);
}
int QueueByTwoStackSize(QueueByTwoStack*qts)
{
assert(qts);
return StackSize(&qts->s1) + StackSize(&qts->s2);
}
void TestQueueByTwoStack()
{
QueueByTwoStack q;
QueueByTwoStackInit(&q);
QueueByTwoStackPush(&q, 1);
QueueByTwoStackPush(&q, 2);
QueueByTwoStackPush(&q, 3);
QueueByTwoStackPush(&q, 4);
QueueByTwoStackPop(&q);
QueueByTwoStackPop(&q);
QueueByTwoStackPush(&q, 5);
QueueByTwoStackPush(&q, 6);
while (QueueByTwoStackEmpty(&q))
{
printf("%d ", QueueByTwoStackFront(&q));
QueueByTwoStackPop(&q);
}
printf("\n");
}
两个队列实现一个栈
思路:
入栈时,压入到空栈中,q1和q2哪个为空入到哪个里面去(例如q1为空)
出栈时,先将queue1中的元素除最后一个元素外依次出队列,并压入队列queue2中,将留在queue1中的最后一个元素出队列即为 出栈元素,最后还要把queue2中的元素再次压入queue1中
代码实现(C语言)
扫描二维码关注公众号,回复:
4152667 查看本文章
![](/qrcode.jpg)
Topic.h
#include "Stack.h"
#include "Queue.h"
typedef struct StackByTwoQueue
{
Queue q1;
Queue q2;
}StackByTwoQueue;
void StackByTwoQueueInit(StackByTwoQueue* stq);
void StackByTwoQueueDestory(StackByTwoQueue* stq);
void StackByTwoQueuePush(StackByTwoQueue* stq, DataType x);
void StackByTwoQueuePop(StackByTwoQueue* stq);
DataType StackByTwoQueueTop(StackByTwoQueue*stq);
int StackByTwoQueueEmpty(StackByTwoQueue* stq);
int StackByTwoQueueSize(StackByTwoQueue* stq);
Topic.c
#include "Topic.h"
void StackByTwoQueueInit(StackByTwoQueue* stq)
{
assert(stq);
QueueInit(&stq->q1);
QueueInit(&stq->q2);
}
void StackByTwoQueueDestory(StackByTwoQueue* stq)
{
QueueDestory(&stq->q1);
QueueDestory(&stq->q2);
}
void StackByTwoQueuePush(StackByTwoQueue* stq, DataType x)
{
assert(stq);
if (!QueueEmpty(&stq->q1))
{
QueuePush(&stq->q1, x);
}
else
{
QueuePush(&stq->q2, x);
}
}
void StackByTwoQueuePop(StackByTwoQueue* stq)
{
assert(stq);
Queue*empty = &stq->q1, *nonempty = &stq->q2;
if (!QueueEmpty(&stq->q1))
{
empty = &stq->q2;
nonempty = &stq->q1;
}
while (QueueSize(nonempty) > 1)
{
QueuePush(empty, QueueFront(nonempty));
QueuePop(nonempty);
}
QueuePop(nonempty);
}
DataType StackByTwoQueueTop(StackByTwoQueue*stq)
{
assert(stq);
if (QueueEmpty(&stq->q1) != 0)
{
return stq->q1._back->_data;
}
else
{
return stq->q2._back->_data;
}
}
int StackByTwoQueueEmpty(StackByTwoQueue* stq)
{
assert(stq);
return QueueEmpty(&stq->q1) | QueueEmpty(&stq->q2);
}
int StackByTwoQueueSize(StackByTwoQueue* stq)
{
assert(stq);
return QueueSize(&stq->q1) + QueueSize(&stq->q2);
}
TestStackByTwoQueue()
{
StackByTwoQueue s;
StackByTwoQueueInit(&s);
StackByTwoQueuePush(&s, 1);
StackByTwoQueuePush(&s, 2);
StackByTwoQueuePush(&s, 3);
StackByTwoQueuePush(&s, 4);
StackByTwoQueuePush(&s, 5);
while (StackByTwoQueueEmpty(&s))
{
printf("%d ", StackByTwoQueueTop(&s));
StackByTwoQueuePop(&s);
}
}