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

数据结构-栈的实现

一、栈的概念和结构

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

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

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

二、栈的实现

2.1 栈的结构

// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;		// 栈顶int capacity;  // 容量 
}Stack;

2.2 栈的初始化StackInit

// 初始化栈 
void StackInit(Stack* ps) {assert(ps);ps->a = NULL;ps->capacity = ps->top = 0;
}

2.3 入栈StackPush

void CheckCapacity(Stack* ps) {if (ps->capacity == ps->top) {int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));if (tmp == NULL) {perror("CheckCapacity()::realloc");exit(1);}ps->a = tmp;ps->capacity = newcapacity;}
}
// 入栈 
void StackPush(Stack* ps, STDataType data) {assert(ps);CheckCapacity(ps);ps->a[ps->top] = data;ps->top++;
}

2.4 出栈StackPop

// 出栈 
void StackPop(Stack* ps) {assert(ps);assert(ps->top > 0);ps->top--;
}

2.5 取栈顶元素StackTop

// 获取栈顶元素 
STDataType StackTop(Stack* ps) {assert(ps);assert(ps->top > 0);return ps->a[ps->top - 1];
}

2.6 判空StackEmpty

// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps) {return ps->top == 0 ? 0 : 1;
}

2.7 获取栈中元素个数StackSize

// 获取栈中有效元素个数 
int StackSize(Stack* ps) {return ps->top;
}

2.8 销毁StackDestroy

// 销毁栈 
void StackDestroy(Stack* ps) {assert(ps);ps->capacity = ps->capacity = 0;free(ps->a);ps->a = NULL;
}

三、栈的应用

括号匹配问题:20. 有效的括号 - 力扣(LeetCode)

四、代码

4.1 Stack.h

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;		// 栈顶int capacity;  // 容量 
}Stack;
// 初始化栈 
void StackInit(Stack* ps);
// 入栈 
void StackPush(Stack* ps, STDataType data);
// 出栈 
void StackPop(Stack* ps);
// 获取栈顶元素 
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数 
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps);
// 销毁栈 
void StackDestroy(Stack* ps);

4.2 Stack.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
// 初始化栈 
void StackInit(Stack* ps) {assert(ps);ps->a = NULL;ps->capacity = ps->top = 0;
}
// 销毁栈 
void StackDestroy(Stack* ps) {assert(ps);ps->capacity = ps->capacity = 0;free(ps->a);ps->a = NULL;
}
void CheckCapacity(Stack* ps) {if (ps->capacity == ps->top) {int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));if (tmp == NULL) {perror("CheckCapacity()::realloc");exit(1);}ps->a = tmp;ps->capacity = newcapacity;}
}
// 入栈 
void StackPush(Stack* ps, STDataType data) {assert(ps);CheckCapacity(ps);ps->a[ps->top] = data;ps->top++;
}
// 出栈 
void StackPop(Stack* ps) {assert(ps);assert(ps->top > 0);ps->top--;
}
// 获取栈顶元素 
STDataType StackTop(Stack* ps) {assert(ps);assert(ps->top > 0);return ps->a[ps->top - 1];
}
// 获取栈中有效元素个数 
int StackSize(Stack* ps) {return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0 
int StackEmpty(Stack* ps) {return ps->top == 0 ? 0 : 1;
}

4.3 test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void test01()
{Stack st = { 0 };StackInit(&st);StackPush(&st, 1);//测试入栈StackPush(&st, 2);//测试入栈StackPush(&st, 3);//测试入栈StackPush(&st, 4);//测试入栈StackPush(&st, 5);//测试入栈int count = StackSize(&st);//测试获取栈中有效元素个数printf("%d\n", count);while (StackEmpty(&st)) {//检测栈是否为空,如果为空返回非零结果,如果不为空返回0printf("%d ", StackTop(&st));//测试取栈顶元素StackPop(&st);//测试出栈}StackDestroy(&st);}int main() {test01();//测试return 0;
}

http://www.dtcms.com/a/265650.html

相关文章:

  • 电动车信用免押小程序免押租赁小程序php方案
  • 数据库运维手册指导书
  • 移动端Html5播放器按钮变小的问题解决方法
  • Laravel8中使用phpword生成word文档
  • LeetCode--40.组合总和II
  • 【ArcGIS Pro】属性表咋不能编辑了?
  • wvp-GB28181-pro 项目 ZLMediaKit 部署 (Centos7)
  • XILINX Ultrascale+ Kintex系列FPGA的架构
  • R语言开发记录,二(创建R包)
  • vue-37(模拟依赖项进行隔离测试)
  • 《导引系统原理》-西北工业大学-周军-“2️⃣导引头的角度稳定系统”
  • 定时点击二次鼠标 定时点击鼠标
  • Node.js中exports与module.exports区别
  • DPDK开发环境配置
  • SpringCloud系列(49)--SpringCloud Stream消息驱动之实现生产者
  • 《Spring 中上下文传递的那些事儿》 Part 1:ThreadLocal、MDC、TTL 原理与实践
  • 使用 Docker Swarm 部署高可用集群指南
  • 副作用是什么?
  • DQL-3-聚合函数
  • lspci查看PCI设备详细信息
  • linux常用命令(10):scp命令(远程拷贝命令,复制文件到远程服务器)
  • PlatformIO 在使用 GitHub 上的第三方库
  • Spark 4.0的VariantType 类型以及内部存储
  • 云上堡垒:如何用AWS原生服务构筑坚不可摧的主机安全体系
  • java教程——初识guava(2)
  • 在 React 中使用 WebSockets 构建实时聊天应用程序
  • 实训项目记录 | 7.3
  • AI会取代网络工程师吗?理解AI在网络安全中的角色
  • 【网络安全】Webshell命令执行失败解决思路
  • 如何避免服务器出现故障情况?