用栈实现队列(力扣第232题)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "assert.h"
#include "stdio.h"
#include "stdbool.h"
#include "stdlib.h"
#include "string.h"
#define N 10
typedef int STDataType;
int data;
//静态栈
//typedef struct Stack {
//	STDataType _a[N];
//	int _top;//栈顶元素
//}Stack;

//动态栈
typedef struct ST {
	STDataType* _a;
	int _top;//栈顶元素
	int _capacity;//最大容量
}Stack;

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

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

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

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

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

//判断栈是否为空,是返回1,非空返回0
bool StackEmpty(Stack* pst);

//打印栈同时销毁
void StackPrint(Stack* pst);

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


//初始化栈
void StackInit(Stack* pst)
{
	assert(pst);
	pst->_a = NULL;
	pst->_top = 0;
	pst->_capacity = 0;
}
//入栈
void StackPush(Stack* pst, STDataType x)
{
	assert(pst);
	if (pst->_top == pst->_capacity)
	{
		STDataType newcapacity = pst->_capacity == 0 ? 4 : (pst->_capacity * 2);
		STDataType* temp = (STDataType*)realloc(pst->_a, sizeof(STDataType) * newcapacity);
		if (temp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		pst->_a = temp;
		pst->_capacity = newcapacity;
	}
	pst->_a[pst->_top] = x;
	pst->_top++;
}

//出栈
void StackPop(Stack* pst)
{
	assert(pst);
	assert(pst->_top > 0);
	pst->_top--;
}

//获取栈顶元素
STDataType StackTop(Stack* pst)
{
	assert(pst);
	assert(pst->_top>0);
	return pst->_a[pst->_top-1];
}

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

//判断栈是否为空,是返回1,非空返回0
bool StackEmpty(Stack* pst)
{
	assert(pst);
	if (pst->_top == 0)
		return true;
	else
		return false;
}

//打印栈
void StackPrint(Stack* pst)
{
	while (!StackEmpty(pst))
	{
		printf("%d\n", StackTop(pst));
		StackPop(pst);
	}
}


//销毁栈
void StackDestory(Stack* pst)
{
	assert(pst);
	free(pst->_a);
	pst->_a = NULL;
	pst->_top = pst->_capacity = 0;
}

//队列先进先出,栈先进后出
typedef struct {
    Stack st1;
    Stack st2;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* pq=(MyQueue*)malloc(sizeof(MyQueue));
    if(pq==NULL)
    return NULL;
    StackInit(&pq->st1);
    StackInit(&pq->st2);
    return pq;
}

void myQueuePush(MyQueue* obj, int x) {
    if(StackEmpty(&obj->st1))
    {
        StackPush(&obj->st2,x);
    }
    if(StackEmpty(&obj->st2))
    {
        StackPush(&obj->st1,x);
    }
}

int myQueuePop(MyQueue* obj) {
  if(StackEmpty(&obj->st1))
    {
        while((&obj->st2)->_top!=1){
        StackPush(&obj->st1,StackTop(&obj->st2));
        StackPop(&obj->st2);
        }
        data=StackTop(&obj->st2);
        StackPop(&obj->st2);
        while((&obj->st1)->_top!=0){
            StackPush(&obj->st2,StackTop(&obj->st1));
            StackPop(&obj->st1);
        }
        return data;
    }
    else if(StackEmpty(&obj->st2))
    {
        while((&obj->st1)->_top!=1){
        StackPush(&obj->st2,StackTop(&obj->st1));
        StackPop(&obj->st1);
        }
        data=StackTop(&obj->st1);
        StackPop(&obj->st1);
         while((&obj->st2)->_top!=0){
            StackPush(&obj->st1,StackTop(&obj->st2));
            StackPop(&obj->st2);
        }
    }
        return data;
}

int myQueuePeek(MyQueue* obj) {
    if(StackEmpty(&obj->st1))
    {
        while((&obj->st2)->_top!=0){
        StackPush(&obj->st1,StackTop(&obj->st2));
        StackPop(&obj->st2);
        }
        data=StackTop(&obj->st1);
        while((&obj->st1)->_top!=0){
        StackPush(&obj->st2,StackTop(&obj->st1));
        StackPop(&obj->st1);}
        return data;
    }
    if(StackEmpty(&obj->st2))
    {
        while((&obj->st1)->_top!=0){
        StackPush(&obj->st2,StackTop(&obj->st1));
        StackPop(&obj->st1);
        }
         data=StackTop(&obj->st2);
        while((&obj->st2)->_top!=0){
        StackPush(&obj->st1,StackTop(&obj->st2));
        StackPop(&obj->st2);
        }
    }
    return data;
}

bool myQueueEmpty(MyQueue* obj) {
    if(StackEmpty(&obj->st1)&&StackEmpty(&obj->st2))
    return true;
    return false;
}

void myQueueFree(MyQueue* obj) {
    StackDestory(&obj->st1);
    StackDestory(&obj->st2);
    free(obj);
    obj=NULL;
}

/**
 * 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);
*/
相关推荐
Fanxt_Ja1 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下1 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶1 小时前
算法 --- 字符串
算法
博笙困了1 小时前
AcWing学习——差分
c++·算法
NAGNIP1 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP1 小时前
大模型微调框架之LLaMA Factory
算法
echoarts1 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Python技术极客2 小时前
一款超好用的 Python 交互式可视化工具,强烈推荐~
算法
徐小夕2 小时前
花了一天时间,开源了一套精美且支持复杂操作的表格编辑器tablejs
前端·算法·github
小刘鸭地下城2 小时前
深入浅出链表:从基础概念到核心操作全面解析
算法