使用栈实现队列的下列操作:
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 操作)。
https://leetcode-cn.com/problems/implement-queue-using-stacks/description/
用两个栈实现队列:利用两个栈来实现
一个栈专门负责入数据;
一个栈专门负责出数据;
如果要出数据时,出栈为空就要把入栈里面的所有数据都转移至出栈
出数据
在入数据
栈的实现
typedef int STDataType;
typedef struct Stack
{
STDataType *_a;//指针
int _top;//栈顶的下一个元素
int _capacity;//容量
}Stack;
void StackInit(Stack *ps, int n)
{
assert(ps);
ps->_a = (STDataType *)malloc(sizeof(STDataType)*n);
//开辟空间是否成功
assert(ps->_a);
ps->_capacity = n;
ps->_top = 0;
}
void StackDestory(Stack *ps)
{
assert(ps);
if (ps->_top)
{
free(ps->_a);
ps->_a = NULL;
ps->_top = 0;
ps->_capacity = 0;
}
}
void StackPush(Stack *ps, STDataType x)
{
assert(ps);
//判断容量是否满
if (ps->_top == ps->_capacity)
{
ps->_a = realloc(ps->_a, sizeof(STDataType)*ps->_capacity * 2);
ps->_capacity *= 2;
}
ps->_a[ps->_top] = x;
ps->_top++;
}
void StackPop(Stack *ps)
{
assert(ps);
//判断还有没有数据可删
if (ps->_top > 0)
{
ps->_top--;
}
}
STDataType StackTop(Stack *ps)
{
assert(ps);
return ps->_a[ps->_top - 1];
}
int StackSize(Stack *ps)
{
assert(ps);
return ps->_top;
}
int StackEmpty(Stack *ps)
{
assert(ps);
return ps->_top == 0 ? 0 : 1;
}
typedef struct {
Stack PushStack;
Stack PopStack;
} MyQueue;
实现
/** Initialize your data structure here. */
MyQueue* myQueueCreate(int maxSize) {
MyQueue *obj = (MyQueue *)malloc(sizeof(MyQueue));
StackInit(&obj->PushStack,maxSize);
StackInit(&obj->PopStack,maxSize);
return obj;
}
/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
assert(obj);
StackPush(&obj->PushStack,x);
}
/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
assert(obj);
//如果PopStack为空的话,把PopStack的数据都转移过来
if(StackEmpty(&obj->PopStack) == 0)
{
while(StackEmpty(&obj->PushStack) != 0)
{
StackPush(&obj->PopStack,StackTop(&obj->PushStack));
StackPop(&obj->PushStack);
}
}
//从PopStack出数据,并返回
int top = StackTop(&obj->PopStack);
StackPop(&obj->PopStack);
return top;
}
/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
assert(obj);
//如果PopStack为空的话,把PopStack的数据都转移过来
if(StackEmpty(&obj->PopStack) == 0)
{
while(StackEmpty(&obj->PushStack) != 0)
{
StackPush(&obj->PopStack,StackTop(&obj->PushStack));
StackPop(&obj->PushStack);
}
}
return StackTop(&obj->PopStack);
}
/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
//两个Stack都为空的话才为空
return (StackEmpty(&obj->PushStack) == 0 && StackEmpty(&obj->PopStack) == 0);
}
void myQueueFree(MyQueue* obj) {
StackDestory(&obj->PushStack);
StackDestory(&obj->PopStack);
free(obj);
obj = NULL;
}
/**
* 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);
*/