【力扣-232.】用栈实现队列

🖊作者 : D. Star.
📘专栏 : 数据结构
😆今日分享 :“多看效应”-----对越熟悉的东西越喜欢的现象,心理学上称为“多看效应”。多看效应不仅仅是在心理学实验中才出现,在生活中,人们也常常能发现这种现象。在大家新认识的人中,有时会有相貌不佳的人,最初,我们可能会觉得这个人难看,可是在多次见到此人之后,逐渐就不觉得他难看了,有时甚至会觉得他在某些方面很有魅力。

请添加图片描述

🔎题目链接:

【力扣-232.】用栈实现队列

🔎题目:

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(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(双端队列)来模拟一个栈,只要是标准的栈操作即可。

示例:

输入:
[“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

🔎分块解析:

✔栈的功能实现:

#define DataType int

typedef struct Stack
{
	DataType* a;
	int top;
	int capacity;
}ST;

//初始化
void stackInit(ST *st);
//销毁
void stackDestory(ST* st);
//入栈
void stackPush(ST* st,DataType x);
//出栈
void stackPop(ST* st);
//获取栈顶元素
DataType stackTop(ST* st);//这里使不使用指针都可以
//判断栈是否为空
bool stackEmpty(ST* st);//这里使不使用指针都可以
//获取栈的大小
int stackSize(ST* st);//这里使不使用指针都可以


//初始化
void stackInit(ST* st)
{
	assert(st);//断言st是否是空指针(因为assert不能为空)

	st->a = (DataType*)malloc(sizeof(DataType));//1个空间
	if (st->a == NULL)
	{
		perror("malloc fail:");
		exit(-1);
	}
	st->capacity = 1;
	st->top = -1;
}

//销毁
void stackDestory(ST* st)
{
	assert(st);//断言st是否是空指针(因为assert不能为空)

	free(st->a);//注意:这里free的是数组的空间,不是结构体
	st->a = NULL;
	st->capacity = 0;
	st->top = -1;
}

//入栈
void stackPush(ST* st, DataType x)
{
	assert(st);//断言st是否是空指针(因为assert不能为空)

	//不为空时,入栈
	// 判断数组是否满
	if (st->top + 1 == st->capacity)
	/*if(st->capacity == st->top )*/
	{
		DataType* tmp = (DataType*)realloc(st->a, 2 * sizeof(DataType) * st->capacity );
		//开辟空间失败时,返回
		if (tmp == NULL)
		{
			perror("realloc fail : ");
			exit(-1);
		}
		//成功时,将开辟空间的地址给到st->a
		st->a = tmp;
		st->capacity *= 2;
	}
	//尾插
	st->top++;
	st->a[st->top] = x;
}

//出栈
void stackPop(ST* st)
{
	assert(st);
	assert(st->top > -1);

	//出栈
	st->top--;
}

//获取栈顶元素
DataType stackTop(ST* st)
{
	assert(st);
	assert(!stackEmpty(st));

	return st->a[st->top];

	//assert(!stackEmpty(&st));
	//return st.a[st.top];
}

//判断栈是否为空
bool stackEmpty(ST* st)
{
	assert(st);
	//法一:
	//if (st->top > -1)
	//{
	//	return false;
	//}
	//else return true;
	
	//法二:
	return st->top == -1;
}

//获取栈的大小
int stackSize(ST* st)
{
	assert(st);

	return st->top + 1;//这里的top为下标,top刚好指向栈顶元素,所以+1;
}

✔用栈实现队列:

typedef struct {
    ST pushST;
    ST popST;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* pq = (MyQueue*)malloc(sizeof(MyQueue));
    stackInit(&pq->pushST);
    stackInit(&pq->popST);
    return pq;
}

//返回队列开头的元素
int myQueuePeek(MyQueue* obj) {
    assert(obj);

    //如果pop为空,则将push中的逐个转移到pop中
    if(stackEmpty(&obj->popST))
    {
        while(!stackEmpty(&obj->pushST))
        {
         //将push中的数据转到pop中
            stackPush(&obj->popST,stackTop(&obj->pushST));
            stackPop(&obj->pushST);
        }
    }

    return stackTop(&obj->popST);
}

//将元素 x 推到队列的末尾
void myQueuePush(MyQueue* obj, int x) {
    assert(obj);

    stackPush(&obj->pushST,x);
}

//如果队列为空,返回 true ;否则,返回 false
bool myQueueEmpty(MyQueue* obj) {
    return stackEmpty(&obj->pushST) && stackEmpty(&obj->popST);
}

//从队列的开头移除并返回元素
int myQueuePop(MyQueue* obj) {
    assert(obj);
    assert(!myQueueEmpty(obj));

    int top = myQueuePeek(obj);
    stackPop(&obj->popST);

    return top;
}

void myQueueFree(MyQueue* obj) {
    stackDestory(&obj->pushST);
    stackDestory(&obj->popST);
    free(obj);
}

🔎代码详情:

#define DataType int

typedef struct Stack
{
	DataType* a;
	int top;
	int capacity;
}ST;

//初始化
void stackInit(ST *st);
//销毁
void stackDestory(ST* st);
//入栈
void stackPush(ST* st,DataType x);
//出栈
void stackPop(ST* st);
//获取栈顶元素
DataType stackTop(ST* st);//这里使不使用指针都可以
//判断栈是否为空
bool stackEmpty(ST* st);//这里使不使用指针都可以
//获取栈的大小
int stackSize(ST* st);//这里使不使用指针都可以


//初始化
void stackInit(ST* st)
{
	assert(st);//断言st是否是空指针(因为assert不能为空)

	st->a = (DataType*)malloc(sizeof(DataType));//1个空间
	if (st->a == NULL)
	{
		perror("malloc fail:");
		exit(-1);
	}
	st->capacity = 1;
	st->top = -1;
}

//销毁
void stackDestory(ST* st)
{
	assert(st);//断言st是否是空指针(因为assert不能为空)

	free(st->a);//注意:这里free的是数组的空间,不是结构体
	st->a = NULL;
	st->capacity = 0;
	st->top = -1;
}

//入栈
void stackPush(ST* st, DataType x)
{
	assert(st);//断言st是否是空指针(因为assert不能为空)

	//不为空时,入栈
	// 判断数组是否满
	if (st->top + 1 == st->capacity)
	/*if(st->capacity == st->top )*/
	{
		DataType* tmp = (DataType*)realloc(st->a, 2 * sizeof(DataType) * st->capacity );
		//开辟空间失败时,返回
		if (tmp == NULL)
		{
			perror("realloc fail : ");
			exit(-1);
		}
		//成功时,将开辟空间的地址给到st->a
		st->a = tmp;
		st->capacity *= 2;
	}
	//尾插
	st->top++;
	st->a[st->top] = x;
}

//出栈
void stackPop(ST* st)
{
	assert(st);
	assert(st->top > -1);

	//出栈
	st->top--;
}

//获取栈顶元素
DataType stackTop(ST* st)
{
	assert(st);
	assert(!stackEmpty(st));

	return st->a[st->top];

	//assert(!stackEmpty(&st));
	//return st.a[st.top];
}

//判断栈是否为空
bool stackEmpty(ST* st)
{
	assert(st);
	//法一:
	//if (st->top > -1)
	//{
	//	return false;
	//}
	//else return true;
	
	//法二:
	return st->top == -1;
}

//获取栈的大小
int stackSize(ST* st)
{
	assert(st);

	return st->top + 1;//这里的top为下标,top刚好指向栈顶元素,所以+1;
}

typedef struct {
    ST pushST;
    ST popST;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* pq = (MyQueue*)malloc(sizeof(MyQueue));
    stackInit(&pq->pushST);
    stackInit(&pq->popST);
    return pq;
}

//返回队列开头的元素
int myQueuePeek(MyQueue* obj) {
    assert(obj);

    //如果pop为空,则将push中的逐个转移到pop中
    if(stackEmpty(&obj->popST))
    {
        while(!stackEmpty(&obj->pushST))
        {
         //将push中的数据转到pop中
            stackPush(&obj->popST,stackTop(&obj->pushST));
            stackPop(&obj->pushST);
        }
    }

    return stackTop(&obj->popST);
}

//将元素 x 推到队列的末尾
void myQueuePush(MyQueue* obj, int x) {
    assert(obj);

    stackPush(&obj->pushST,x);
}

//如果队列为空,返回 true ;否则,返回 false
bool myQueueEmpty(MyQueue* obj) {
    return stackEmpty(&obj->pushST) && stackEmpty(&obj->popST);
}

//从队列的开头移除并返回元素
int myQueuePop(MyQueue* obj) {
    assert(obj);
    assert(!myQueueEmpty(obj));

    int top = myQueuePeek(obj);
    stackPop(&obj->popST);

    return top;
}

void myQueueFree(MyQueue* obj) {
    stackDestory(&obj->pushST);
    stackDestory(&obj->popST);
    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);
*/

感谢家人的阅读,若有不准确的地方 欢迎在评论区指正!

家人们,点个请添加图片描述再走呗~

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

D. Star.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值