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);
*/

相关推荐

  1. TTL 232难兄难弟对比

    2024-07-21 11:42:04       27 阅读
  2. 232.用栈实现队列

    2024-07-21 11:42:04       34 阅读
  3. 【LC刷题】DAY03:242 349 202 1

    2024-07-21 11:42:04       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 11:42:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 11:42:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 11:42:04       45 阅读
  4. Python语言-面向对象

    2024-07-21 11:42:04       55 阅读

热门阅读

  1. redis笔记

    2024-07-21 11:42:04       14 阅读
  2. Mysql、Oracle 审计日志的开启

    2024-07-21 11:42:04       20 阅读
  3. 服务互联:在Eureka中实现服务的依赖注入

    2024-07-21 11:42:04       12 阅读
  4. 十四、正则表达式

    2024-07-21 11:42:04       18 阅读
  5. 【笔记-软考】架构演化

    2024-07-21 11:42:04       16 阅读
  6. 每天一个数据分析题(四百三十九)- 用户画像

    2024-07-21 11:42:04       17 阅读
  7. SpinalHDL之总线

    2024-07-21 11:42:04       15 阅读