自学内容网 自学内容网

LeetCode //C - 232. Implement Queue using Stacks

232. Implement Queue using Stacks

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).

Implement the MyQueue class:

  • void push(int x) Pushes element x to the back of the queue.
  • int pop() Removes the element from the front of the queue and returns it.
  • int peek() Returns the element at the front of the queue.
  • boolean empty() Returns true if the queue is empty, false otherwise.

Notes:

  • You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.
  • Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack’s standard operations.
     
Example 1:

Input:
[“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”]
[[], [1], [2], [], [], []]
Output:
[null, null, null, 1, 1, false]
Explanation:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

Constraints:
  • 1 <= x <= 9
  • At most 100 calls will be made to push, pop, peek, and empty.
  • All the calls to pop and peek are valid.

From: LeetCode
Link: 232. Implement Queue using Stacks


Solution:

Ideas:
  • Stack operations: These are standard stack operations (push, pop, peek, empty).
  • Queue operations: push always pushes onto stack1. For pop and peek, if stack2 is empty, elements from stack1 are moved to stack2 to maintain FIFO order.
  • Memory management: The myQueueFree function frees the memory allocated for the queue and its stacks.
Code:
#define MAX_SIZE 100

typedef struct {
    int data[MAX_SIZE];
    int top;
} Stack;

typedef struct {
    Stack *stack1;
    Stack *stack2;
} MyQueue;

Stack* createStack() {
    Stack *stack = (Stack*)malloc(sizeof(Stack));
    stack->top = -1;
    return stack;
}

MyQueue* myQueueCreate() {
    MyQueue *queue = (MyQueue*)malloc(sizeof(MyQueue));
    queue->stack1 = createStack();
    queue->stack2 = createStack();
    return queue;
}

void stackPush(Stack *stack, int x) {
    if (stack->top < MAX_SIZE - 1) {
        stack->data[++(stack->top)] = x;
    }
}

int stackPop(Stack *stack) {
    if (stack->top >= 0) {
        return stack->data[(stack->top)--];
    }
    return -1; // Stack is empty
}

int stackPeek(Stack *stack) {
    if (stack->top >= 0) {
        return stack->data[stack->top];
    }
    return -1; // Stack is empty
}

bool stackEmpty(Stack *stack) {
    return stack->top == -1;
}

void myQueuePush(MyQueue* obj, int x) {
    stackPush(obj->stack1, x);
}

int myQueuePop(MyQueue* obj) {
    if (stackEmpty(obj->stack2)) {
        while (!stackEmpty(obj->stack1)) {
            stackPush(obj->stack2, stackPop(obj->stack1));
        }
    }
    return stackPop(obj->stack2);
}

int myQueuePeek(MyQueue* obj) {
    if (stackEmpty(obj->stack2)) {
        while (!stackEmpty(obj->stack1)) {
            stackPush(obj->stack2, stackPop(obj->stack1));
        }
    }
    return stackPeek(obj->stack2);
}

bool myQueueEmpty(MyQueue* obj) {
    return stackEmpty(obj->stack1) && stackEmpty(obj->stack2);
}

void myQueueFree(MyQueue* obj) {
    free(obj->stack1);
    free(obj->stack2);
    free(obj);
}

/**
 * Your MyQueue struct will be instantiated and called as such:
 * MyQueue* obj = myQueueCreate();
 * myQueuePush(obj, x);
 
 * int param_2 = myQueuePop(obj);
 
 * int param_3 = myQueuePeek(obj);
 
 * bool param_4 = myQueueEmpty(obj);
 
 * myQueueFree(obj);
*/

原文地址:https://blog.csdn.net/navicheung/article/details/140511088

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!