当前位置: 首页 > news >正文

C语言 数据结构 【栈】动态模拟实现

引言

        动态模拟实现栈的各个接口

一、栈的概念与结构

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

压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。

出栈:栈的删除操作叫做出栈。出数据也在栈顶。 

栈底层结构选型

        栈的实现⼀般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。而且与链表的结构体相比,数组所占内存的较小。 

 二、栈的模拟实现

因为栈是用动态数组来模拟实现的,所以顺序表要是会的话,这个是很简单的

分三个文件来写:以代码注释为主

test.c //测试文件,测试栈的接口是否正确
Stack.h //实现栈所需要的所有的头文件(核心)
Stack.c //实现栈各个接口的代码(核心)

1、在Stack.h中定义栈的结构:

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int STDataType;  //取别名,方便以后修改数据类型

typedef struct Stack
{
	STDataType* arr;   //数组
	int top;           //栈的有效元素个数
	int capacity;      //栈的总大小
}ST;                   //取别名,方便下面代码的书写

2、栈的初始化、入栈

//初始化
void StackInit(ST* ps)
{
	ps->arr = NULL;      //先将数组指针设置为NULL
	ps->top = ps->capacity = 0; //都是0
}

//入栈 -- 栈顶入栈
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity) //空间不够先增容
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity; //以二倍方式扩容
		STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;  //将扩容后的地址给Stack的arr
		ps->capacity = newCapacity;  //修改空间大小;
	}
	ps->arr[ps->top++] = x; //赋值
}

3、判断栈是否为空、出栈操作

//判断栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

//出栈 -- 栈顶出栈
void StackPop(ST* ps)
{
	assert(!StackEmpty(ps));
	--ps->top;  //将栈顶减一
}

4、取栈顶元素(不出栈)、获取栈中有效元素个数

//取栈顶元素(不出栈)
STDataType StackTop(ST* ps)
{
	assert(!StackEmpty(ps));
	return ps->arr[ps->top - 1];
}
//获取栈中有效元素个数
int StackSize(ST* ps)
{
	return ps->top;
}

5、销毁栈 

//摧毁
void StackDestory(ST* ps)
{
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}

3、所有代码:

Stack.h中的代码:

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

typedef int STDataType;  //取别名,方便以后修改数据类型

typedef struct Stack
{
	STDataType* arr;   //数组
	int top;           //栈的有效元素个数
	int capacity;      //栈的总大小
}ST;                   //取别名,方便下面代码的书写

//初始化
void StackInit(ST* ps);

//摧毁
void StackDestroy(ST* ps);

//入栈 -- 栈顶入栈
void StackPush(ST* ps, STDataType x);

//判断栈是否为空
bool StackEmpty(ST* ps);

//出栈 -- 栈顶出栈
void StackPop(ST* ps);

//取栈顶元素(不出栈)
STDataType StackTop(ST* ps);

//获取栈中有效元素个数
int StackSize(ST* ps);

Stack.c文件中的代码:

#include"Stack.h"

//初始化
void StackInit(ST* ps)
{
	ps->arr = NULL;      //先将数组指针设置为NULL
	ps->top = ps->capacity = 0; //都是0
}

//入栈 -- 栈顶入栈
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity) //空间不够先增容
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity; //以二倍方式扩容
		STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;  //将扩容后的地址给Stack的arr
		ps->capacity = newCapacity;  //修改空间大小;
	}
	ps->arr[ps->top++] = x; //赋值
}

//判断栈是否为空
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

//出栈 -- 栈顶出栈
void StackPop(ST* ps)
{
	assert(!StackEmpty(ps));
	--ps->top;  //将栈顶减一
}

//取栈顶元素(不出栈)
STDataType StackTop(ST* ps)
{
	assert(!StackEmpty(ps));
	return ps->arr[ps->top - 1];
}
//获取栈中有效元素个数
int StackSize(ST* ps)
{
	return ps->top;
}

//摧毁
void StackDestory(ST* ps)
{
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}

test.c文件中的代码:

#include"Stack.h"

int main()
{
	ST st;
	StackInit(&st);
	StackPush(&st, 1);
	StackPush(&st, 2);
	StackPush(&st, 3);
	StackPush(&st, 4);
	StackPop(&st);
	StackPop(&st);
	int size = StackSize(&st);
	printf("%d ", size);
	while (!StackEmpty(&st))
	{
		StackPop(&st);
		size = StackSize(&st);
		printf("%d ", size);
	}
	return 0;
}

相关文章:

  • 学习日志37—基于变分量子电路的量子机器学习算法综述
  • 气质联用仪器GCMSMS配置CTC 性能测试
  • 拥抱AI,永洪vividime迈进数据智能时代
  • 视频设备轨迹回放平台EasyCVR远程监控体系落地筑牢国土监管防线
  • 常用控件的使用
  • Idea将Java工程打包成war包并发布
  • mysql-INNODB_FT_INDEX_TABLE表中的 first_doc_id 和、last_doc_id 和doc_id
  • 软考(软件设计师)之操作系统
  • GRBL运动控制算法(四)加减速运算
  • JAVA接口和继承
  • AI-人工智能-多模态药物识别AI新算法GSFM,为精准药物表征装上“智慧眼”
  • 深入探索 `malloc`:内存分配失败的原因及正确使用规范
  • C语言:32位数据转换为floaf解析
  • RHCSA Linux 系统 文件的查看、复制、移动、重命名、编辑文件
  • GPT-5、o3和o4-mini即将到来
  • PCI认证 密钥注入 ECC算法工具 NID_secp521r1 国密算法 openssl 全套证书生成,从证书提取公私钥数组 x,y等
  • WinForm真入门(11)——ComboBox控件详解
  • 996引擎-源码学习:Cocos2d-Lua 的 class(classname, ...)
  • 2025 年河北交安安全员考试:巧用行业报告丰富知识储备​
  • 信息安全测评中心-国产化!