[Linked List OJ Question 8] Use stacks to implement queues. I didn't expect you to have such a good foundation and get it right so quickly.

Table of contents

Source of topic:

Code:

Idea analysis:

Implementation process:


Source of topic:

Lituo- 232. Implementing queues with stacks

Title description:

Code:

Our stack here has been written. If you don’t know the stack well, you can read this article: CSDN - [Data Structure--C Language] Stack (stack)

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;		// 栈顶
	int capacity;  // 容量 
}Stack;
// 初始化栈 
void StackInit(Stack* ps);
// 入栈 
void StackPush(Stack* ps, STDataType data);
// 出栈 
void StackPop(Stack* ps);
// 获取栈顶元素 
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数 
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps);
// 销毁栈 
void StackDestroy(Stack* ps);
// 初始化栈 
void StackInit(Stack* ps)
{
	assert(ps);

	ps->a = NULL;
	//ps->top = -1;//top 指栈顶数据
	ps->top = 0;//top 指栈顶数据的下一个位置
	ps->capacity = 0;
}
// 入栈 
void StackPush(Stack* ps, STDataType data)
{
	assert(ps);

	if (ps->capacity == ps->top)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail:");
			return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}

	ps->a[ps->top] = data;
	ps->top++;
}
// 出栈 
void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	ps->top--;
}
// 获取栈顶元素 
STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

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

	return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps)
{
	assert(ps);

	if (0 == ps->top)
		return 1;
	else
		return 0;
}
// 销毁栈 
void StackDestroy(Stack* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

typedef struct {
    Stack push;
    Stack pop;
} MyQueue;

MyQueue* myQueueCreate() {
    MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&obj->push);
    StackInit(&obj->pop);

    return obj;
}

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

int myQueuePop(MyQueue* obj) {
    int front = myQueuePeek(obj);
    StackPop(&obj->pop);
    return front;
}

int myQueuePeek(MyQueue* obj) {
    if(StackEmpty(&obj->pop))
    {
        while(!StackEmpty(&obj->push))
        {
            StackPush(&obj->pop, StackTop(&obj->push));
            StackPop(&obj->push);
        }
    }
    return StackTop(&obj->pop);
}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->push) && StackEmpty(&obj->pop);
}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->push);
    StackDestroy(&obj->pop);
}

Idea analysis:

We know the characteristics of the queue: first in first out; the characteristics of the stack: first in first out. So we define two stacks, one is the stack of elements (push stack), and the other is the stack of elements (pop stack). When we want to join the queue, we put the elements directly into the push stack. When we leave the queue, we put all the elements in the push stack into the pop stack, and pop the top elements of the pop stack out of the stack to realize the characteristics of the queue. .

The essence of this question is to realize the characteristics of the queue with the characteristics of the stack.

Implementation process:

1. We first write out all the interfaces of the stack. Then we create two stacks, one is the stack that implements the enqueue function (push stack), and the other is the stack that implements the dequeue function (pop stack);

2. When we want to enter the queue, we put the elements into the push stack. When we want to leave the queue, we first transfer the elements in the push stack to the pop stack, and then pop out the pop stack. In this way, we can achieve the head element;

3. When leaving the queue, we first check whether there are any elements in the pop stack. If there are elements, we can directly pop the top element in the pop stack. If not, first transfer the elements in the push stack to the pop stack; When entering the team, it can be directly entered into the push stack.

Friends who don’t know much about the stack can refer to the article on the code to implement that part of the stack .

*** End of article ***

Guess you like

Origin blog.csdn.net/Ljy_cx_21_4_3/article/details/130875363