题目描述
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push
、pop
、peek
、empty
):
实现 MyQueue
类:
void push(int x)
将元素 x 推到队列的末尾int pop()
从队列的开头移除并返回元素int peek()
返回队列开头的元素boolean empty()
如果队列为空,返回true
;否则,返回false
说明:
- 你 只能 使用标准的栈操作 —— 也就是只有
push to top
,peek/pop from top
,size
, 和is empty
操作是合法的。 - 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例 1:
输入: ["MyQueue", "push", "push", "peek", "pop", "empty"] [[], [1], [2], [], [], []] 输出: [null, null, null, 1, 1, false]
基本思路
1.我们把队列的数据压入栈中,这样栈底的数据是队列头的数据。
2.此时我们把栈中的数据在压入另外一个栈中,这样另外一个栈顶就我们的队列头。
2.按照前面的思路,每次出队列我们就去查看栈2有没有数据,有的话就直接出栈,没有的话就把栈1的数据压入到栈2。
3.每次入队列都压入到栈1中
代码如下
typedef int Stack_Data_Ty;
typedef struct Stack
{
Stack_Data_Ty * data;
int top;//栈顶
int capacity;//栈空间大小
}Stack;
//初始化
void StackInit(Stack * ST);
//出
void StackPop(Stack * ST);
//入
void StackPush(Stack * ST,Stack_Data_Ty x);
//是否为空
bool StackEmpt(Stack * ST);
//获取大小
int StackSize(Stack * ST);
//获取顶部数据
Stack_Data_Ty StackTop(Stack * ST);
//获取栈的大小
/**
* role:初始化栈
* parm:ST---要进行初始化的栈
* return:void
*
*
**/
void StackInit(Stack * ST)
{
assert(ST);
ST->data = NULL;
ST->capacity = 0;
ST->top = 0;
}
/**
* role:入栈操作
* parm:ST---目标栈
* x --- 数据
* return:void
*
*
**/
void StackPush(Stack * ST,Stack_Data_Ty x)
{
assert(ST);
if(ST->capacity == ST->top)
{
ST->capacity = ST->capacity == 0?6:ST->capacity * 2;
ST->data = (Stack_Data_Ty*)realloc(ST->data,ST->capacity * sizeof(Stack_Data_Ty));
if(ST->data == NULL)
{
perror("realloc error");
exit(-1);
}
}
ST->data[ST->top++] = x;
}
/**
* role:出栈操作
* parm:ST---目标栈
*
* return:void
**/
void StackPop(Stack * ST)
{
assert(ST);
assert(!StackEmpt(ST));
ST->top--;
}
/**
* role:判断栈是否为空
* parm:ST---目标栈
*
* return:true为空 false为非空
**/
bool StackEmpt(Stack * ST)
{
assert(ST);
return ST->top == 0;
}
/**
* role:获取栈顶数据
* parm:ST---目标栈
*
* return:数据
**/
Stack_Data_Ty StackTop(Stack * ST)
{
assert(ST);
assert(!StackEmpt(ST));
return ST->data[ST->top -1];
}
/**
* role:获取栈的大小
* parm:ST---目标栈
*
* return:栈大小
**/
int StackSize(Stack * ST)
{
assert(ST);
return ST->top;
}
typedef struct {
Stack Input;
Stack Output;
} MyQueue;
MyQueue* myQueueCreate()
{
MyQueue * obj = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&obj->Input);
StackInit(&obj->Output);
return obj;
}
void myQueuePush(MyQueue* obj, int x)
{
StackPush(&obj->Input,x);
}
int myQueuePop(MyQueue* obj)
{
Stack_Data_Ty ret = 0;
//出数据的栈没有数据
if(StackEmpt(&obj->Output))
{
//把Input的数据压入Output
while(!StackEmpt(&obj->Input))
{
StackPush(&obj->Output,StackTop(&obj->Input));
StackPop(&obj->Input);
}
}
ret = StackTop(&obj->Output);
StackPop(&obj->Output);
return ret;
}
int myQueuePeek(MyQueue* obj)
{
Stack_Data_Ty ret = 0;
//出数据的栈没有数据
if(StackEmpt(&obj->Output))
{
//把Input的数据压入Output
while(!StackEmpt(&obj->Input))
{
StackPush(&obj->Output,StackTop(&obj->Input));
StackPop(&obj->Input);
}
}
ret = StackTop(&obj->Output);
return ret;
}
bool myQueueEmpty(MyQueue* obj)
{
if(StackEmpt(&obj->Input) &&StackEmpt(&obj->Output))
{
return true;
}
return false;
}
void myQueueFree(MyQueue* obj)
{
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);
*/