【数据结构初阶】栈

目录

一、栈的概念及结构

二、栈的实现

        2.1 定义一个栈结构

        2.2 初始化栈

        2.3 销毁栈 

        2.4 入栈

        2.5 出栈 

        2.6 获取栈顶元素数据

        2.7获取栈中有效元素个数 

        2.8检测栈是否为空

        2.9 测试

三、栈实现全部代码


一、栈的概念及结构

        栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO (Last In First Out)的原则。

栈就像弹夹一样,后压入的子弹先打出去,先压入的子弹后打出去。

下面是栈进出元素的模拟过程:

二、栈的实现

        栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小,并且CPU缓冲利用率也更高。

        下面我们主要使用数组来实现栈。

        2.1 定义一个栈结构

在实现栈之前我们先要确定栈的结构:这里我们使用自定义结构体类型Stack来实现我们的栈,其中有可动态扩容的数据类型 _a,一个记录栈内所储存元素总个数(栈顶)的_top,和一个记录栈总可以存储数据元素个数的_capacity。

typedef int STDataType;//数据类型
typedef struct Stack
{
	STDataType* _a; //数据
	int _top;		// 栈顶
	int _capacity;  // 容量 
}Stack;

:这里的对int类型进行重定义是为了我们更好的看懂STDataType只是一种数据而不仅仅是int类型。所以我们在使用栈时存储的数据并不限制于int类型,这里仅仅是举例。

        2.2 初始化栈

void StackInit(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	ps->_a = (STDataType*)malloc(sizeof(STDataType)*4);//给栈开一个可以容纳4个数据单位的空间
	if (ps->_a == NULL)//判断开辟是否成功
	{
		perror("malloc");
		exit(-1);
	}
	ps->_capacity = 4;//初始栈容量为4
	ps->_top = 0;//初始栈内元素为0
}

        2.3 销毁栈 

void StackDestroy(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	free(ps->_a);//释放栈数据空间
	ps->_capacity = 0;//将栈容量改为0
	ps->_top = 0;//将栈顶该为0
}

        2.4 入栈

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);//传入的指针不能为空
	if (ps->_capacity == ps->_top)//判断栈是否已满,满了就扩容
	{
		STDataType* temp = (STDataType*)realloc(ps->_a, (ps->_capacity + 4) * sizeof(STDataType));
		if (temp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		ps->_a = temp;
		ps->_capacity += 4;
	}
	ps->_a[ps->_top] = data;//向栈内添加数据
	ps->_top++;//栈顶增加1
}

        2.5 出栈 

void StackPop(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	if (ps->_top <= 0)//栈空就不可出栈了
		return;
	ps->_top--;
}

        2.6 获取栈顶元素数据

STDataType StackTop(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	assert(ps->_top > 0);//栈不能为空
	return ps->_a[ps->_top - 1];//返回栈顶元素
}

        2.7获取栈中有效元素个数 

int StackSize(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	return ps->_top;//此时栈顶元素下标-1就为栈中有效元素个数
}

        2.8检测栈是否为空

bool StackEmpty(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	return ps->_top == 0;//判断栈是否为空
}

        2.9 测试

void Test()
{
	Stack s;
	StackInit(&s);
	printf("栈是否为空:%d\n", StackEmpty(&s));
	StackPush(&s, 1);
	printf("栈内元素个数:%d,栈顶元素数据:%d,栈是否为空:%d\n", StackSize(&s), StackTop(&s), StackEmpty(&s));
	StackPush(&s, 2);
	printf("栈内元素个数:%d,栈顶元素数据:%d,栈是否为空:%d\n", StackSize(&s), StackTop(&s), StackEmpty(&s));
	StackPush(&s, 3);
	printf("栈内元素个数:%d,栈顶元素数据:%d,栈是否为空:%d\n", StackSize(&s), StackTop(&s), StackEmpty(&s));
	StackPush(&s, 4);
	printf("栈内元素个数:%d,栈顶元素数据:%d,栈是否为空:%d\n", StackSize(&s), StackTop(&s), StackEmpty(&s));
	StackPop(&s);
	printf("栈内元素个数:%d,栈顶元素数据:%d,栈是否为空:%d\n", StackSize(&s), StackTop(&s), StackEmpty(&s));
	StackPop(&s);
	printf("栈内元素个数:%d,栈顶元素数据:%d,栈是否为空:%d\n", StackSize(&s), StackTop(&s), StackEmpty(&s));
	StackPop(&s);
	printf("栈内元素个数:%d,栈顶元素数据:%d,栈是否为空:%d\n", StackSize(&s), StackTop(&s), StackEmpty(&s));
	StackDestroy(&s);
	printf("栈是否为空:%d\n", StackEmpty(&s));
}

效果:

三、栈实现全部代码

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
// 支持动态增长的栈
typedef int STDataType;//数据类型
typedef struct Stack
{
	STDataType* _a; //数据
	int _top;		// 栈顶
	int _capacity;  // 容量 
}Stack;
// 初始化栈 
void StackInit(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	ps->_a = (STDataType*)malloc(sizeof(STDataType)*4);//给栈开一个可以容纳4个数据单位的空间
	if (ps->_a == NULL)//判断开辟是否成功
	{
		perror("malloc");
		exit(-1);
	}
	ps->_capacity = 4;//初始栈容量为4
	ps->_top = 0;//初始栈内元素为0
}
// 入栈 
void StackPush(Stack* ps, STDataType data)
{
	assert(ps);//传入的指针不能为空
	if (ps->_capacity == ps->_top)//判断栈是否已满,满了就扩容
	{
		STDataType* temp = (STDataType*)realloc(ps->_a, (ps->_capacity + 4) * sizeof(STDataType));
		if (temp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		ps->_a = temp;
		ps->_capacity += 4;
	}
	ps->_a[ps->_top] = data;//向栈内添加数据
	ps->_top++;//栈顶增加1
}
// 出栈 
void StackPop(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	if (ps->_top <= 0)//栈空就不可出栈了
		return;
	ps->_top--;
}
// 获取栈顶元素 
STDataType StackTop(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	assert(ps->_top > 0);//栈不能为空
	return ps->_a[ps->_top - 1];//返回栈顶元素
}
// 获取栈中有效元素个数 
int StackSize(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	return ps->_top;//此时栈顶元素下标-1就为栈中有效元素个数
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
bool StackEmpty(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	return ps->_top == 0;//判断栈是否为空
}
// 销毁栈 
void StackDestroy(Stack* ps)
{
	assert(ps);//传入的指针不能为空
	free(ps->_a);//释放栈数据空间
	ps->_capacity = 0;//初始栈容量为4
	ps->_top = 0;//初始栈内元素为0
}


本期博客就到这啦,下面将会给大家大家带来数据结构的队列实现,敬请期待~

  • 12
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1e-12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值