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

栈的各种接口的实现(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.kler.cn/news/312624.html

相关文章:

  • 软设9.20
  • 828华为云征文|基于华为云Flexus X实例快速搭建Halo博客平台
  • 后台数据管理系统 - 项目架构设计-Vue3+axios+Element-plus(0916)
  • 基于SSM+Vue+MySQL的家教服务管理系统
  • Spring Boot中使用注解拦截器实现通用校验器和基于角色的权限注解
  • CSS 笔记 1
  • 小琳AI课堂:MASS模型——革新自然语言处理的预训练技术
  • celery
  • 鸿蒙生态应用
  • 网关登录校验(2)----网关如何将用户信息传递给微服务
  • 金钥匙系列:Kubernetes (K8s) 服务集群技术栈学习路线
  • 表单里面input的type属性值有哪些?
  • CCF201909_1
  • docker发布redis容器
  • 数据库-约束与多表查询
  • uniapp快速入门教程,内容来源于官方文档,仅仅记录快速入门需要了解到的知识点
  • Java之封装
  • QT --- 初识QT
  • 简单了解 JVM
  • Mac使用技巧-来自苹果专人在线辅导服务2
  • windows环境下载ubuntu22.04源码,提示invalid path aux.c
  • [Visual Stuidio 2022使用技巧]2.配置及常用快捷键
  • LeetCode746:使用花费最小爬楼梯
  • Vue: watch5种监听情况
  • Kubernetes故障排除全面指南
  • Day27_0.1基础学习MATLAB学习小技巧总结(27)——曲线拟合函数
  • Unsupervised Domain Adaptation by Backpropagation
  • 【每日刷题】Day127
  • Scrapy爬虫框架 Spider Middleware 爬虫页中间件
  • 【软件设计】常用设计模式--观察者模式