数据结构数组栈的实现

news/2024/10/18 12:26:49/

在这里插入图片描述

Hello,今天我们来实现一下数组栈,学完这个我们又更进一步了。

一、栈

栈的概念

栈是一种特殊的线性表,它只允许在固定的一端进行插入和删除元素的操作。
进行数据的插入和删除只在栈顶实现,另一端就是栈底。
栈的元素是后进先出。

压栈:栈的数据进入就是压栈
出栈:栈的数据删除就叫出栈

我们画一个原理图让大家比较好理解一下。

在这里插入图片描述

这一过程叫做pop出栈

我们上述的过程都是在栈顶实现出栈入栈,并不能像顺序表和单链表那样从任意位置删除和增加,但这就是栈的性质,我们后面会讲它的作用。

实现栈我们可以用链表产生节点的方式链接他们,但是也可以用数组下标访问的方式,类似顺序表这样的方法
那这两个方法哪个好呢,我们来比较一下。

因为栈的性质我们不得从栈顶出栈和入栈,如果我们实现的时候是链表的方式,那必然会存在一个问题,就是我们的时间复杂度是O(N)我们需要遍历一遍数组,这样的话栈好像变得“土”,所以用数组的方式更快的提高效率,

二、栈的定义

typedef int StackDataType;
#define N 100
struct Stack
{StackDataType arry[N];StackDataType top;
};

这是静态栈,在顺序表的时候我们就讲过静态栈存在缺点,最大的缺点就是不能开辟空间,100个最多只能存100个数据,如果我只使用10个int空间就存在浪费了,如果我要存储1000个数据,我们的空间又不够了,这就会造成一系列问题,所以我们改一下,变成动态栈,我们来实现一下吧。

typedef int StackDataType;
typedef struct Stack
{StackDataType* arry;int top;int capacity;
}Stack;

有了结构体还是老样子,我们来实现一下接口函数,开整!
初始化栈

void StackInit(Stack* pst)
{assert(pst);pst->arry = NULL;pst->capacity = pst->top = 0;
}

初始化栈这个大家肯定会了。

销毁


void StackDestory(Stack* pst)
{assert(pst);free(pst->arry);pst->capacity = pst->top = 0;}

判断栈是否为空

bool StackEmpty(Stack* pst)
{assert(pst);return pst->top == 0;
}

现在我们要实现一个入栈的方法,入栈的时候我们需要检查一下我们的内存空间是不是满了,和顺序表一样的道理,如果满了我们就需要扩容。所以在入栈的时候需要判断一下它空间有没有满。

void StackPush(Stack* pst, StackDataType x)
{assert(pst);if (pst->capacity == pst->top){int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;StackDataType* tmp = (StackDataType*)realloc(pst->arry, sizeof(int) * newcapacity);if (tmp == NULL){printf("realloc fail\n");exit(-1);}pst->arry = tmp;pst->capacity = newcapacity;}pst->arry[pst->top - 1] = x;pst->top++;}

这和我们顺序表的尾插一摸一样,现在看大家肯定觉得简单很多了。
有了入栈,那就有出栈。

void StackPop(Stack* pst)
{assert(pst);if (pst->top > 0){pst->top--;}
}

因为我们上面写了一个判断该数是不是为空我们也可以写成

void StackPop(Stack* pst)
{assert(pst);if (!StackEmpty(pst)){pst->top--;}
}

返回栈顶数据

StackDataType StackTop(Stack* pst)
{assert(pst);return pst->arry[pst->top - 1];
}

统计栈里有多少数

int StackSize(Stack* pst)
{assert(pst);return pst->top;
}

完整代码

#pragma once#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
//typedef int StackDataType;
//#define N 100
//struct Stack
//{
//	StackDataType arry[N];
//	StackDataType top;
//};typedef int StackDataType;
typedef struct Stack
{StackDataType* arry;int top;int capacity;
}Stack;void StackInit(Stack* pst);void StackDestory(Stack* pst);bool StackEmpty(Stack* pst);void StackPush(Stack* pst, StackDataType x);void StackPop(Stack* pst);StackDataType StackTop(Stack* pst);int StackSize(Stack* pst);

#include"Stack.h"void StackInit(Stack* pst)
{assert(pst);pst->arry = NULL;pst->capacity = pst->top = 0;
}void StackDestory(Stack* pst)
{assert(pst);free(pst->arry);pst->capacity = pst->top = 0;}bool StackEmpty(Stack* pst)
{assert(pst);return pst->top == 0;
}void StackPush(Stack* pst, StackDataType x)
{assert(pst);if (pst->capacity == pst->top){int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;StackDataType* tmp = (StackDataType*)realloc(pst->arry, sizeof(int) * newcapacity);if (tmp == NULL){printf("realloc fail\n");exit(-1);}pst->arry = tmp;pst->capacity = newcapacity;}pst->arry[pst->top - 1] = x;pst->top++;}void StackPop(Stack* pst)
{assert(pst);if (pst->top > 0){pst->top--;}
}void StackPop(Stack* pst)
{assert(pst);if (!StackEmpty(pst)){pst->top--;}
}StackDataType StackTop(Stack* pst)
{assert(pst);return pst->arry[pst->top - 1];
}int StackSize(Stack* pst)
{assert(pst);return pst->top;
}

栈的应用也有很多,后面会分享给大家,我们下次再见


http://www.ppmy.cn/news/1063027.html

相关文章

用AI + Milvus Cloud搭建着装搭配推荐系统

在上一篇文章中,我们学习了如何利用人工智能技术(例如开源 AI 向量数据库 Milvus Cloud 和 Hugging Face 模型)寻找与自己穿搭风格相似的明星。在这篇文章中,我们将进一步介绍如何通过对上篇文章中的项目代码稍作修改,获得更详细和准确的结果,文末附赠彩蛋。 注:试用此…

Linux内核学习(九)—— 虚拟文件系统(基于Linux 2.6内核)

虚拟文件系统&#xff08;VFS&#xff09;作为内核子系统&#xff0c;为用户空间程序提供了文件和文件系统相关的接口。通过虚拟文件系统&#xff0c;程序可以利用标准的 Unix 系统调用对不同的文件系统&#xff08;甚至不同介质上的文件系统&#xff09;进行读写操作。 一、通…

2023SpringMvc和spring之八股文——面试题

概述 什么是Spring MVC&#xff1f;简单介绍下你对Spring MVC的理解&#xff1f; Spring MVC是一个基于Java的实现了MVC设计模式的请求驱动类型的轻量级 Web框架&#xff0c;通过把模型-视图-控制器分离&#xff0c;将web层进行职责解耦&#xff0c;把复杂的 web应用分成逻辑…

数据结构例题代码及其讲解-链表

链表 单链表的结构体定义及其初始化。 typedef struct LNode {int data;struct LNode* next; }LNode, *LinkList;①强调结点 LNode *p; ②强调链表 LinkList p; //初始化 LNode* initList() {//定义头结点LNode* L (LNode*)malloc(sizeof(LNode));L->next NULL;return …

iPhone 15 Pro与谷歌Pixel 7 Pro:哪款相机手机更好?

考虑到苹果最近将更多高级功能转移到iPhone Pro设备上的趋势,今年秋天iPhone 15 Pro与谷歌Pixel 7 Pro的对决将是一场特别有趣的对决。去年发布的iPhone 14 Pro确实发生了这种情况,有传言称iPhone 15 Pro再次受到了苹果的大部分关注。 预计iPhone 15系列会有一些变化,例如切…

Vue--进度条

挺有意思的&#xff0c;大家可以玩一玩儿&#xff1a; 前端代码如下&#xff1a;可以直接运行的代码。 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content&qu…

PROFIBUS主站转MODBUS TCP网关

1.产品功能 YC-DPM-TCP网关在Profibus总线侧实现主站功能&#xff0c;在以太网侧实现ModbusTcp服务器功能。可将Profibus DP从站接入到ModbusTcp网络&#xff1b;通过增加DP/PA耦合器&#xff0c;也可将Profibus PA从站接入ModbusTcp网络。YC-DPM-TCP网关最多支持125个Profibu…

CPU架构和指令集

文章目录 一 、CPU架构二、操作系统和CPU三、CPU的位数和字长 一 、CPU架构 不同的CPU架构通常使用不同的指令集。每种CPU架构都有其自己的一组特定的机器指令&#xff0c;这些指令用于执行计算机程序。不同的CPU架构之间的指令集是不兼容的&#xff0c;这意味着编写的程序通常…