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

栈的各种接口的实现(C)

栈的概念

  • 栈:
    一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
  • 压栈:
    栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
  • 出栈:
    栈的删除操作叫做出栈。出数据也在栈顶。
    ![[Pasted image 20240919140145.png]]

栈的实现

栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
用链表实现栈,栈顶在头节点,出栈和入栈用头删和头插
![[Pasted image 20240920222337.png]]

栈的定义
typedef int STDataType;
typedef struct Stack
{STDataType* a;int top;int capacity;
}ST;
  • 创建Stack结构体,重命名为ST,方便后续代码的书写
  • 重命名int为STDataType,方便后续对数据类型的修改
  • a是一个指针,用来表示数组
  • capacity表示栈的容量
初始化

top初始化为-1,意味着top是栈顶元素
初始化为0,意味着top是栈顶元素的下一个位置

void STInit(ST* ps)
{assert(ps);ps->a = NULL;ps->capacity = 0;ps->top = 0;
}

![[Pasted image 20240920224845.png]]

销毁
void STDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->top = ps->capacity = 0;
}
  • 判断ps是否为空
  • 释放空间
  • 指针置空
  • top和capacity也赋给0
入栈
void STPush(ST* ps, STDataType x)
{assert(ps);if (ps->top == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);if (tmp == NULL){perror ("realloc fail");exit(-1);}pa->a = tmp;ps->capacity = newCapacity;}ps->a[ps->top] = x;ps->top++;
}

realloc如果第一个指针是空指针,效果和malloc一样

  • assert判断ps是否为空
    如果top等于capacity,进行扩容
  • 定义新容量,如果是0,就赋给4,否则乘以2倍
  • realloc新空间赋给一个新节点tmp,判断创建空间是否成功
  • 将tmp赋给a,新容量赋给capacity
出栈
void STPop(ST* ps)
{assert(ps);assert(ps->top > 0);--ps->top;
}
  • 判断ps是否为空
  • 判断栈是否为空
  • 将top–
获取栈顶元素
STDataType STTop(ST* ps)
{assert(ps);assert(ps->top > 0);return ps->a[ps->top - 1];
}
  • 判断ps是否为空
  • 判断栈是否为空
  • 由于top初始化为0,top表示栈顶元素的下一个位置,top-1就能获取栈顶元素
返回个数
int STSize(ST* ps)
{assert(ps);return ps->top;
}
  • 判断ps是否为空
  • 直接返回top
判断是否为空
bool STEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}
  • 判断ps是否为空
  • 返回判断top是否等于0,等于0返回true,不等于0返回false

栈声明定义分离实现

#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* ps);
void STDestroy(ST* ps);
void STPush(ST* ps, STDataType x);
void STPop(ST* ps);
STDataType STTop(ST* ps);
int STSize(ST* ps);
bool STEmpty(ST* ps);
void STInit(ST* ps)
{assert(ps);ps->a = NULL;ps->capacity = 0;ps->top = 0;
}void STDestroy(ST* ps)
{assert(ps);free(ps->a);ps->a = NULL;ps->top = ps->capacity = 0;
}void STPush(ST* ps, STDataType x)
{assert(ps);if (ps->top == ps->capacity){int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);if (tmp == NULL){perror ("realloc fail");exit(-1);}pa->a = tmp;ps->capacity = newCapacity;}ps->a[ps->top] = x;ps->top++;
}void STPop(ST* ps)
{assert(ps);assert(ps->top > 0);--ps->top;
}STDataType STTop(ST* ps)
{assert(ps);assert(ps->top > 0);return ps->a[ps->top - 1];
}bool STEmpty(ST* ps)
{assert(ps);return ps->top == 0;
}

http://www.mrgr.cn/news/31350.html

相关文章:

  • 《史上最简单的 SpringCloud 教程》
  • k8s自动清理pod脚本分享
  • 认识自动化测试
  • 数据结构之二叉树(1)
  • Shader 中的光源
  • 排序-----选择排序
  • 机器人上的DPDK使用思考
  • 对商品分类系统的若干问题的思考
  • Go语言基础学习01
  • OpenHarmony(鸿蒙南向开发)——小型系统内核(LiteOS-A)【扩展组件】上
  • C#开发记录如何建立虚拟串口,进行串口通信,以及通信模板
  • DOS(Disk Operating System,磁盘操作系统)常用指令
  • Java知识点小结3:内存回收
  • C++自动寻径算法
  • 网关登录校验(2)----网关如何将用户信息传递给微服务
  • Django+React+Neo4j实现的地质领域知识图谱系统
  • DNS解析流程
  • pandas入门
  • day51
  • df将字典转换为df,如何以key为行而不是列