题目
使用栈实现队列的下列操作:
- 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);
*/
题解
思路: 用两个栈模拟队列,一个push栈,一个pop栈,push栈栈顶模拟队列队尾,pop栈栈顶模拟队列队首,当需要push时,将数据转移到push栈,pop和top时,将数据转移到pop栈。
#include<stdlib.h>
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);
}