用两个栈实现队列

简介: 用两个栈实现队列

一、栈的基本结构及其接口

//栈的结构定义
typedef int STDataType;
typedef struct Stack{
    STDataType *a;
    int top;
    int capacity;
}ST;
//栈的初始化
void STInit(ST* pst)
{
    pst->a=NULL;
    pst->top=0;
    pst->capacity=0;
}
//栈的扩容
void checkcapacity(ST* pst)
{
    if(pst->top==pst->capacity)
    {
        int newcapacity=pst->capacity==0?4:pst->capacity*4;
        STDataType* tmp=(STDataType*)realloc(pst->a,sizeof(STDataType)*newcapacity);
        if(tmp==NULL)
        {
            perror("realloc fail");
            exit(-1);
        }
        pst->a=tmp;
        pst->capacity=newcapacity;
    }
}
//入栈
void STPush(ST* pst,STDataType x)
{
    assert(pst);
    checkcapacity(pst);
    pst->a[pst->top++]=x;
}
//出栈
void STPop(ST* pst)
{
    assert(pst);
    assert(pst->top);//空栈
    pst->top--;
}
//取栈顶元素
STDataType STTop(ST* pst)
{
    assert(pst);
    assert(pst->top);//空栈
    return pst->a[pst->top-1];
}
//判断栈是否为空
bool STEmpty(ST* pst)
{
    return pst->top==0;
}
//销毁栈
void STDestroy(ST* pst)
{
    assert(pst);
    free(pst->a);
    pst->a=NULL;
    pst->top=0;
    pst->capacity=0;
}

二、我的队列结构定义

//我的队列
typedef struct {
    ST s1;
    ST s2;
} MyQueue;

三、我的队列创建及其初始化

//我的队列的创建及其初始化
MyQueue* myQueueCreate() {
    MyQueue* myqueue=(MyQueue*)malloc(sizeof(MyQueue));
    if(myqueue==NULL)
    {
        perror("malloc fail");
        exit(-1);
    }
    STInit(&myqueue->s1);
    STInit(&myqueue->s2);
    return myqueue;
}

四、我的队列入队

//我的队列入队
void myQueuePush(MyQueue* obj, int x) {
    STPush(&obj->s1,x);
}

五、我的队列出队

//我的队列出队
int myQueuePop(MyQueue* obj) {
    while(obj->s1.top>1)
    {
        STPush(&obj->s2,STTop(&obj->s1));
        STPop(&obj->s1);
    }
    int tmp=STTop(&obj->s1);
    STPop(&obj->s1);
    while(obj->s2.top>0)
    {
        STPush(&obj->s1,STTop(&obj->s2));
        STPop(&obj->s2);
    }
    return tmp;
}

六、我的队列取队头元素

//我的队列取队头元素
int myQueuePeek(MyQueue* obj) {
    while(obj->s1.top>1)
    {
        STPush(&obj->s2,STTop(&obj->s1));
        STPop(&obj->s1);
    }
    int tmp=STTop(&obj->s1);
    while(obj->s2.top>0)
    {
        STPush(&obj->s1,STTop(&obj->s2));
        STPop(&obj->s2);
    }
    return tmp;
}

七、我的队列判空

//我的队列判空
bool myQueueEmpty(MyQueue* obj) {
    return STEmpty(&obj->s1);
}

八、我的队列销毁

//我的队列销毁
void myQueueFree(MyQueue* obj) {
    STDestroy(&obj->s1);
    STDestroy(&obj->s2);
    free(obj);
    obj=NULL;
}


目录
相关文章
|
11小时前
|
存储 NoSQL C语言
数据结构——顺序栈与链式栈的实现-2
数据结构——顺序栈与链式栈的实现
数据结构——顺序栈与链式栈的实现-2
|
11小时前
|
存储 C语言
数据结构——顺序栈与链式栈的实现-1
数据结构——顺序栈与链式栈的实现
数据结构——顺序栈与链式栈的实现-1
|
11小时前
栈的基本应用
栈的基本应用
12 3
|
12小时前
栈与队列理解
栈与队列理解
12 1
|
11小时前
|
存储 算法
数据结构与算法 栈与队列
数据结构与算法 栈与队列
12 0
数据结构与算法 栈与队列
|
11小时前
|
C++
数据结构(顺序队列 循环队列
数据结构(顺序队列 循环队列
9 0
|
11小时前
|
C++
数据结构(共享栈
数据结构(共享栈
7 0
|
11小时前
|
C++
数据结构(顺序栈
数据结构(顺序栈
12 2
|
11小时前
|
容器
【栈与队列】栈与队列的相互转换OJ题
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
10 0
|
11小时前
|
机器学习/深度学习 算法 测试技术
【单调栈】3113. 边界元素是最大值的子数组数目
【单调栈】3113. 边界元素是最大值的子数组数目