• 【数据结构】栈(C语言实现)



    📙 作者简介 :RO-BERRY
    📗 学习方向:致力于C、C++、数据结构、TCP/IP、数据库等等一系列知识
    📒 日后方向 : 偏向于CPP开发以及大数据方向,欢迎各位关注,谢谢各位的支持


    请添加图片描述


    1.栈

    1.1栈的概念及结构

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

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

    出栈:栈的删除操作叫做出栈。出数据也在栈顶。
    这里有一张学长做的图片,我们借鉴来看一下
    在这里插入图片描述
    在这里插入图片描述


    1.2栈的实现

    栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。

    在这里插入图片描述
    在这里插入图片描述
    我们接下来将整个栈的函数体实现出来
    分为三个文件:

    1. Stack.h (函数头和函数体)
    2. Stack.c (功能具体实现)
    3. Test.c (功能测试)

    1.2.1 Stack.h

    #include
    #include
    #include
    #include
    
    // 下面是定长的静态栈的结构,实际中一般不实用,所以我们主要实现下面的支持动态增长的栈
    //typedef int STDataType;
    //#define N 10
    //typedef struct Stack
    //{
    //	STDataType a[N];
    //	int top; // 栈顶
    //}Stack;
    
    // 支持动态增长的栈
    typedef int STDataType;
    typedef struct Stack
    {
    	STDataType* a;
    	int top;
    	int capacity; 
    }ST;
    
    // 初始化栈
    void STInit(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 STDestroy(ST* ps);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    1.2.2 Stack.c

    初始化

    我们将队列中容量和个数都置为0,将存储数值的指针变量a置为空

    void STInit(ST* ps)
    {
    	assert(ps);
    	ps->a = NULL;
    	ps->capacity = 0;
    	ps->top = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    入栈
    void STPush(ST* ps, STDataType x)
    {
    	assert(ps);       //先断言判断是否为空
    	if (ps->top == ps->capacity)       //如果存储的值个数和容量相等
    	{
    		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2 ;     //容量为0则置为4,不为0则置为原容量的两倍
    		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);    //开辟新空间
    		if (tmp == NULL)
    		{
    			perror("realloc fail");
    			exit(-1);
    		}
    		ps->a = tmp;
    		ps->capacity = newCapacity;
    	}
    	ps->a[ps->top] = x;
    	ps->top++;   //数值增加了一个
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    出栈
    void STPop(ST* ps)
    {
    	assert(ps);
    	assert(ps->top > 0);
    	--ps->top;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    获取栈顶元素
    STDataType STTop(ST* ps)
    {
    	assert(ps);
    	assert(ps->top > 0);
    	return ps->a[ps->top - 1];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    获取栈中有效元素个数
    int STSize(ST* ps)
    {
    	assert(ps);
    	return ps->top;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    判空
    bool STEmpty(ST* ps)
    {
    	assert(ps);
    	return ps->top == 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    销毁
    void STDestroy(ST* ps)
    {
    	assert(ps);
    	free(ps->a);
    	ps->a = NULL;
    	ps->top = ps->capacity = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    1.3 源代码

    Stack.h

    #define _CRT_SECURE_NO_WARNINGS 1
    #include
    #include
    #include
    #include
    
     下面是定长的静态栈的结构,实际中一般不实用,所以我们主要实现下面的支持动态增长的栈
    //typedef int STDataType;
    //#define N 10
    //typedef struct Stack
    //{
    //	STDataType a[N];
    //	int top; // 栈顶
    //}Stack;
    
    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    Stack.c

    #define _CRT_SECURE_NO_WARNINGS 1
    #include"Stack.h"
    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);
    		}
    		ps->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];
    }
    int STSize(ST* ps)
    {
    	assert(ps);
    	return ps->top;
    }
    bool STEmpty(ST* ps)
    {
    	assert(ps);
    	return ps->top == 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    最后的Test可以自由发挥

  • 相关阅读:
    STL教程6-deque、stack、queue、list容器
    npm install报错 code:128
    编译器优化丨Cache优化
    解决docker运行elastic服务端启动不成功
    逻辑回归评分卡
    ubuntu16.04上安装gstreamer
    mac安装python 和 pip
    vue根据文字长短展示跑马灯效果
    C语言之数组练习题
    计算机视觉+人工智能面试笔试总结——卷积网络压缩面试题
  • 原文地址:https://blog.csdn.net/weixin_60521256/article/details/133889626