题:用栈实现队列 OJ:【用栈实现队列】题解+代码 | 数据结构:栈和队列 | [C语言]
执行出错信息: sandbox run: prog_joined.c:147: myQueuePeek: Assertion `!myQueueEmpty’ failed.
最后执行的输入:
[“MyQueue”,“push”,“push”,“peek”,“pop”,“empty”]
[[],[1],[2],[],[],[]]
错误代码
//返回队列开头的元素
int myQueuePeek(MyQueue* obj) {
assert(obj);
assert(!myQueueEmpty);
if(StackEmpty(&obj->popst))//如果popst栈为空
{
//队列开头的元素 即 push栈栈底的元素
return (obj->pushst)._a[0];
}
else{
return StackTop(&obj->popst);
}
}
【原因】myQueueEmpty函数没传参数
assert(!myQueueEmpty(obj));
! 使用函数的时候不要忘记传参数也不要传错参数