• 数据结构 | 栈的实现


    数据结构 | 栈的实现

    栈的概念及结构

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

    在这里插入图片描述


    在这里插入图片描述

    栈的实现

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

    在这里插入图片描述


    在这里插入图片描述


    Stack.h

    #pragma once
    
    #include
    #include
    #include
    #include
    
    typedef int STDataType;
    
    typedef struct Stack
    {
    	STDataType* a;
    	int top;
    	int capacity;
    }ST;
    
    // 初始化栈
    void StackInit(ST* ps);
    // 入栈
    void StackPush(ST* ps, STDataType x);
    // 出栈
    void StackPop(ST* ps);
    // 获取栈顶元素
    STDataType StackTop(ST* ps);
    // 获取栈中有效元素个数
    int StackSize(ST* ps);
    // 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
    bool StackEmpty(ST* ps);
    // 销毁栈
    void StackDestroy(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

    初始化栈

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

    入栈

    void StackPush(ST* ps, STDataType x)
    {
    	assert(ps);
    	if (ps->capacity == ps->top)
    	{
    		STDataType newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
    		if (tmp == NULL)
    		{
    			perror("relloc fail!\n");
    			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 StackPop(ST* ps)
    {
    	assert(ps);
    	assert(ps->top > 0);
    	ps->top--;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    获取栈顶元素

    STDataType StackTop(ST* ps)
    {
    	assert(ps);
    	assert(ps->top > 0);
    	return ps->a[ps->top - 1];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    获取栈中有效元素个数

    int StackSize(ST* ps)
    {
    	assert(ps);
    	return ps->top;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    检测栈是否为空

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

    销毁栈

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

    Stack.c

    #define _CRT_SECURE_NO_WARNINGS 1
    
    #include"Stack.h"
    
    // 初始化栈
    void StackInit(ST* ps)
    {
    	assert(ps);
    	ps->a = NULL;
    	ps->capacity = 0;
    	//top 表示指向栈顶元素
    	//ps->top = -1;
    	//top 表示指向栈顶元素的下一个
    	ps->top = 0;
    }
    // 入栈
    void StackPush(ST* ps, STDataType x)
    {
    	assert(ps);
    	if (ps->capacity == ps->top)
    	{
    		STDataType newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
    		if (tmp == NULL)
    		{
    			perror("relloc fail!\n");
    			exit(-1);
    		}
    		ps->a = tmp;
    		ps->capacity = newcapacity;
    	}
    	ps->a[ps->top] = x;
    	ps->top++;
    }
    // 出栈
    void StackPop(ST* ps)
    {
    	assert(ps);
    	assert(ps->top > 0);
    	ps->top--;
    }
    // 获取栈顶元素
    STDataType StackTop(ST* ps)
    {
    	assert(ps);
    	assert(ps->top > 0);
    	return ps->a[ps->top - 1];
    }
    // 获取栈中有效元素个数
    int StackSize(ST* ps)
    {
    	assert(ps);
    	return ps->top;
    }
    // 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
    bool StackEmpty(ST* ps)
    {
    	assert(ps);
    	return ps->top == 0;
    }
    // 销毁栈
    void StackDestroy(ST* ps)
    {
    	assert(ps);
    	ps->a = NULL;
    	ps->capacity = 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
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    好了,栈的实现就到这里结束了,有用的话点个赞吧~~

  • 相关阅读:
    .NET Core 实现Excel的导入导出
    Grpc MagicOnion库 之 客户端和服务端 (案例版)
    shiro反序列化漏洞复现(CVE-2016-4437)
    零信任的基本概念【新航海】
    十八、图像像素类型转换和归一化操作
    [Linux打怪升级之路]-冯诺依曼体系结构和对操作系统的认识
    【数据结构与算法】堆&&堆排序(堆是一种数据结构).
    MySQL数据库增删改查
    【GlobalMapper精品教程】023:Excel数据通过相同字段连接到属性表中(气温降水连接到气象台站)
    远程连接centos 7 图形化桌面
  • 原文地址:https://blog.csdn.net/2201_76004325/article/details/134365644