栈实现队列
力扣链接:栈实现队列
csdn:队列实现栈链接
这里的思路和队列实现栈的思路大同小异
思路:创建两个栈
用来倒数据,用它们其中一个来反转
元素的入队顺序,用另一个来存储
元素的最终顺序。
- 一个pushst(
用来入栈
),一个popst(用来出栈
)
push接口:
直接在pushst中入就行,栈就是先进后出
peek接口:
这个接口获取的是队头数据,我们需要把pushst中的数据转移
到popst中用到的接口是StackPush
和StackTop
(获取栈顶数据把数据转到popst中)
这样队列头的数据就会到栈顶,在取栈顶数据就可以了
pop接口:
直接取栈顶元素保存,然后pop掉栈顶元素,在返回就行
empty接口:
两个栈都为NULL才是空。为空返回true
,否则返回false
代码:
typedef struct {
ST pushst;
ST popst;
} MyQueue;
bool myQueueEmpty(MyQueue* obj);
MyQueue* myQueueCreate() {
MyQueue* pq = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&pq->pushst);
StackInit(&pq->popst);
return pq;
}
void myQueuePush(MyQueue* obj, int x) {
assert(obj);
StackPush(&obj->pushst, x);
}
int myQueuePop(MyQueue* obj) {
assert(obj);
assert(!myQueueEmpty(obj));
int peek = myQueuePeek(obj);
StackPop(&obj->popst);
return peek;
}
int myQueuePeek(MyQueue* obj) {
assert(obj);
assert(!(myQueueEmpty(obj)));
//popst为空就倒数据
if(StackEmpty(&obj->popst))
{
while(!StackEmpty(&obj->pushst))
{
StackPush(&obj->popst, StackTop(&obj->pushst));
StackPop(&obj->pushst);
}
}
return StackTop(&obj->popst);
}
bool myQueueEmpty(MyQueue* obj) {
assert(obj);
return StackEmpty(&obj->pushst) && StackEmpty(&obj->popst);
}
void myQueueFree(MyQueue* obj) {
assert(obj);
StackDestroy(&obj->pushst);
StackDestroy(&obj->popst);
free(obj);
}
最后的free接口要注意,obj是MyQueue*指针,它指向的空间还有2个ST类型的空间也是需要释放的
整体代码:
typedef int STDatatype;
typedef struct Stack
{
STDatatype* a;
int top; // 栈顶
int capacity; // 容量
}ST;
int StackSize(ST* ps);
bool StackEmpty(ST* ps);
void StackInit(ST* ps)
{
assert(ps);
ps->a = (STDatatype*)malloc(sizeof(STDatatype)* 4);
if (ps->a == NULL)
{
perror("malloc fail");
exit(-1);
}
ps->top = 0;
ps->capacity = 4;
}
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackPush(ST* ps, STDatatype x)
{
assert(ps);
//
if (ps->top == ps->capacity)
{
STDatatype* tmp = (STDatatype*)realloc(ps->a, ps->capacity * 2 * sizeof(STDatatype));
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->a = tmp;
ps->capacity *= 2;
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
STDatatype StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
bool StackEmpty(ST* ps)
{
assert(ps);
/*if (ps->top == 0)
{
return true;
}
else
{
return false;
}*/
return ps->top == 0;
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
typedef struct {
ST pushst;
ST popst;
} MyQueue;
bool myQueueEmpty(MyQueue* obj);
MyQueue* myQueueCreate() {
MyQueue* pq = (MyQueue*)malloc(sizeof(MyQueue));
StackInit(&pq->pushst);
StackInit(&pq->popst);
return pq;
}
void myQueuePush(MyQueue* obj, int x) {
assert(obj);
StackPush(&obj->pushst, x);
}
int myQueuePop(MyQueue* obj) {
assert(obj);
assert(!myQueueEmpty(obj));
int peek = myQueuePeek(obj);
StackPop(&obj->popst);
return peek;
}
int myQueuePeek(MyQueue* obj) {
assert(obj);
assert(!(myQueueEmpty(obj)));
//popst为空就倒数据
if(StackEmpty(&obj->popst))
{
while(!StackEmpty(&obj->pushst))
{
StackPush(&obj->popst, StackTop(&obj->pushst));
StackPop(&obj->pushst);
}
}
return StackTop(&obj->popst);
}
bool myQueueEmpty(MyQueue* obj) {
assert(obj);
return StackEmpty(&obj->pushst) && StackEmpty(&obj->popst);
}
void myQueueFree(MyQueue* obj) {
assert(obj);
StackDestroy(&obj->pushst);
StackDestroy(&obj->popst);
free(obj);
}