Leetcode algorithm problem-using queues to implement stacks

Subject requirements:

Insert picture description here

Use queue to achieve stack

The principle of leaving the team: first in, first out. The principle of stacking: last in, first out.
For example:
there are four elements 1, 2, 3, 4 in the team in turn, the order of their dequeue is: 1, 2, 3, 4.
If 1, 2, 3, and 4 are pushed onto the stack in turn, the pop order is 4, 3, 2, 1.
That is to say, the pop operation removes the last element 4 of the current queue. Then we only need to dequeue the first 3 elements and re-enter the team. At this time, 4 is the first element of the team, and then it can be dequeued. Look at the diagram below:
Insert picture description here

The remaining operations such as obtaining the top element of the stack, destroying the stack, etc. are relatively simple, so I won’t go into details here, and attach the queue to implement the stack – leetcode link

And the complete code:


#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>


typedef int Qdatatype;
typedef struct Qnode {
    
    
	Qdatatype data;
	struct Qnode* next;
}Qnode;

typedef struct Queue {
    
    
	
	//头尾指针
	struct Qnode*head;
	struct Qnode*tail;

}Queue;


//初始化
void Init(Queue* q)
{
    
    
	if (q == NULL)
		return;
	q->head = q->tail = NULL;

}

//创建新结点
struct Qnode* creatnode(Qdatatype val)
{
    
    
	struct Qnode* node = (struct Qnode*)malloc(sizeof(struct Qnode));
	node->data = val;
	node->next = NULL;
	return node;
}

//入队-尾插
void Queuepush(Queue* q,Qdatatype val)
{
    
    
	if (q == NULL)
		return;
	// 尾插
	struct Qnode*node = creatnode(val);
	if (q->head == NULL)
		q->head = q->tail = node;
	else
	{
    
    
		q->tail->next = node;
		q->tail = node;
	}
}

//出队-头删
void Queuepop(Queue* q)
{
    
    
	if (q == NULL||q->head==NULL)
		return;
	//头删
	struct Qnode* next = q->head->next;
	free(q->head); 
	q->head = next;
	//若该队列只有一个结点,尾指针需要置空
	if (q->head == NULL)
		q->tail = NULL;
}

//获取队首元素
Qdatatype Queuefront(Queue* q)
{
    
    
	return q->head->data;
}

//队尾元素
Qdatatype Queueback(Queue* q)
{
    
    
	return q->tail->data;
}

//判断队列是否为空
int Queueempty(Queue* q)
{
    
    
	if (q->head == NULL)
		return 1;
	else
		return 0;
}

//队列大小
int Queuesize(Queue* q)
{
    
    
	int n = 0;
	struct Qnode*p=q->head;
	while (p)
	{
    
    
		++n;
		p = p->next;
	}
	return n;
}

//销毁队列
void Queuedestory(Queue*q)
{
    
    
	struct Qnode*cur=q->head;
	while (cur)
	{
    
    
		struct Qnode*next = cur->next;
		free(cur);
		cur = next;
	}
	q->head = q->tail = NULL;
}



typedef struct {
    
    
    struct Queue q;

} MyStack;

/** Initialize your data structure here. */

MyStack* myStackCreate() {
    
    
    MyStack* pst=(MyStack*)malloc(sizeof(MyStack));
    Init(&pst->q);
    return pst;
}

/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) {
    
    
    Queuepush(&obj->q,x);
}

/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) {
    
    
    int n=Queuesize(&obj->q);
    
    while(n>1)
    {
    
    
        //前n-1个元素出队再入队,此时队首为原来的队尾元素
        int front=Queuefront(&obj->q);
        Queuepop(&obj->q);
        Queuepush(&obj->q,front);
        --n;
    }
    int top=Queuefront(&obj->q);
    Queuepop(&obj->q);
    return top;

}

/** Get the top element. */
int myStackTop(MyStack* obj) {
    
    
    return Queueback(&obj->q);
}

/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) {
    
    
    return Queueempty(&obj->q);
}

void myStackFree(MyStack* obj) {
    
    
    Queuedestory(&obj->q);
    free(obj);
}

/**
 * Your MyStack struct will be instantiated and called as such:
 * MyStack* obj = myStackCreate();
 * myStackPush(obj, x);
 
 * int param_2 = myStackPop(obj);
 
 * int param_3 = myStackTop(obj);
 
 * bool param_4 = myStackEmpty(obj);
 
 * myStackFree(obj);
*/

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/111992098