LeetCode_225. 用队列实现栈

题目

LeetCode_225. 用队列实现栈
使用队列实现栈的下列操作:

  • push(x) – 元素 x 入栈
  • pop() – 移除栈顶元素
  • top() – 获取栈顶元素
  • empty() – 返回栈是否为空

注意:

  • 你只能使用队列的基本操作– 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
  • 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
  • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
typedef struct {

} MyStack;

/** Initialize your data structure here. */
MyStack* myStackCreate(int maxSize) {

}

/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {

}

/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {

}

/** Get the top element. */
int myStackTop(MyStack* obj) {

}

/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {

}

void myStackFree(MyStack* obj) {

}

/**
 * Your MyStack struct will be instantiated and called as such:
 * struct MyStack* obj = myStackCreate(maxSize);
 * myStackPush(obj, x);
 * int param_2 = myStackPop(obj);
 * int param_3 = myStackTop(obj);
 * bool param_4 = myStackEmpty(obj);
 * myStackFree(obj);
 */

题解

思路: 队列和栈基础练习,用循环队列模拟栈,队尾为栈顶,入栈时直接入队,出栈时先将队首出队并入队count - 1次,将栈顶移动到队首,然后将队首出队

    typedef struct {
        int *data;
        int front, tail;
        int size;
        int count;
    } MyQueue;

    MyQueue *myQueueInit(int size) {
        MyQueue *q = (MyQueue *)malloc(sizeof(MyQueue));
        q->data = (int *)malloc(sizeof(int) * size);
        q->front = 0;
        q->tail = -1;
        q->size = size;
        q->count = 0;
        return q;
    }

    bool myQueueEmpty(MyQueue *q) {
        return q->count == 0;
    }

    void myQueuePush(MyQueue *q, int value) {
        q->data[++q->tail] = value;
        q->tail %= q->size;
        q->count++;
    }

    int myQueuePop(MyQueue *q) {
        q->count--;
        int num = q->data[q->front++];
        q->front %= q->size; 
        return num;
    } 

    int myQueueFront(MyQueue * q) {
        return q->data[q->front];
    }

    void myQueueFree(MyQueue *q) {
        free(q->data);
        free(q);
    }

    typedef struct {
        MyQueue *q;
        int count;
    } MyStack;

    /** Initialize your data structure here. */
    MyStack* myStackCreate(int maxSize) {
        MyStack *s = (MyStack *)malloc(sizeof(MyStack));
        s->q = myQueueInit(maxSize);
        s->count = 0;
        return s; 
    }

    /** Push element x onto stack. */
    void myStackPush(MyStack* obj, int x) {
        myQueuePush(obj->q, x);
        obj->count++;
    }

    /** Removes the element on top of the stack and returns that element. */
    int myStackPop(MyStack* obj) {
        --obj->count;
        int num = obj->count;
        while (num--) {
            myQueuePush(obj->q, myQueuePop(obj->q));
        }
        return myQueuePop(obj->q);
    }

    /** Get the top element. */
    int myStackTop(MyStack* obj) {
        int num = obj->count - 1;
        while (num--) {
            myQueuePush(obj->q, myQueuePop(obj->q));
        }
        int temp = myQueueFront(obj->q);
        myQueuePush(obj->q, myQueuePop(obj->q));
        return temp;
    }

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

    void myStackFree(MyStack* obj) {
        myQueueFree(obj->q);
        free(obj);
    }

    /**
     * Your MyStack struct will be instantiated and called as such:
     * struct MyStack* obj = myStackCreate(maxSize);
     * myStackPush(obj, x);
     * int param_2 = myStackPop(obj);
     * int param_3 = myStackTop(obj);
     * bool param_4 = myStackEmpty(obj);
     * myStackFree(obj);
     */

LeetCode习题汇总

猜你喜欢

转载自blog.csdn.net/skange/article/details/81146431