用栈实现队列--栈和队列

用两个栈实现队列
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

//栈的代码---------------------------首
typedef  int STDatatype;
typedef  struct Stack
{
	STDatatype* a;
	int top;
	int capacity;
}ST;

void  StackInit(ST* ps);
void  StackDestroy(ST* ps);
//顶进顶出
void  StackPush(ST* ps, STDatatype x);
void  StackPop(ST* ps);
//判空
bool  StackEmpty(ST* ps);
int StackSise(ST* ps);
//寻找顶点
STDatatype StackTop(ST* ps);

void  StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;//-1
	ps->capacity = 0;
}
void  StackDestroy(ST* ps)
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
	}
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

//顶进顶出
//插入
void  StackPush(ST* ps, STDatatype x)
{
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;//不等于0改成2倍
			STDatatype* tmp = realloc(ps->a, sizeof(STDatatype)*newcapacity);
			if (tmp == NULL)
			{
				printf("realloc fail");
			}
			ps->a = tmp;
			ps->capacity = newcapacity;
	}
	//初始值为0,先赋值再++
	ps->a[ps->top] = x;
	ps->top++;
}
//删除
void  StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	--ps->top;
}
//判空
bool  StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
int StackSise(ST* ps)
{
	assert(ps);
	//因为初始化为0不是-1所以size就是top,-1要返回top+1
	return ps->top;
}
//寻找顶点
STDatatype StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->a[ps->top - 1];
}
//栈的代码----------------------------------尾



typedef struct {
  ST  pushST;//最好理解这里为什么不用指针
  ST  popST;
} MyQueue;

/** Initialize your data structure here. */
//创建空间并初始化
MyQueue* myQueueCreate() {
    MyQueue* q=(MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&q->pushST);
    StackInit(&q->popST);

    return q;

}

/** Push element x to the back of queue. */
void myQueuePush(MyQueue* obj, int x) {
    assert(obj);
    StackPush(&obj->pushST,x);
}

/** Get the front element. */
//取队头的数据
int myQueuePeek(MyQueue* obj) {
    assert(obj);
    if(StackEmpty(&obj->popST))//如果第二个栈为空
    {
        while(!StackEmpty(&obj->pushST))//第一个栈不为空
        {
            //取数据插数据
            StackPush(&obj->popST,StackTop(&obj->pushST));
            StackPop(&obj->pushST);
        }
    }
    return StackTop(&obj->popST);
}

/** Removes the element from in front of queue and returns that element. */
//注意看上方有返回值要求 ,int myQueuePop(MyQueue* obj)也可以看得出有返回值
int myQueuePop(MyQueue* obj) {
    assert(obj);
    if(StackEmpty(&obj->popST))//如果第二个栈为空
    {
        while(!StackEmpty(&obj->pushST))//第一个栈不为空
        {
            //取数据插数据
            StackPush(&obj->popST,StackTop(&obj->pushST));
            StackPop(&obj->pushST);
        }
    }
    int  front=StackTop(&obj->popST);//记录头
    StackPop(&obj->popST);//队列-头出
    return front;

//或者写
//int  front=StackPop(&popST);
// StackPop(&obj->popST)
// return front;
}

/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
  assert(obj);
  return  StackEmpty(&obj->popST) && StackEmpty(&obj->pushST);//两者都为才为空
}

void myQueueFree(MyQueue* obj) {//销毁
     StackDestroy(&obj->popST);
     StackDestroy(&obj->pushST);
     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);
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值