【leetcode】用化栈为队

typedef struct {
    int top;
    int bottom;
    int arr[10000];
} MyQueue;

/** Initialize your data structure here. */

MyQueue* myQueueCreate() {
    MyQueue* obj = (MyQueue*)calloc(1,sizeof(MyQueue));
    obj->top=0;
    obj->bottom=-1;
    return obj;
}

/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
    obj->arr[++obj->bottom]=x;
}

/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
    return obj->arr[obj->top++];
}

/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
    return obj->arr[obj->top];
}

/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
    return obj->top > obj->bottom;
}

void myQueueFree(MyQueue* obj) {
    free(obj);
}
原文地址:https://www.cnblogs.com/ganxiang/p/13733718.html