数据结构——栈(C语言版)

前言:

在学习完数据结构顺序表和链表之后,其实我们就可以做很多事情了,后面的栈和队列,其实就是对前面的顺序表和链表的灵活运用,今天我们就来学习一下栈的原理和应用。

准备工作:本人习惯将文件放在test.c、SeqList.c、SeqList.h三个文件中来实现,其中test.c用来放主函数,SeqList.c用来放调用的函数,SeqList.h用来放头文件和函数声明

目录

什么是队列?

栈的节点结构

栈的基本操作

1、初始化

2、销毁

3、插入元素

4、判断栈顶元素是否为空

5、删除元素

6、返回栈顶元素

7、栈中元素个数

完整的栈实例

总结


什么是队列?

队列中的数据是按照先进后出的顺序的,也就是说先进去的数字后出来

因为栈的这种性质,所以栈我们用顺序表来实现比链表方便很多,顺序表就可以实现尾插尾出,所以我们一般就采用顺序表来实现

栈的节点结构

队列采用的顺序表的结构,所以与顺序表差异不大

cpp 复制代码
typedef int STDataType;
typedef struct stack
{
	STDataType* a;
	int top;         //指向栈元素下一位
	int capacity;
}ST;

栈的结构很简单,定义一个整形指针,一个表示容量和一个表示尾部元素的整形变量即可

栈的基本操作

cpp 复制代码
//初始化
void STInit(ST* pst);
//销毁
void STDestroy(ST* pst);
//插入元素
void STPush(ST* pst, STDataType x);
//删除元素
void STPop(ST* pst);
//判断栈顶元素是否为空
bool STEmpty(ST* pst);
//找栈顶元素
STDataType STTop(ST* pst);
//栈中元素个数
STDataType STSize(ST* pst);

看上面的函数声明部分我们就可以看到我们每一步要实现的内容,接下来,我们就来一步一步进行实现

1、初始化

cpp 复制代码
//初始化
void STInit(ST* pst)
{
	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;
}

2、销毁

cpp 复制代码
//销毁
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->capacity = pst->top = 0;
}

3、插入元素

插入元素时要先检查空间是否够用,如果不够用要先进行扩容

cpp 复制代码
//插入元素
void STPush(ST* pst, STDataType x)
{
	if (pst->top == pst->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* newnode = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
		if (newnode == NULL)
		{
			perror("STPush");
			return;
		}
		pst->a = newnode;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}

4、判断栈顶元素是否为空

这一步在下面有用到,例如当删除栈顶元素时,如果栈顶元素为空就无法操作,所以需要判断栈顶元素是否为空

cpp 复制代码
//判断栈顶元素是否为空
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}

5、删除元素

这里删除元素是删除栈顶元素,因为栈的特性是即出即删

cpp 复制代码
//删除元素
void STPop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));
	pst->top--;
}

6、返回栈顶元素

cpp 复制代码
//找栈顶元素
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));
	return pst->a[pst->top - 1];
}

7、栈中元素个数

cpp 复制代码
//栈中元素个数
STDataType STSize(ST* pst)
{
	assert(pst);
	return pst->capacity;
}

完整的栈实例

SeqList.h

cpp 复制代码
//实现栈
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);
//判断栈顶元素是否为空
bool STEmpty(ST* pst);
//找栈顶元素
STDataType STTop(ST* pst);
//栈中元素个数
STDataType STSize(ST* pst);

test.c

cpp 复制代码
//实现栈
void test()
{
	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);
}
int main()
{
	test();
	return 0;
}

SeqList.c

cpp 复制代码
//实现栈
//初始化
void STInit(ST* pst)
{
	pst->a = NULL;
	pst->capacity = 0;
	pst->top = 0;
}
//销毁
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	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* newnode = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
		if (newnode == NULL)
		{
			perror("STPush");
			return;
		}
		pst->a = newnode;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}
//判断栈顶元素是否为空
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}
//删除元素
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];
}
//栈中元素个数
STDataType STSize(ST* pst)
{
	assert(pst);
	return pst->capacity;
}

总结

总之,其实栈就是对顺序表的应用,熟练栈和队列,对我们巩固顺序表和链表帮助很大,当然,栈在一些场景下很实用,后面我会出一个专门的习题讲解篇章,讲数据结构的一些经典题型,感兴趣的可以点赞关注一下

创作不易,还请各位大佬点赞支持一下!!!

相关推荐
NAGNIP6 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队7 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja11 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下11 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶12 小时前
算法 --- 字符串
算法
CoovallyAIHub12 小时前
微软发布 Visual Studio 2026 Insider:AI深度集成,性能大提升,让开发效率倍增(附下载地址)
后端·编程语言·visual studio
博笙困了12 小时前
AcWing学习——差分
c++·算法
NAGNIP12 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP12 小时前
大模型微调框架之LLaMA Factory
算法
echoarts12 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust