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

[数据结构]5. 栈-Stack

栈-Stack

  • 1. 介绍
  • 2. 栈的实现
    • 2.1 基于链表的实现
    • 2.2 基于数组的实现
  • 3. 栈操作
    • Create
    • Initilizate
    • Destory
    • Push
    • Pop
    • Top
    • Empty
    • Size

1. 介绍

栈(stack) 是一种遵循先入后出逻辑的线性数据结构。顶部称为“栈顶”,底部称为“栈底”。把元素添加到栈顶的操作叫作“入栈”,删除栈顶元素的操作叫作“出栈”。
请添加图片描述

2. 栈的实现

2.1 基于链表的实现

请添加图片描述请添加图片描述请添加图片描述

2.2 基于数组的实现

请添加图片描述请添加图片描述
请添加图片描述

3. 栈操作

Create

typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;

Initilizate

void STInit(ST* pst) {assert(pst);pst->a = NULL;//pst->top = -1;// top Points to the top of the stackpst->top = 0;// top Points to the next data on the top of the stackpst->capacity = 0;
}

Destory

void STDestory(ST* pst) {assert(pst);free(pst->a);pst->top = pst->capacity = 0;
}

Push

void STPush(ST* pst, STDataType x) {// Enpend capacityif (pst->top == pst->capacity) {int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;// If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. STDataType* tmp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));if (tmp == NULL) {perror("relloc fail");return;}pst->a = tmp;pst->capacity = newCapacity;}pst->a[pst->top] = x;pst->top++;
}

Pop

void STPop(ST* pst) {assert(pst);assert(!STEmpty(pst));pst->top--;
}

Top

STDataType STTop(ST* pst) {assert(pst);assert(!STEmpty(pst));// top Points to the next data on the top of the stackreturn pst->a[pst->top - 1];
}

Empty

bool STEmpty(ST* pst) {assert(pst);return pst->top == 0;
}

Size

int STSize(ST* pst) {assert(pst);return pst->top;
}
http://www.dtcms.com/a/190338.html

相关文章:

  • ​Android学习总结之handler中源码解析和场景回答
  • 计算机操作系统(七)详细讲解进程的组成与特性,状态与转换
  • 可视化数据图表怎么做?如何实现三维数据可视化?
  • 技术中台-核心技术介绍(微服务、云原生、DevOps等)
  • Prometheus+Grafana+AlertManager完整安装过程
  • YOLO v2:目标检测领域的全面性进化
  • 网络防空总结 各种攻击
  • 光流 | Matlab工具中的光流算法
  • acwing 4275. Dijkstra序列
  • JVM学习专题(二)内存模型深度剖析
  • 尚硅谷阳哥JVM
  • upload-labs通关笔记-第5关 文件上传之.ini绕过
  • CSS Grid布局:从入门到实战
  • 【windows server脚本每天从网络盘复制到本地】
  • AI世界的崩塌:当人类思考枯竭引发数据生态链断裂
  • 数据安全与权限管控,如何实现双重保障?
  • Vue3学习(组合式API——计算属性computed详解)
  • Android学习总结之Glide自定义三级缓存(面试篇)
  • 关于 Golang GC 机制的一些细节:什么是根对象?GC 机制的触发时机?
  • “天神之眼”计算平台的算力设计(预计500-1000 TOPS)
  • 基于EFISH-SCB-RK3576/SAIL-RK3576的无人快递柜控制器技术方案
  • 【sql】按照数据的日期/天 ,对入库数据做数量分类
  • 驾驭数据洪流:大数据治理的全面解析与实战方案
  • ⭐️⭐️⭐️【课时6:如何创建工作流应用】学习总结 ⭐️⭐️⭐️ for《大模型Clouder认证:基于百炼平台构建智能体应用》认证
  • Git的安装和配置(idea中配置Git)
  • 当数控编程“联姻”AI:制造工厂的“智能大脑”如何炼成?
  • 全局优化搜索高次方程的解
  • ssh connect to remote gitlab without authority
  • 完整的 CentOS 6.10 虚拟机安装启动脚本
  • 【python爬虫】python+selenium实现Google Play Store应用信息爬虫+apk下载