【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);
}

 

posted @ 2020-09-26 08:41  温暖了寂寞  阅读(117)  评论(0编辑  收藏  举报