【leetcode】用队列实现栈

 

typedef struct {
    int top;
    int q[10000];
} MyStack;

/** Initialize your data structure here. */

MyStack* myStackCreate() {
    MyStack* obj = (MyStack*)calloc(1,sizeof(MyStack));
    obj->top=-1;
    return obj;
}

/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
    obj->q[++obj->top]=x;
}

/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
    return obj->q[obj->top--];
}

/** Get the top element. */
int myStackTop(MyStack* obj) {
    return obj->q[obj->top];
}

/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
    return obj->top == -1;
}

void myStackFree(MyStack* obj) {
    free(obj);
}

 

posted @ 2020-09-25 19:19  温暖了寂寞  阅读(98)  评论(0编辑  收藏  举报