先写头文件
#pragma once
#include "Stack.h"
//用两个栈模拟实现的队列的结构体
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(PTwo_stack_queue tsq, ELEM_TYPE val);
//出队(还需要一个输出参数,帮助我将出队的值带出来)
bool my_Pop(PTwo_stack_queue tsq, ELEM_TYPE* rtval);
//获取队头元素值(还需要一个输出参数,帮助我将出队的值带出来)
bool my_Top(PTwo_stack_queue tsq, ELEM_TYPE* rtval);
//判空
bool my_IsEmpty(PTwo_stack_queue tsq);
//有效元素个数
int my_Get_length(PTwo_stack_queue tsq);
//打印
void my_Show(PTwo_stack_queue tsq);
//清空
void my_Clear(PTwo_stack_queue tsq);
//销毁
void my_Destroy (PTwo_stack_queue tsq);
再写cpp文件
#include "two_stack_to_queue.h"
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
//初始化
void my_Init_2stack_queue(struct Two_stack_queue* tsq)
{
Init_Stack(&tsq->s1);//直接调用s1和s2自身的初始化函数即可
Init_Stack(&tsq->s2);
}
//入队 //只向s1里面入
bool my_Push(PTwo_stack_queue tsq, ELEM_TYPE val)
{
return Push(&tsq->s1, val);
}
//出队(还需要一个输出参数,帮助我将出队的值带出来)
//从s2出,如果s2不空,直接出,
//如果s2空,先将s1的数据全部颠倒存放到s2里,这时s2就不空了
bool my_Pop(PTwo_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);
Push(&tsq->s2, tmp);
}
return Pop(&tsq->s2, rtval);
}
else//如果s2不空 直接出值
{
return Pop(&tsq->s2, rtval);
}
}
//获取队头元素值(还需要一个输出参数,帮助我将出队的值带出来)
bool my_Top(PTwo_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);
Push(&tsq->s2, tmp);
}
return Top(&tsq->s2, rtval);
}
else//如果s2不空 直接出值
{
return Top(&tsq->s2, rtval);
}
}
//判空
bool my_IsEmpty(PTwo_stack_queue tsq)
{
if(IsEmpty(&tsq->s1) && IsEmpty(&tsq->s2))
{
return true;
}
return false;
}
//有效元素个数
int my_Get_length(PTwo_stack_queue tsq)
{
return Get_length(&tsq->s1) + Get_length(&tsq->s2);
}
//打印
void my_Show(PTwo_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(PTwo_stack_queue tsq)
{
Clear(&tsq->s1);
Clear(&tsq->s2);
}
//销毁
void my_Destroy (PTwo_stack_queue tsq)
{
Destroy(&tsq->s1);
Destroy(&tsq->s2);
}
最后在主函数中运行
#include <stdio.h>
#include "assert.h"
#include <stdlib.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;
}