leetcode-用栈实现队列(c语言)

使用两个栈,通过栈的特性模拟队列的入队和出队操作,实现LeetCode上的用栈实现队列问题。
摘要由CSDN通过智能技术生成

用栈实现队列

https://leetcode-cn.com/problems/implement-queue-using-stacks/

思路:通过两个栈来实现

在这里插入图片描述

//实现栈建议数组
typedef int STDdataType;
typedef struct Stack{
    STDdataType *_a;
    int _top;
    int _capacity;
}Stack;
//初始化
void StackInit(Stack*pst){
    assert(pst);
    pst->_a=malloc(sizeof(STDdataType)*5);
    pst->_top=0;
    pst->_capacity=5;
}
//压栈
void StackPush(Stack*pst,STDdataType data){
    assert(pst);
    if(pst->_top==pst->_capacity){
        pst->_capacity*=2;
        pst->_a=(STDdataType*)realloc(pst->_a,sizeof(STDdataType)*pst->_capacity);
        if(pst->_a==NULL){
            printf("内存不足!");
            exit(-1);
        }
    }
    pst->_a[pst->_top]=data;
    pst->_top++;
}
//弹出
void StackPop(Stack*pst){
    assert(pst);
    assert(pst->_top>0);
    pst->_top-=1;
}
//获取栈顶
STDdataType StackTop(Stack*pst){
    assert(pst);
    assert(pst->_top>0);
    return (pst->_a[pst->_top-1]);
}

//判空
int StackEmpty(Stack*pst){
    assert(pst);
    return pst->_top==0?1:0;
}
//销毁
void StackDestroy(Stack*pst){
    assert(pst);
    free(pst->_a);
    pst->_a=NULL;
    pst->_top=pst->_capacity=0;
}
//获取数据个数
int StackSize(Stack *pst){
    assert(pst);
    return pst->_top;
}

typedef struct {
     Stack _pushST;
     Stack _popST;
} MyQueue;

/** Initialize your data structure here. */

MyQueue* myQueueCreate() {
    MyQueue*q=(MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&q->_pushST);
    StackInit(&q->_popST);
    return q;    //  对应于下面的obj
}

/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
    StackPush(&obj->_pushST,x);
}

/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
    int front=myQueuePeek(obj);
    StackPop(&obj->_popST);
    return front;
}

/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
    //注意获队头元素,如果pushST里的元素都入到popST里,但是popST只出了部分,当再次想popST里出元素的时候,还是要先出popST里的
    //所以要先判断popST里有没有出干净
    if(!StackEmpty(&obj->_popST)){
        return StackTop(&obj->_popST);
    }
    else{
        while(!StackEmpty(&obj->_pushST)){
        StackPush(&obj->_popST,StackTop(&obj->_pushST)); 
        StackPop(&obj->_pushST);
        }
        return StackTop(&obj->_popST);
    }
    
    
}

/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->_pushST)&&StackEmpty(&obj->_popST);
}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->_pushST);
    StackDestroy(&obj->_popST);
    free(obj);
}

/**
 * Your MyQueue struct will be instantiated and called as such:
 * MyQueue* obj = myQueueCreate();
 * myQueuePush(obj, x);
 
 * int param_2 = myQueuePop(obj);
 
 * int param_3 = myQueuePeek(obj);
 
 * bool param_4 = myQueueEmpty(obj);
 
 * myQueueFree(obj);
*/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值