本篇文章来用两个栈来实现队列
目录
做题链接:232. 用栈实现队列 - 力扣(LeetCode) (leetcode-cn.com)
用栈实现队列
栈的编写
栈有后进先出的性质,对栈顶进行操作即可,我们选用顺序表来进行栈的实现,前面已经实现过,我们直接上代码:
typedef int Datatype;
typedef struct Stack
{
Datatype* a;
int top; //栈顶位置
int capacity; //容量
}ST;
void StackInit(ST* ps);
void StackDestory(ST* ps);
void StackPush(ST* ps,Datatype x);
void StackPop(ST* ps);
//判断栈是否为空
bool StackEmpty(ST* ps);
//数据个数
int StackSize(ST* ps);
//访问栈顶数据
Datatype StackTop(ST* ps);
//初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;
}
//销毁
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, Datatype x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
ps->a = realloc(ps->a, newcapacity*sizeof(Datatype));
if (ps->a == NULL)
{
printf("realloc fail\n");
exit(-1);
}
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top>0);
ps->top--;
}
//判断栈是否为空
bool StackEmpty(ST* ps)
{
assert(ps);
//if (ps->top > 0)
//{
// return false;
//}
//else
//{
// return true;
//}
return ps->top == 0;
}
//数据个数
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
//访问栈顶数据
Datatype StackTop(ST* ps)
{
assert(ps);
assert(ps->top>0);
return ps->a[ps->top-1];
}
队列的实现分析
队列的定义及初始化
依旧是开辟空间,判断是否成功,然后初始化队列中的两个栈。
typedef struct {
ST Pushs1;
ST Pops2;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));
assert(obj);
StackInit(&obj->Pushs1);
StackInit(&obj->Pops2);
return obj;
}
入队(入栈)
栈初始化,已经置空,直接入栈即可
void myQueuePush(MyQueue* obj, int x) {
assert(obj);
StackPush(&obj->Pushs1,x);
}
出队(出栈)
int myQueuePop(MyQueue* obj) {
assert(obj);
//s2空时需要push进s1元素才能出
if(StackEmpty(&obj->Pops2))
{
while(!StackEmpty(&obj->Pushs1))
{
StackPush(&obj->Pops2,StackTop(&obj->Pushs1));
StackPop(&obj->Pushs1);
}
}
int front=StackTop(&obj->Pops2);
StackPop(&obj->Pops2);
return front;
}
返回队头元素
int myQueuePeek(MyQueue* obj) {
assert(obj);
if(StackEmpty(&obj->Pops2))
{
while(!StackEmpty(&obj->Pushs1))
{
StackPush(&obj->Pops2,StackTop(&obj->Pushs1));
StackPop(&obj->Pushs1);
}
}
return StackTop(&obj->Pops2);
}
判断队列是否为空和队列的销毁
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->Pushs1)&&StackEmpty(&obj->Pops2);
}
void myQueueFree(MyQueue* obj) {
StackDestory(&obj->Pushs1);
StackDestory(&obj->Pops2);
free(obj);
}