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

【数据结构】栈---C语言版(详解!!!)

在这里插入图片描述

文章目录

  • 🐸一、栈的概念及结构
    • 🍄1、栈的概念定义
    • 🍄2、动图演示
      • 🌲入栈
      • 🌲出栈
      • 🌲整体过程
  • 🐸二、栈的实现
  • 🐸三、数组结构栈详解
    • 🍎创建栈的结构
    • ⭕接口1:定义结构体(ST)
    • ⭕接口2:初始化(STInit)
    • ⭕接口3:销毁(STDestroy)
    • ⭕接口4:入栈(STPush)
    • ⭕接口5:出栈(STPop)
    • ⭕接口6:取栈顶数据(STTop)
    • ⭕接口7:判空(STEmpty)
    • ⭕接口8:获取栈的大小(STSize)
  • 🐸四、完整代码
    • 🥝Stack.h
    • 🥝Stack.c
    • 🥝Test.c

在这里插入图片描述

🐸一、栈的概念及结构

🍄1、栈的概念定义

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

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

🍄2、动图演示

🌲入栈

在这里插入图片描述

🌲出栈

在这里插入图片描述

🌲整体过程

在这里插入图片描述

🐸二、栈的实现

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

🐸三、数组结构栈详解

在这里插入图片描述

🍎创建栈的结构

🥰这里先创建三个文件:
1️⃣:Stack.h文件用于函数的声明
2️⃣:Stack.c文件用于函数的定义
3️⃣:Test.c文件用于测试函数
建立三个文件的目的: 将栈作为一个项目来进行编写,方便我们的学习与观察。

⭕接口1:定义结构体(ST)

🥰请看代码与注释👇

//自定义类型
typedef int STDataType;
//创建栈的结构
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;

⭕接口2:初始化(STInit)

🥰请看代码与注释👇

//初始化
void STInit(ST* pst)
{//断言传入指针不为NULLassert(pst);pst->a = NULL;pst->top = -1;  //top指向栈顶数据pst->top = 0;   //top 指向栈顶数据的下一个位置pst->capacity = 0;
}

⭕接口3:销毁(STDestroy)

🥰请看代码与注释👇

//销毁
void STDestroy(ST* pst)
{//断言传入指针不为NULLassert(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;
}

🥰这期内容相对比较简单,希望烙铁们可以理解消化哦!

总结🥰
以上就是 【数据结构】栈—C语言版 的全部内容啦🥳🥳🥳🥳
本文章所在【数据结构与算法】专栏,感兴趣的烙铁可以订阅本专栏哦🥳🥳🥳
前途很远,也很暗,但是不要怕,不怕的人面前才有路。💕💕💕
小的会继续学习,继续努力带来更好的作品😊😊😊
创作写文不易,还多请各位大佬uu们多多支持哦🥰🥰🥰

请添加图片描述

http://www.lryc.cn/news/162344.html

相关文章:

  • sqlserver 联表查询、子查询、窗口函数、聚合函数等概念与例子
  • GO学习之 消息队列(Kafka)
  • 搭建自己的OCR服务,第三步:PPOCRLabel标注工具安装
  • Java学习笔记37——网络编程01
  • powershell 搜索文本并返回行号
  • 网络原理
  • 力扣(LeetCode)算法_C++——同构字符串
  • 网管实战⑼:配置华为S5720交换机
  • 文件上传漏洞第十六关十七关
  • Try llama2 in NUC (by quqi99)
  • 强大易用的开源 建站工具Halo
  • 如何使用vuex
  • 动手深度学习——Windows下的环境安装流程(一步一步安装,图文并配)
  • 个人博客系统-测试用例+自动化测试
  • C语言文件读写常用函数
  • 【C++基础】实现日期类
  • C语言程序设计—通讯录实现
  • 实战:大数据Flink CDC同步Mysql数据到ElasticSearch
  • B-Tree 索引和 Hash 索引的对比
  • 入门Python编程:了解计算机语言、Python介绍和开发环境搭建
  • 深度解析Redisson框架的分布式锁运行原理与高级知识点
  • C#扩展方法
  • uniapp 高度铺满全屏
  • UG\NX二次开发 判断向量在指定的公差内是否为零,判断是否是零向量 UF_VEC3_is_zero
  • 2023年MySQL实战核心技术第一篇
  • hivesql执行过程
  • C语言学习:8、深入数据类型
  • 生成树协议 STP(spanning-tree protocol)
  • 【LeetCode】312.戳气球
  • 商业数据分析概论