目录
一、问题描述
二、前置知识
关于栈的详细讲解请阅读这篇文章
【数据结构与算法】使用数组实现栈:原理、步骤与应用-CSDN博客
关于队列的详细讲解请阅读这篇文章
【数据结构与算法】使用单链表实现队列:原理、步骤与应用-CSDN博客
三、解题思路
图解:
(为方便理解对队列的结构进行了简化)
取队首元素类似于出队列,只是取出的元素不出栈
四、C语言实现代码
🍃栈实现代码:
// 支持动态增长的栈
typedef int STDataType;//对数据类型重命名,方便后期修改类型
typedef struct Stack
{
STDataType* a;
int top; // 栈顶
int capacity; // 容量
}Stack;//定义结构同时重命名
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
//判断是否需要扩容
if (ps->top == ps->capacity)
{
int newcapa = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapa);
if (tmp == NULL)
{
perror("realloc\n");
exit(1);
}
ps->a = tmp;
ps->capacity = newcapa;
}
//确定空间足够之后再插入数据
ps->a[ps->top] = data;
ps->top++;
}
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(ps->top);
ps->top--;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(ps->top);
return ps->a[ps->top-1];
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
🍃栈实现队列代码:
typedef struct {
Stack pushst;//入数据的栈
Stack popst;//出数据的栈
} MyQueue;
MyQueue* myQueueCreate() //初始化
{
MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&(obj->pushst));
StackInit(&(obj->popst));
return obj;
}
void myQueuePush(MyQueue* obj, int x) //模拟入队列
{
assert(obj);
StackPush(&(obj->pushst), x);//入队列直接插入到pushst栈
}
int myQueuePop(MyQueue* obj) //模拟出队列
{
assert(obj);
assert(!StackEmpty(&(obj->popst)) || !StackEmpty(&(obj->pushst)));//首先两个栈不能都为空
if (StackEmpty(&(obj->popst)))//如果popst栈为空,把数据倒过去
{
while (obj->pushst.top)
{
StackPush(&(obj->popst), StackTop(&(obj->pushst)));
StackPop (&(obj->pushst));
}
}
int top = StackTop(&(obj->popst));
StackPop(&(obj->popst));//再从popst栈出数据
return top;
}
int myQueuePeek(MyQueue* obj) //模拟取队列首元素
{
assert(obj);
assert(!StackEmpty(&(obj->popst)) || !StackEmpty(&(obj->pushst)));
if (StackEmpty(&(obj->popst)))//如果popst栈为空,把数据倒过去
{
while (obj->pushst.top)
{
StackPush(&(obj->popst), StackTop(&(obj->pushst)));
StackPop(&(obj->pushst));
}
}
return StackTop(&(obj->popst));
}
bool myQueueEmpty(MyQueue* obj) //模拟队列判空
{
return StackEmpty(&(obj->pushst)) && StackEmpty(&(obj->popst));
}
void myQueueFree(MyQueue* obj) //销毁
{
StackDestroy(&(obj->pushst));
StackDestroy(&(obj->popst));
free(obj);
obj = NULL;
}
🍃测试文件及结果:
void test2()
{
MyQueue* Queue = myQueueCreate();
if (myQueueEmpty(Queue))
printf("队列空\n");
else
printf("队列非空\n");
myQueuePush(Queue, 1);
myQueuePush(Queue, 2);
myQueuePush(Queue, 3);
myQueuePush(Queue, 4);
printf("队首元素 %d\n", myQueuePeek(Queue));
if (myQueueEmpty(Queue))
printf("队列空\n");
else
printf("队列非空\n");
while (!myQueueEmpty(Queue))
{
printf("%d ", myQueuePop(Queue));
}
printf("\n");
if (myQueueEmpty(Queue))
printf("队列空\n");
else
printf("队列非空\n");
myQueueFree(Queue);
}