Two stack to queue规则:(重点就是理解规则)
如何入队:入队只向s1里面入队(默认s1是可以无限存放的)
如何出队:看s2里面有没有值,有的话直接出;如果s2为空怎么办?将s1里面的值,全部颠倒存放到s2中,这时s2就不空了,则直接出即可。
两个栈实现一个队列:这个很简单里边的很多函数都是可以直接调用我们上一节写过的顺序栈中的函数。(栈(顺序栈) - 基础数据_hi332516_1的博客-CSDN博客)
先写头文件two_stack_to_queue.h
#pragma once
#include"Stack.h"
typedef int ELEM_TYPE;
//用两个栈实现的队列的结构体
typedef struct Two_stack_queue
{
struct Stack s1;//是之前自己实现过的顺序栈
struct Stack s2;
}Two_stack_queue, * PTwo_stack_queue;
//初始化
void my_Init_2stack_queue(struct Two_stack_queue* tsq);
//入队
bool my_Push(struct Two_stack_queue* tsq, ELEM_TYPE val);
//出队(还需要一个输出参数,帮助将出队的值带出来)
bool my_Pop(struct Two_stack_queue* tsq, ELEM_TYPE* rtval);
//获取队头元素值(还需要一个输出参数,帮助将出队的值带出来)
bool my_Top(struct Two_stack_queue* tsq, ELEM_TYPE* rtval);
//判空
bool my_IsEmpty(struct Two_stack_queue* tsq);
//有效元素个数
int my_Get_length(struct Two_stack_queue* tsq);
//打印
void my_Show(struct Two_stack_queue* tsq);
//清空
void my_Clear(struct Two_stack_queue* tsq);
//销毁
void my_Destroy(struct Two_stack_queue* tsq);
再写two_stack_to_queue.cpp文件(里边有很多内容都是直接调用的顺序栈的内容)为什么每一个前边都加了my,这是为了和顺序栈的函数声明作以区分。
#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#include<stdlib.h>
#include"Stack.h"
#include"two_stack_to_queue.h"
//初始化
void my_Init_2stack_queue(struct Two_stack_queue* tsq)
{
Init_stack(&tsq->s1);//直接调用s1和s2自身的初始化函数即可
Init_stack(&tsq->s2);
}
//入队
bool my_Push(struct Two_stack_queue* tsq, ELEM_TYPE val)
{
return Push(&tsq->s1, val);//直接调用s1和s2自身的入栈函数即可
}
//出队(还需要一个输出参数,帮助将出队的值带出来)
//从s2出,如果s2不空,直接出
//如果s2为空,先将s1的数据全部颠倒到s2中
bool my_Pop(struct Two_stack_queue* tsq, ELEM_TYPE* rtval)
{
//assert
if (my_IsEmpty(tsq))//证明模拟实现的队列里有数据,在哪不确定
{
return false;
}
if (IsEmpty(&tsq->s2))//确定了数据在s1中
{
ELEM_TYPE tmp;
while (!IsEmpty(&tsq->s1))//只要s1里边还有值就取出来放在s2中
{
Pop(&tsq->s1, &tmp);//直接调用s1和s2自身的入栈函数即可
Push(&tsq->s2, tmp);
}
return Pop(&tsq->s2, rtval);
}
else//如果s2有值,直接出
{
return Pop(&tsq->s2, rtval);
}
}
//获取队头元素值(还需要一个输出参数,帮助将出队的值带出来)
bool my_Top(struct Two_stack_queue* tsq, ELEM_TYPE* rtval)
{
//assert
if (my_IsEmpty(tsq))//证明模拟实现的队列里有数据,在哪不确定
{
return false;
}
if (IsEmpty(&tsq->s2))//确定了数据在s1中
{
ELEM_TYPE tmp;
while (!IsEmpty(&tsq->s1))//只要s1里边还有值就取出来放在s2中
{
Pop(&tsq->s1, &tmp);//直接调用s1和s2自身的入栈函数即可
Push(&tsq->s2, tmp);
}
return Top(&tsq->s2, rtval);
}
else//如果s2有值,直接出
{
return Top(&tsq->s2, rtval);
}
}
//判空
bool my_IsEmpty(struct Two_stack_queue* tsq)
{
if (IsEmpty(&tsq->s1) && IsEmpty(&tsq->s2))
{
return false;
}
}
//有效元素个数
int my_Get_length(struct Two_stack_queue* tsq)
{
return Get_length(&tsq->s1) + Get_length(&tsq->s2);
}
//打印
void my_Show(struct Two_stack_queue* tsq)
{
//先s2,再s1
//s2从上到下打印,s1从下到上打印
int len = Get_length(&tsq->s2);
for (int i = len - 1; i >= 0 ;i-- )
{
printf("%d ", tsq->s2.base[i]);
}
Show(&tsq->s1);
}
//清空
void my_Clear(struct Two_stack_queue* tsq)
{
Clear(&tsq->s1);
Clear(&tsq->s2);
}
//销毁
void my_Destroy(struct Two_stack_queue* tsq)
{
Destroy(&tsq->s1);
Destroy(&tsq->s2);
}
最后在主函数中测试代码的正确性
#include<stdio.h>
#include<malloc.h>
#include <stdlib.h>
#include<assert.h>
#include<vld.h>
#include"two_stack_to_queue.h"
//两个栈模拟实现一个队列的测试用例
int main()
{
struct Two_stack_queue head;
my_Init_2stack_queue(&head);
for (int i = 1; i <= 3; i++)
{
my_Push(&head, i);
}
my_Show(&head);
ELEM_TYPE flag;
bool tag = my_Pop(&head, &flag);
if (tag)
{
printf("Pop = %d\n", flag);
}
my_Show(&head);
for (int i = 4; i <= 6; i++)
{
my_Push(&head, i);
}
my_Show(&head);
my_Pop(&head, &flag);
printf("Pop = %d\n", flag);
my_Pop(&head, &flag);
printf("Pop = %d\n", flag);
my_Pop(&head, &flag);
printf("Pop = %d\n", flag);
my_Pop(&head, &flag);
printf("Pop = %d\n", flag);
printf("length = %d\n", my_Get_length(&head));
my_Top(&head, &flag);
printf("Top = %d\n", flag);
printf("length = %d\n", my_Get_length(&head));
my_Destroy(&head);
return 0;
}
测试结果