LeetCode_232.スタックによるキューの実装


タイトル
232.スタックによるキューの実装
スタックを使用してキューを実装するには、次の手順に従います.
  • push(x)-キューの末尾に要素を配置します.
  • pop()–キューの先頭から要素を除去します.
  • peek()–キューヘッダの要素を返します.
  • empty()–キューが空であるかどうかを返します.

  • 例:
    MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek();//は、1 queueを返します.pop();//は、1 queueを返します.empty();//falseを返す
    説明:
  • 標準のスタック操作しか使用できません.つまり、push to top、peek/pop from top、size、is empty操作だけが合法です.
  • あなたが使っている言語はスタックをサポートしていないかもしれません.listまたはdeque(両端キュー)を使用して、標準的なスタック操作であればスタックをシミュレートできます.
  • は、すべての操作が有効であると仮定する(例えば、空のキューがpopまたはpeek操作を呼び出さない).
  • typedef struct {
    
    } MyQueue;
    
    /** Initialize your data structure here. */
    MyQueue* myQueueCreate(int maxSize) {
    
    }
    
    /** Push element x to the back of queue. */
    void myQueuePush(MyQueue* obj, int x) {
    
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int myQueuePop(MyQueue* obj) {
    
    }
    
    /** Get the front element. */
    int myQueuePeek(MyQueue* obj) {
    
    }
    
    /** Returns whether the queue is empty. */
    bool myQueueEmpty(MyQueue* obj) {
    
    }
    
    void myQueueFree(MyQueue* obj) {
    
    }
    
    /**
     * Your MyQueue struct will be instantiated and called as such:
     * struct MyQueue* obj = myQueueCreate(maxSize);
     * myQueuePush(obj, x);
     * int param_2 = myQueuePop(obj);
     * int param_3 = myQueuePeek(obj);
     * bool param_4 = myQueueEmpty(obj);
     * myQueueFree(obj);
     */

    問題解
    構想:2つのスタックでキューをシミュレートし、1つのpushスタック、1つのpopスタック、pushスタックトップでキューキューキューの先頭をシミュレートし、popスタックトップでキューの先頭をシミュレートし、pushが必要な場合、pushスタック、pop、topにデータを転送すると、popスタックにデータを転送します.
    #include
    typedef struct {
        int *data;
        int top, size;
    } MyStack;
    
    MyStack *myStackInit(int maxSize) {
        MyStack *s = (MyStack *)malloc(sizeof(MyStack));
        s->data = (int *)malloc(sizeof(int) * maxSize);
        s->top = -1;
        s->size = maxSize;
        return s;
    }
    
    int myStackEmpty(MyStack *s) {
        return s->top == -1;
    }
    
    int myStackPush(MyStack *s, int value) {
        if (s->top + 1 == s->size) {
            return 0;
        }
        s->data[++s->top] = value;
        return 1;
    }
    
    int myStackPop(MyStack *s) {
        return s->data[s->top--];
    }
    
    int myStackTop(MyStack *s) {
        return s->data[s->top];
    }
    
    
    void myStackClear(MyStack *s) {
        if (myStackEmpty(s)) {
            return ;
        }
        free(s->data);
        free(s);
    }
    
    
    typedef struct {
        MyStack *s_pop, *s_push;
    } MyQueue;
    
    /** Initialize your data structure here. */
    MyQueue* myQueueCreate(int maxSize) {
        MyQueue *q = (MyQueue *)malloc(sizeof(MyQueue));
        q->s_pop = myStackInit(maxSize);
        q->s_push = myStackInit(maxSize);
        return q;
    }
    
    /** Move s1 element to s2*/
    void reserve(MyStack *s1, MyStack *s2) {
        while (!myStackEmpty(s1)) {
            myStackPush(s2, s1->data[s1->top--]);
        }
    }
    
    /** Push element x to the back of queue. */
    void myQueuePush(MyQueue* obj, int x) {
        if (!myStackEmpty(obj->s_pop)) {
            reserve(obj->s_pop, obj->s_push);
        }
        myStackPush(obj->s_push, x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int myQueuePop(MyQueue* obj) {
        if (!myStackEmpty(obj->s_push)) {
            reserve(obj->s_push, obj->s_pop); 
        }
        return myStackPop(obj->s_pop);
    }
    
    /** Get the front element. */
    int myQueuePeek(MyQueue* obj) {
        if (!myStackEmpty(obj->s_push)) {
            reserve(obj->s_push, obj->s_pop); 
        }
        return myStackTop(obj->s_pop);
    }
    
    /** Returns whether the queue is empty. */
    bool myQueueEmpty(MyQueue* obj) {
        return myStackEmpty(obj->s_pop) && myStackEmpty(obj->s_push);
    }
    
    void myQueueFree(MyQueue* obj) {
        if (myQueueEmpty(obj)) return ;
        myStackClear(obj->s_pop);
        myStackClear(obj->s_push);
        free(obj);
    }

    LeetCode練習まとめ