当前位置: 首页 > 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.xdnf.cn/news/450523.html

相关文章:

  • 服务器数据恢复—XFS文件系统分区消失的数据恢复案例
  • 基于.Net开发的网络管理与监控工具
  • 【算法】版本号排序
  • C++笔记-AVL树(包括单旋和双旋等)
  • 微信小程序学习之轮播图swiper
  • DeepSeek:AI助力高效工作与智能管理
  • Qwen3如何强化推理能力?
  • AISBench benchmark评测工具实操-精度评测场景-采用命令行指定模型和数据集的方式
  • ESP系列单片机选择指南:结合实际场景的最优选择方案
  • Jmeter 安装包与界面汉化
  • 【大模型】LLM概念相关问题(中)
  • day014-服务管理
  • Python机器学习笔记(二十二、模型评估-交叉验证)
  • 润金店发布“爱有千斤重“30周年限定爱意礼盒:以东方美学诠释爱的重量
  • elementplus el-tree 二次封装支持配置删除后展示展开或折叠编辑复选框懒加载功能
  • js对象原型,原型链
  • 制作一款打飞机游戏48:敌人转向
  • 嵌入式学习笔记 D20 :单向链表的基本操作
  • 3DMAX脚本病毒Spy CA查杀方法
  • 计算机网络笔记(二十八)——4.10软件定义网络SDN简介
  • 【0415】Postgres内核 释放指定 memory context 中所有内存 ④
  • 5.14 BGP作业
  • Linux操作系统实战:进程创建的底层原理(转)
  • 朱老师, 3518e系列,第三季
  • 【Python】杂乱-[代码]Python 替换字符串中相关字符的方法
  • 容器安全-核心概述
  • OpenCV人脸识别LBPH算法原理、案例解析
  • Codeforces Round 1003 (Div. 4)
  • 分布式一致性协议Raft
  • 动物乐园-第16届蓝桥第5次STEMA测评Scratch真题第5题