
 
文章目录
 
 
🐸一、栈的概念及结构
🍄1、栈的概念定义
🍄2、动图演示
🌲入栈

🌲出栈

🌲整体过程

🐸二、栈的实现
🐸三、数组结构栈详解

🍎创建栈的结构
⭕接口1:定义结构体(ST)
🥰请看代码与注释👇
//自定义类型
typedef int STDataType;
//创建栈的结构
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
 
⭕接口2:初始化(STInit)
🥰请看代码与注释👇
//初始化
void STInit(ST* pst)
{
	//断言传入指针不为NULL
	assert(pst);
	pst->a = NULL;
	pst->top = -1;  //top指向栈顶数据
	pst->top = 0;   //top 指向栈顶数据的下一个位置
	pst->capacity = 0;
}
 
⭕接口3:销毁(STDestroy)
🥰请看代码与注释👇
//销毁
void STDestroy(ST* pst)
{
	//断言传入指针不为NULL
	assert(pst);
	//释放
	free(pst->a);
	pst->a = NULL;
	pst->capacity = pst->top = 0;
}
 
⭕接口4:入栈(STPush)
🥰请看代码与注释👇
//入栈
void STPush(ST* pst, STDataType x)
{
	if (pst->top == pst->capacity)
	{
		int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* temp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));
		if (temp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = temp;
		pst->capacity = newCapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}
 
⭕接口5:出栈(STPop)
🥰请看代码与注释👇
//出栈
void STPop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));
	pst->top--;
}
 
⭕接口6:取栈顶数据(STTop)
🥰请看代码与注释👇
//取栈顶数据
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));
	return pst->a[pst->top - 1];
}
 
⭕接口7:判空(STEmpty)
🥰请看代码与注释👇
//判空
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}
 
⭕接口8:获取栈的大小(STSize)
🥰请看代码与注释👇
//获取栈的大小
int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}
 
🐸四、完整代码
🥝Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
//初始化
void STInit(ST* pst);
//销毁
void STDestroy(ST* pst);
//入栈
void STPush(ST* pst, STDataType x);
//出栈
void STPop(ST* pst);
//取栈顶数据
STDataType STTop(ST* pst);
//判空
bool STEmpty(ST* pst);
//获取栈的大小
int STSize(ST* pst);
 
🥝Stack.c
#include"Stack.h"
//初始化
void STInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;
	pst->top = -1;  //top指向栈顶数据
	pst->top = 0;   //top 指向栈顶数据的下一个位置
	pst->capacity = 0;
}
//销毁
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->capacity = pst->top = 0;
}
//入栈
void STPush(ST* pst, STDataType x)
{
	if (pst->top == pst->capacity)
	{
		int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* temp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));
		if (temp == NULL)
		{
			perror("realloc fail");
			return;
		}
		pst->a = temp;
		pst->capacity = newCapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}
//出栈
void STPop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));
	pst->top--;
}
//取栈顶数据
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));
	return pst->a[pst->top - 1];
}
//判空
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}
//获取栈的大小
int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}
 
🥝Test.c
#include"Stack.h"
//入栈测试
void TestStack1()
{
	ST st;
	STInit(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	STPush(&st, 3);
	STPush(&st, 4);
	while (!STEmpty(&st))
	{
		printf("%d ", STTop(&st));
		STPop(&st);
	}
	STDestroy(&st);
}
//测试
void TestStack2()
{
	ST st;
	STInit(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	printf("%d ", STTop(&st));
	STPop(&st);
	STPush(&st, 3);
	STPush(&st, 4);
	while (!STEmpty(&st))
	{
		printf("%d ", STTop(&st));
		STPop(&st);
	}
	STDestroy(&st);
}
int main()
{
	//TestStack1();
	//TestStack2();
	return 0;
}
 
🥰这期内容相对比较简单,希望烙铁们可以理解消化哦!










