【leetcode】232. 用栈实现队列

本文介绍了如何使用两个栈(stack1和stack2)模拟一个队列的功能,包括创建、入队、出队、查看队头和检查队列是否为空的操作。作者详细展示了MyQueue结构体的定义与相关函数实现。
摘要由CSDN通过智能技术生成

用栈实现队列 题目链接
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

typedef int valuetype;
typedef struct {
	valuetype* arr;
	int top;
	int capacity;
} Stack;

void Init(Stack* stack);

void Push(Stack* stack, valuetype value);
void Pop(Stack* stack);

valuetype Top(Stack* stack);
int Size(Stack* stack);
bool Empty(Stack* stack);

void Destroy(Stack* stack);

// 和上面图写的不一样,这里Stack不是指针
typedef struct {
    Stack stack1; // 存
    Stack stack2; // 取
} MyQueue;

MyQueue* myQueueCreate() {
    MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue));
    Init(&queue->stack1);
    Init(&queue->stack2);
    return queue;
}

void myQueuePush(MyQueue* obj, int x) {
    assert(obj);
    Push(&obj->stack1, x);
}

int myQueuePop(MyQueue* obj) {
    assert(obj);
    int top = myQueuePeek(obj);
    Pop(&obj->stack2);
    return top;
}

int myQueuePeek(MyQueue* obj) {
    assert(obj);
    if (Empty(&obj->stack2)) {
        // 把stack1的数据挨个取出来放到stack2
        while (!Empty(&obj->stack1)) {
            Push(&obj->stack2, Top(&obj->stack1));
            Pop(&obj->stack1);
        }
    } 
    // 从stack2取队头数据
    return Top(&obj->stack2);
}

bool myQueueEmpty(MyQueue* obj) {
    assert(obj);
    return Empty(&obj->stack1) && Empty(&obj->stack2);
}

void myQueueFree(MyQueue* obj) {
    assert(obj);
    Destroy(&obj->stack1);
    Destroy(&obj->stack2);
    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);
*/

void Init(Stack* stack) {
	assert(stack);
	stack->arr = NULL;
	stack->capacity = stack->top = 0;
}

void Push(Stack* stack, valuetype value) {
	assert(stack);
	if (stack->top == stack->capacity) {
		stack->capacity = stack->capacity == 0 ? 10 : (int)(stack->capacity * 1.5);
		stack->arr = (valuetype*)realloc(stack->arr, sizeof(valuetype) * stack->capacity);
		if (stack->arr == NULL) {
			perror("realloc failed in the function Push(Stack*, valuetype).");
			return;
		}
	}
	stack->arr[stack->top++] = value;
}

void Pop(Stack* stack) {
	assert(stack && stack->top > 0);
	stack->top--;
}

valuetype Top(Stack* stack) {
	assert(stack && stack->top > 0);
	return stack->arr[stack->top - 1];
}

int Size(Stack* stack) {
	assert(stack);
	return stack->top;
}

bool Empty(Stack* stack) {
	assert(stack);
	return stack->top == 0;
}

void Destroy(Stack* stack) {
	assert(stack);
	free(stack->arr);
	stack->arr = NULL;
	stack->capacity = stack->top = 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

念来过倒字名qwq

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

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

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

打赏作者

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

抵扣说明:

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

余额充值