• 【C语言】栈的实现


    目录

    栈的概念及结构

    用代码实现栈

     定义栈的结构

    实现栈的基本功能

     栈的初始化

     进栈

    出栈 

     判空 

     获取栈顶元素

    得到栈的元素个数

    销毁栈 

    完整代码

    头文件

    源文件

    总结


     栈的概念及结构

    栈的概念:栈是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素,进行数据的插入和删除的一端称为栈顶,另一端称为栈底,栈中的数据元素遵循后进先出(LIFO)或者先进后出的原则。

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

    出栈:栈的删除操作叫做出栈,出数据也在栈顶

     栈的结构

    用代码实现栈

    要实现栈无非就两种结构:数组和链表。

    但是用数组来实现栈是更好的,因为栈是在栈顶进行插入删除的,而顺序表的尾插和尾删效率是非常高的,并且CPU高速缓存利用率也高。

     定义栈的结构

    1. typedef int STDataType;
    2. typedef struct Stack
    3. {
    4. STDataType* a;
    5. int top;
    6. int capacity;
    7. }ST;

    top是用来标识栈顶的位置,当然我这里是实现的动态栈,因为实现静态栈跟用链表来实现栈一样不太好。

    实现栈的基本功能

    1. //初始化栈
    2. void StackInit(ST* ps);
    3. //销毁栈
    4. void StackDestroy(ST* ps);
    5. //进栈
    6. void StackPush(ST* ps, STDataType x);
    7. //出栈
    8. void StackPop(ST* ps);
    9. //获取栈顶元素
    10. STDataType StackTop(ST* ps);
    11. //判空
    12. bool StackEmpty(ST* ps);
    13. //栈的元素个数
    14. int StackSize(ST* ps);

     栈的初始化

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

    这个就没什么好说的了。重点在后面 

     进栈

    首先要判断一下空间够不够,不够就扩容,原理和顺序表的一样。相信熟悉顺序表的你对这个应该是信手拈来。但是插入数据也有两种情况

    第一种

     先插入后加加

     第二种

    先加加后插入

     代码实现: 

    1. void StackPush(ST* ps, STDataType x)
    2. {
    3. assert(ps);
    4. if (ps->top == ps->capacity)
    5. {
    6. int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    7. STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType)* newcapacity);
    8. if (tmp == NULL)
    9. {
    10. printf("realloc fail\n");
    11. exit(-1);
    12. }
    13. ps->a = tmp;
    14. ps->capacity = newcapacity;
    15. }
    16. ps->a[ps->top] = x;
    17. ps->top++;
    18. }

    出栈 

    出栈就简单了,只需要将top--就好了,但是在此之前不要忘记判断一下栈是否为空。

    1. void StackPop(ST* ps)
    2. {
    3. assert(ps);
    4. assert(!StackEmpty(ps));
    5. ps->top--;
    6. }

     判空 

     只需要判断栈顶位置为不为0即可。

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

     获取栈顶元素

    我们是用数组实现栈,所以我们可以用下标访问栈顶元素。 可别忘了top-1才是栈顶元素的下标。 

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

    得到栈的元素个数

    top值就是栈的元素个数,所以只需返回top值就行。 

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

    销毁栈 

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

    完整代码

    头文件

    1. #pragma once
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <stdbool.h>
    5. #include <assert.h>
    6. typedef int STDataType;
    7. typedef struct Stack
    8. {
    9. STDataType* a;
    10. int top;
    11. int capacity;
    12. }ST;
    13. //初始化栈
    14. void StackInit(ST* ps);
    15. //销毁栈
    16. void StackDestroy(ST* ps);
    17. //进栈
    18. void StackPush(ST* ps, STDataType x);
    19. //出栈
    20. void StackPop(ST* ps);
    21. //获取栈顶元素
    22. STDataType StackTop(ST* ps);
    23. //判空
    24. bool StackEmpty(ST* ps);
    25. //栈的元素个数
    26. int StackSize(ST* ps);

    源文件

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include "stack.h"
    3. void StackInit(ST* ps)
    4. {
    5. assert(ps);
    6. ps->a = NULL;
    7. ps->top = 0;
    8. ps->capacity = 0;
    9. }
    10. void StackDestroy(ST* ps)
    11. {
    12. assert(ps);
    13. free(ps->a);
    14. ps->a = NULL;
    15. ps->top = ps->capacity = 0;
    16. }
    17. void StackPush(ST* ps, STDataType x)
    18. {
    19. assert(ps);
    20. if (ps->top == ps->capacity)
    21. {
    22. int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    23. STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType)* newcapacity);
    24. if (tmp == NULL)
    25. {
    26. printf("realloc fail\n");
    27. exit(-1);
    28. }
    29. ps->a = tmp;
    30. ps->capacity = newcapacity;
    31. }
    32. ps->a[ps->top] = x;
    33. ps->top++;
    34. }
    35. void StackPop(ST* ps)
    36. {
    37. assert(ps);
    38. assert(!StackEmpty(ps));
    39. ps->top--;
    40. }
    41. STDataType StackTop(ST* ps)
    42. {
    43. assert(ps);
    44. assert(!StackEmpty(ps));
    45. return ps->a[ps->top - 1];
    46. }
    47. bool StackEmpty(ST* ps)
    48. {
    49. assert(ps);
    50. return ps->top == 0;
    51. }
    52. int StackSize(ST* ps)
    53. {
    54. assert(ps);
    55. return ps->top;
    56. }

    总结:

    总体来说,学习了顺序表之后,栈的实现还是很简单的,无非就是在顺序表的基础上变了下形。

     觉得内容有用的话,就给博主三连吧!!!

  • 相关阅读:
    SQL—数据库查询语言,全面详解演示,入门进阶必会
    python 根据shp文件解析经纬度市县信息
    [libc-2.31 off_by_null] N0wayBack ezheap练习
    多线程【初阶-上篇】
    【php快速入门】学习笔记
    Python:if判断--综合案例练习:石头剪刀布
    vscode的红色箭头爆红和medule has no default export报错Vetur
    C语言——2.安装并使用VS
    Spring Cloud + Nacos 集成Netty Socket.IO
    Mongoose基本操作
  • 原文地址:https://blog.csdn.net/m0_62537611/article/details/125439866