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

数据结构 --- 栈

1.栈的初始化

2.入栈

3.出栈

4.取出栈顶元素

5.获取栈中有效元素个数

6.栈的销毁


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

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

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

1.栈的初始化

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//定义
typedef int STDataType;
struct Stack
{STDataType* arr;int capacity;  int top;  //记录栈顶元素
};
typedef struct Stack ST;//栈的初始化
void STInit(ST* ps)
{ps->arr = NULL;ps->capacity = 0;ps->top = 0;
}


2.入栈

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//定义
typedef int STDataType;
struct Stack
{STDataType* arr;int capacity;  int top;  //记录栈顶元素
};
typedef struct Stack ST;//入栈
void STPush(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{perror("realloc fail");exit(1);}ps->capacity = newcapacity;ps->arr = tmp;}ps->arr[ps->top++] = x;//入栈
}

3.出栈

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//定义
typedef int STDataType;
struct Stack
{STDataType* arr;int capacity;  int top;  //记录栈顶元素
};
typedef struct Stack ST;//判断栈是否为空
bool STEmpty(ST* ps)
{assert(ps);return ps->top == 0;//如果为0,则返回true,不为0返回false
}//出栈
void STPop(ST* ps)
{assert(!STEmpty(ps));//记得要取反 --- 栈不为空才可以出栈,否则就是非法访问--ps->top;//对栈顶--即可
}

4.取出栈顶元素

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//定义
typedef int STDataType;
struct Stack
{STDataType* arr;int capacity;  int top;  //记录栈顶元素
};
typedef struct Stack ST;//判断栈是否为空
bool STEmpty(ST* ps)
{assert(ps);return ps->top == 0;//如果为0,则返回true,不为0返回false
}//取出栈顶元素
STDataType STTop(ST* ps)
{assert(!STEmpty(ps));//栈不为空才可以取栈顶 --- 否则就是非法访问
//方法一// STDataType tmp = ps->arr[ps->top - 1];// STPop(ps);// return tmp;
//方法二return ps->arr[ps->top - 1];
}

5.获取栈中有效元素个数

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//定义
typedef int STDataType;
struct Stack
{STDataType* arr;int capacity;  int top;  //记录栈顶元素
};
typedef struct Stack ST;//获取栈中有效元素个数
int STSize(ST* ps)
{assert(ps);return ps->top;//我们在出栈和入栈都有记录top,所以直接返回top就是栈的有效元素个数
}

6.栈的销毁

//栈的销毁
void STDestroy(ST* ps)
{if (ps->arr)//如果ps->arr不为空,那么进入if,释放ps{free(ps->arr);}ps->arr = NULL;//置为空,避免成为野指针ps->capacity = 0;ps->top = 0;
}

http://www.xdnf.cn/news/4190.html

相关文章:

  • 基于RT-Thread的STM32F4开发第二讲第一篇——ADC
  • Flutter 布局
  • dubbo限流
  • Android OKHttp原理简单说明
  • 怎样通过API 实现python调用Chatgpt,gemini
  • 俄罗斯电商市场:增长与变革中的新势力崛起
  • 理解IP四元组与网络五元组:网络流量的“身份证”
  • vue+tsc+noEmit导致打包报TS类型错误问题及解决方法
  • 如何解决Kafka集群中Broker磁盘IO瓶颈?
  • ActiveMQ 安全机制与企业级实践(一)
  • Leetcode Hot 100最长连续序列
  • MongoDB常用操作示例
  • vue3+ts学习!
  • Banana Pi BPI-CM6 是一款八核 RISC-V 模块,兼容 Raspberry Pi CM 载板
  • 算法思想之深度优先搜索(DFS)、递归以及案例(最多能得到多少克黄金、精准核酸检测、最富裕的小家庭)
  • 【Redis篇】linux 7.6安装单机Redis7.0(参数优化详解)
  • Kotlin数据类在Android开发中的应用
  • Linux56 YUM源配置
  • 『Linux_网络』 基于状态机的Connect断线重连
  • 机器学习实操 第二部分 神经网路和深度学习 第13章 使用TensorFlow加载和预处理数据
  • 修改MySQL枚举类型添加‘location‘值
  • 什么是gitlab自动部署,怎么配置gitlab自动部署
  • 论文阅读笔记——ROBOGROUND: Robotic Manipulation with Grounded Vision-Language Priors
  • tinyrenderer笔记(中)
  • Debian系统上PostgreSQL15版本安装调试插件及DBeaver相应配置
  • Flink + Kafka 构建实时指标体系的实战方法论
  • 高防CDN、高防IP vs 高防服务器:核心优势与选型指南
  • Linux Input子系统与驱动开发实战
  • 【Python pass 语句】
  • 人工智能与智能合约:如何用AI优化区块链技术中的合约执行?