leetcode算法题-用队列实现栈

题目要求:

在这里插入图片描述

用队列实现出栈

出队的原则:先进先出。 出栈的原则:后进先出。
例如:
有四个元素1,2,3,4依次入队,其出队顺序为:1,2,3,4。
若1,2,3,4依次入栈,其出栈顺序为4,3,2,1。
也就是说出栈操作去除的是当前队列的最后一个元素4,那我们只需要将前3个元素出队再入队,此时4为队首元素,再将其进行出队即可。下面看图解:
在这里插入图片描述

剩余的获取栈顶元素,销毁栈等操作比较简单在此不多赘述,附上用队列实现栈–leetcode链接

以及完整代码:


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

猜你喜欢

转载自blog.csdn.net/weixin_43962381/article/details/111992098