Leetcode Daily Question - "Using a Stack to Implement a Queue"

How are you uu from CSDN, today, Xiaoyalan’s content is to use stacks to implement queues, which seems to have something to do with Xiaoyalan’s last blog "Using Queues to Realize Stacks". In fact, it is indeed In this way, let's enter the world of Leetcode! ! !


Leetcode Daily Question - "Using Queues to Realize Stacks"


 

 

 

 If you are interested, you can take a look at the contents of the stack and queue written by Xiao Yalan:

Stack - "Data Structure and Algorithm"

Queue - "Data Structure and Algorithm"


 

Then guide the data, that is, Pop it:

 

If you want to continue Pop, you don't need to import data like the previous topic "Using Queues to Implement Stacks"

This time Pop can directly pop in the second queue 

If you want Push5 6, what should you do?

We might as well do this: write "dead" directly, set one queue as dedicated to output data, and another queue as dedicated to input data

If you want Push5 6, then, just like this:

 

What if there are three more Pops?

Just know such a principle: as long as the popst (the second queue) is not empty, the data can be output all the time, if the popst is empty, the data will be imported, and then output after the import! ! !

So, the idea of ​​this topic is like this, next, let's start writing code! ! !


First of all, we use C language to tear up a stack :

typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;//栈顶
	int capacity;//容量
}Stack;

// 初始化栈 
void StackInit(Stack* pst);

// 销毁栈 
void StackDestroy(Stack* pst);

// 入栈 
void StackPush(Stack* pst, STDataType x);

// 出栈 
void StackPop(Stack* pst);

// 获取栈顶元素 
STDataType StackTop(Stack* pst);

// 获取栈中有效元素个数 
int StackSize(Stack* pst);

// 检测栈是否为空 
bool StackEmpty(Stack* pst);

// 初始化栈 
void StackInit(Stack* pst)
{
	assert(pst);
	pst->a = NULL;
	pst->top = 0;
	pst->capacity = 0;
}

// 销毁栈 
void StackDestroy(Stack* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}

// 入栈 
void StackPush(Stack* pst, STDataType x)
{
	assert(pst);
	//扩容
	if (pst->top == pst->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}

// 检测栈是否为空
bool StackEmpty(Stack* pst)
{
	assert(pst);
	if (pst->top == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
	//return pst->top==0;
}

// 出栈 
void StackPop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));
	pst->top--;
}

// 获取栈顶元素 
STDataType StackTop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));
	return pst->a[pst->top - 1];
}

// 获取栈中有效元素个数 
int StackSize(Stack* pst)
{
	assert(pst);
	return pst->top;
}

 The remaining functions can be followed by Leetcode

typedef struct {
    Stack pushst;
    Stack popst;
} MyQueue;

This is still an anonymous structure, which is renamed MyQueue using typedef. In this structure, two structures are defined, one is popst for data output, and the other is pushst for data input.

MyQueue* myQueueCreate() {
    MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));
    if(obj==NULL)
    {
        perror("malloc fail");
        return NULL;
    }
    StackInit(&obj->pushst);
    StackInit(&obj->popst);
    return obj;
}
void myQueuePush(MyQueue* obj, int x) {
    StackPush(&obj->pushst,x);
}

 

peek has the meaning of "peep" 

 

//导数据了之后取顶上的数据
int myQueuePeek(MyQueue* obj) {
    //popst为空才需要导数据
    if(StackEmpty(&obj->popst))
    {
        //pushst不为空
        while(!StackEmpty(&obj->pushst))
        {
            //把pushst(栈顶)的数据导给popst
            StackPush(&obj->popst,StackTop(&obj->pushst));
            //然后把pushst的数据删掉
            StackPop(&obj->pushst);
        }
    }
    //popst本身就不为空
    return StackTop(&obj->popst);
}
int myQueuePop(MyQueue* obj) {
    int front=myQueuePeek(obj);
    StackPop(&obj->popst);
    return front;
}
bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->pushst)&&StackEmpty(&obj->popst);
}
void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->popst);
    StackDestroy(&obj->pushst);
    free(obj);
}

The complete code for this topic is as follows:

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;//栈顶
	int capacity;//容量
}Stack;
// 初始化栈 
void StackInit(Stack* pst);
// 销毁栈 
void StackDestroy(Stack* pst);
// 入栈 
void StackPush(Stack* pst, STDataType x);
// 出栈 
void StackPop(Stack* pst);
// 获取栈顶元素 
STDataType StackTop(Stack* pst);
// 获取栈中有效元素个数 
int StackSize(Stack* pst);
// 检测栈是否为空 
bool StackEmpty(Stack* pst);
// 初始化栈 
void StackInit(Stack* pst)
{
	assert(pst);
	pst->a = NULL;
	pst->top = 0;
	pst->capacity = 0;
}
// 销毁栈 
void StackDestroy(Stack* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}
// 入栈 
void StackPush(Stack* pst, STDataType x)
{
	assert(pst);
	//扩容
	if (pst->top == pst->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(pst->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = tmp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}
// 检测栈是否为空
bool StackEmpty(Stack* pst)
{
	assert(pst);
	if (pst->top == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
	//return pst->top==0;
}
// 出栈 
void StackPop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));
	pst->top--;
}
// 获取栈顶元素 
STDataType StackTop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));
	return pst->a[pst->top - 1];
}
// 获取栈中有效元素个数 
int StackSize(Stack* pst)
{
	assert(pst);
	return pst->top;
}
typedef struct {
    Stack pushst;
    Stack popst;
} MyQueue;

MyQueue* myQueueCreate() {
    MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));
    if(obj==NULL)
    {
        perror("malloc fail");
        return NULL;
    }
    StackInit(&obj->pushst);
    StackInit(&obj->popst);
    return obj;
}

void myQueuePush(MyQueue* obj, int x) {
    StackPush(&obj->pushst,x);
}

int myQueuePop(MyQueue* obj) {
    int front=myQueuePeek(obj);
    StackPop(&obj->popst);
    return front;
}
//导数据了之后取顶上的数据
int myQueuePeek(MyQueue* obj) {
    //popst为空才需要导数据
    if(StackEmpty(&obj->popst))
    {
        //pushst不为空
        while(!StackEmpty(&obj->pushst))
        {
            //把pushst(栈顶)的数据导给popst
            StackPush(&obj->popst,StackTop(&obj->pushst));
            //然后把pushst的数据删掉
            StackPop(&obj->pushst);
        }
    }
    //popst本身就不为空
    return StackTop(&obj->popst);
}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->pushst)&&StackEmpty(&obj->popst);
}

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);

*/


Alright, this is the end of Xiao Yalan's content of using stacks to implement queues today, and I will continue to work hard on the questions! ! !

 

 

Guess you like

Origin blog.csdn.net/weixin_74957752/article/details/130817291