Problema de uso del algoritmo Leetcode en colas para implementar pilas

Requisitos de la asignatura:

Inserte la descripción de la imagen aquí

Usa la cola para lograr la pila

El principio de dejar el equipo: primero en entrar, primero en salir. El principio de apilamiento: último en entrar, primero en salir.
Por ejemplo:
hay cuatro elementos 1, 2, 3 y 4 en el equipo a su vez, el orden de su salida de la cola es: 1, 2, 3, 4.
Si se introducen 1, 2, 3, 4 en la pila a su vez, el orden de salida es 4, 3, 2, 1.
Es decir, lo que elimina la operación pop es el último elemento 4 de la cola actual. Luego solo necesitamos sacar los primeros 3 elementos y volver a ingresar a la cola. En este momento, 4 es el elemento líder del equipo, y luego sacarlo de la cola. Mire el diagrama a continuación:
Inserte la descripción de la imagen aquí

Las operaciones restantes, como obtener el elemento superior de la pila, destruir la pila, etc., son relativamente simples, por lo que no entraré en detalles aquí y adjuntaré el enlace para realizar la pila con la cola: enlace de código de código.

Y el código completo:


#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);
*/

Supongo que te gusta

Origin blog.csdn.net/weixin_43962381/article/details/111992098
Recomendado
Clasificación