LeetCode 232.

    技术2022-07-10  121

    #define max 100 typedef struct stack{ int top; int data[max]; }Stack; typedef struct { Stack a1; Stack a2; } MyQueue; /** Initialize your data structure here. */ MyQueue* myQueueCreate() { MyQueue *queue=(MyQueue*)malloc(sizeof(MyQueue)); queue->a1.top=-1; queue->a2.top=-1; return queue; } /** Push element x to the back of queue. */ void myQueuePush(MyQueue* obj, int x) { if(obj->a1.top<max){ while(obj->a1.top!=-1){ obj->a2.data[++(obj->a2.top)]=obj->a1.data[(obj->a1.top)--]; } obj->a1.data[++(obj->a1.top)]=x; while(obj->a2.top!=-1){ obj->a1.data[++(obj->a1.top)]=obj->a2.data[(obj->a2.top)--]; } } } /** Removes the element from in front of queue and returns that element. */ int myQueuePop(MyQueue* obj) { if(obj->a1.top!=-1){ return obj->a1.data[obj->a1.top--]; } return 0; } /** Get the front element. */ int myQueuePeek(MyQueue* obj) { return obj->a1.data[obj->a1.top]; } /** Returns whether the queue is empty. */ bool myQueueEmpty(MyQueue* obj) { if(obj->a1.top==-1) return true; return false; } void myQueueFree(MyQueue* obj) { free(obj); }
    Processed: 0.010, SQL: 9