数据结构之栈

栈的概念

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

  • 压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
  • 出栈:栈的删除操作叫做出栈。出数据也在栈顶。

栈的功能实现

创建三个文件

  1. 头文件Stack.h:引头文件,定义结构体,函数声明
  2. 函数文件Stack.c:各种函数实现
  3. 测试文件test.c:测试函数功能
    在这里插入图片描述

栈结构的实现

typedef int STDatatype;
typedef struct Stack
{
	STDatatype* a;
	//栈顶位置
	int top;	
	//容量
	int capacity;
}ST;

栈的初始化

将结构体指针a初始化为空指针,顶端数据初始化为0,容量初始化为0

void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

栈的判空

判断栈中存储的数据个数是否为零

bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->cap == 0;
}

读取栈顶数据

这个很容易实现,只需返回栈顶数据即可

STDatatype StackTop(ST* ps)
{
	assert(ps);
	//判断栈是否为空
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}

插入数据

思路

  1. 检查空间是否已满,若满了需要增容
  2. 在栈顶插入数据,并使top+1
void StackPush(ST* ps, STDatatype x)
{
	assert(ps);

	//检查空间,如果不够就扩容
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDatatype* tmp = realloc(ps->a, sizeof(STDatatype)*newcapacity);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	//在栈顶插入数据
	ps->a[ps->top] = x;
	ps->top++;
}

检测
在这里插入图片描述

删除数据

只需将top-1就可以了,注意栈不能为空

void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	ps->top--;
}

在这里插入图片描述

栈中数据个数

返回我top即可

int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

检测
在这里插入图片描述

栈的销毁

释放指针,将栈顶数据置为0,空间置为0

void StackDestroy(ST* ps)
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
	}
	ps->a;
	ps->top=0;
	ps->capacity = 0;
}

总结

Stack.h文件

#pragma once
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef int STDatatype;
typedef struct Stack
{
	STDatatype* a;
	//顶端数据
	int top;	
	//容量
	int capacity;
}ST;

//初始化
void StackInit(ST* ps);
//判空
bool StackEmpty(ST* ps);
//读取栈顶数据
STDatatype StackTop(ST* ps);
//插入数据
void StackPush(ST* ps, STDatatype x);
//删除数据
void StackPop(ST* ps);
//计算栈的数据个数
int StackSize(ST* ps);
//销毁栈
void StackDestroy(ST* ps);

Stack.c文件

#include "Stack.h"

void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

STDatatype StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	return ps->a[ps->top - 1];
}

void StackPush(ST* ps, STDatatype x)
{
	assert(ps);

	//检查空间,如果不够就扩容
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDatatype* tmp = realloc(ps->a, sizeof(STDatatype)*newcapacity);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	//在栈顶插入数据
	ps->a[ps->top] = x;
	ps->top++;
}

void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));

	ps->top--;
}

int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

void StackDestroy(ST* ps)
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
	}
	ps->a;
	ps->top=0;
	ps->capacity = 0;
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值