leetcode232. 用栈实现队列

leetcode232. 用栈实现队列
在这里插入图片描述
C语言实现

#define MaxSize 100
typedef int  ElemType;
typedef struct{
    ElemType data[MaxSize];
    int top;//栈顶指针
}SqStack;

SqStack* InitStack(){
    //初始化栈
    SqStack *s=(SqStack*)malloc(sizeof(SqStack));
    s->top=-1;
    return s;
}

bool StackEmpty(SqStack *s){
    //判断栈是否为空
    return s->top==-1;
}

bool StackOverflow(SqStack *s){
    //判断栈是否已满
    return s->top==MaxSize;
}

bool Push(SqStack *s,ElemType x){
    //元素入栈
    if(StackOverflow(s))
        return false;
    s->data[++s->top]=x;
    return true;
}

bool Pop(SqStack *s,ElemType *x){
    //元素出栈
    if(StackEmpty(s))
        return false;
    *x=s->data[s->top--];
    return true;
}

bool GetTop(SqStack *s,ElemType *x){
    //获得栈顶元素
    if(StackEmpty(s))
        return false;
    *x=s->data[s->top];
    return true;
}


typedef struct {
    SqStack *S1,*S2;//两个栈来模拟队列
} MyQueue;

/** Initialize your data structure here. */

MyQueue* myQueueCreate() {
    MyQueue *q=(MyQueue*)malloc(sizeof(MyQueue));
    q->S1=InitStack();
    q->S2=InitStack();
    return q;
}

/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
    if(StackOverflow(obj->S1)&&!StackEmpty(obj->S2))//第一个栈满了,第二个栈不为空
        return ;
    if(StackOverflow(obj->S1)){
        //第一个栈满了,把元素全部放入第二个栈中
        while(!StackEmpty(obj->S1)){
            ElemType temp;//临时变量
            Pop(obj->S1,&temp);
            Push(obj->S2,temp);
        }
    }
    Push(obj->S1,x);
}

/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
    if(StackEmpty(obj->S1)&&StackEmpty(obj->S2)) //队列为空
        return -1;
    if(StackEmpty(obj->S2)){
        //把第一个栈中的元素全部放入第二个栈中
        while(!StackEmpty(obj->S1)){
            ElemType temp;//临时变量
            Pop(obj->S1,&temp);
            Push(obj->S2,temp);
        }
    }
    ElemType x;
    Pop(obj->S2,&x);
    return x;
}

/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
    if(StackEmpty(obj->S1)&&StackEmpty(obj->S2)) //队列为空
        return -1;
    if(StackEmpty(obj->S2)){
        //把第一个栈中的元素全部放入第二个栈中
        while(!StackEmpty(obj->S1)){
            ElemType temp;//临时变量
            Pop(obj->S1,&temp);
            Push(obj->S2,temp);
        }
    }
    ElemType x;
    GetTop(obj->S2,&x);
    return x;
}

/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(obj->S1)&&StackEmpty(obj->S2);//两个栈都为空队列为空
}

void myQueueFree(MyQueue* obj) {
    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);
*/

C++实现

typedef int ElemType;
class MyQueue {
public:
    stack<ElemType> S1,S2;
    /** Initialize your data structure here. */
    MyQueue() {

    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        S1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        if(empty())//队列为空
            return -1;
        if(S2.empty()){
            while(!S1.empty()){
                //把第一个栈中的元素全部放入第二个栈中
                S2.push(S1.top());
                S1.pop();
            }
        }
        ElemType x=S2.top();
        S2.pop();
        return x;
    }
    
    /** Get the front element. */
    int peek() {
        if(empty())//队列为空
            return -1;
        if(S2.empty()){
            while(!S1.empty()){
                //把第一个栈中的元素全部放入第二个栈中
                S2.push(S1.top());
                S1.pop();
            } 
        }
        return S2.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return S1.empty()&&S2.empty();//两个栈都为空队列为空
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值