当前位置:   article > 正文

数据结构之栈详解

数据结构之栈详解

1. 栈的概念以及结构

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

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

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

2.栈的功能以及实现

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

这里定长的静态栈的结构,实现中一般不实用

  1. //静态栈
  2. typedef int STDataType;
  3. #define N 10
  4. typedef struct Stack
  5. {
  6. STDataType a[N];
  7. int top;
  8. }Stack;

所以我们主要实现下面的支持动态增长的栈

  1. //支持动态增长的栈
  2. typedef int STDataType;
  3. typedef struct Stack
  4. {
  5. STDataType* a;
  6. int top; //栈顶
  7. int capacity; //容量
  8. }Stack;

栈所需要实现的一些功能

  1. //栈初始化
  2. void STInit(ST* ps);
  3. //清空栈
  4. void STDestroy(ST* ps);
  5. //插入栈
  6. void STPush(ST* ps);
  7. //删除栈元素
  8. void STPop(ST* ps);
  9. //查看栈的大小
  10. int STSize(ST* ps);
  11. //查看栈中有没有元素
  12. bool STEmpty(ST* ps);
  13. //输出栈顶元素
  14. STDataType STTop(ST* ps);

->1. 栈初始化

  1. void STInit(ST* ps)
  2. {
  3. assert(ps);
  4. ps->a = (ST*)malloc(sizeof(ST) * CAPACITY__SIZE);
  5. if (NULL == ps->a)
  6. {
  7. perror("STInit::malloc");
  8. return;
  9. }
  10. //压栈元素的下一个位置
  11. ps->top = 0;
  12. ps->capacity = CAPACITY__SIZE;
  13. }

->2. 清空栈

  1. //清空栈
  2. void STDestroy(ST* ps)
  3. {
  4. free(ps->a);
  5. ps->a = NULL;
  6. ps->top = 0;
  7. ps->capacity = 0;
  8. }

->3. 插入栈

  1. //插入栈
  2. void STPush(ST* ps, STDataType x)
  3. {
  4. assert(ps);
  5. if (ps->top == ps->capacity)
  6. {
  7. ST* Expand = (ST*)realloc(ps->a,sizeof(ST) * ps->capacity * CAPACITY__SIZE);
  8. if (NULL == Expand)
  9. {
  10. perror("STPop::malloc");
  11. exit(-1);
  12. }
  13. ps->a = Expand;
  14. ps->capacity *= CAPACITY__SIZE;
  15. }
  16. ps->a[ps->top++] = x;
  17. }

->4. 删除栈元素

  1. //删除栈元素
  2. void STPop(ST* ps)
  3. {
  4. assert(ps);
  5. assert(!STEmpty);
  6. ps->top--;
  7. }

->5. 查看栈的大小

  1. //查看栈的大小
  2. int STSize(ST* ps)
  3. {
  4. return ps->top;
  5. }

->6. 查看栈中有没有元素

  1. //查看栈中有没有元素
  2. bool STEmpty(ST* ps)
  3. {
  4. return ps->top == 0;
  5. }

->7.输出栈顶元素

  1. //输出栈顶元素
  2. STDataType STTop(ST* ps)
  3. {
  4. assert(ps);
  5. assert(!STEmpty);
  6. return ps->a[ps->top - 1];
  7. }

整合以上我们来实现栈,下面是实现栈的完整代码

Stack.h

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #include <stdbool.h>
  6. //动态栈
  7. #define CAPACITY__SIZE 4
  8. typedef int STDataType;
  9. typedef struct Stack
  10. {
  11. int* a;
  12. int top;
  13. int capacity;
  14. }ST;
  15. //栈初始化
  16. void STInit(ST* ps);
  17. //清空栈
  18. void STDestroy(ST* ps);
  19. //插入栈
  20. void STPush(ST* ps, STDataType x);
  21. //删除栈元素
  22. void STPop(ST* ps);
  23. //查看栈的大小
  24. int STSize(ST* ps);
  25. //查看栈中有没有元素
  26. bool STEmpty(ST* ps);
  27. //输出栈顶元素
  28. STDataType STTop(ST* ps);

Stack.c

  1. #include "Stack.h"
  2. void STInit(ST* ps)
  3. {
  4. assert(ps);
  5. ps->a = (STDataType*)malloc(sizeof(STDataType) * CAPACITY__SIZE);
  6. if (NULL == ps->a)
  7. {
  8. perror("STInit::malloc");
  9. return;
  10. }
  11. //压栈元素的下一个位置
  12. ps->top = 0;
  13. ps->capacity = CAPACITY__SIZE;
  14. }
  15. //清空栈
  16. void STDestroy(ST* ps)
  17. {
  18. free(ps->a);
  19. ps->a = NULL;
  20. ps->top = 0;
  21. ps->capacity = 0;
  22. }
  23. //插入栈
  24. void STPush(ST* ps, STDataType x)
  25. {
  26. assert(ps);
  27. if (ps->top == ps->capacity)
  28. {
  29. STDataType* Expand = (STDataType*)realloc(ps->a,sizeof(STDataType) * ps->capacity * CAPACITY__SIZE);
  30. if (NULL == Expand)
  31. {
  32. perror("STPop::malloc");
  33. exit(-1);
  34. }
  35. ps->a = Expand;
  36. ps->capacity *= CAPACITY__SIZE;
  37. }
  38. ps->a[ps->top++] = x;
  39. }
  40. //删除栈元素
  41. void STPop(ST* ps)
  42. {
  43. assert(ps);
  44. assert(!STEmpty(ps));
  45. ps->top--;
  46. }
  47. //查看栈的大小
  48. int STSize(ST* ps)
  49. {
  50. return ps->top;
  51. }
  52. //查看栈中有没有元素
  53. bool STEmpty(ST* ps)
  54. {
  55. return ps->top == 0;
  56. }
  57. //输出栈顶元素
  58. STDataType STTop(ST* ps)
  59. {
  60. assert(ps);
  61. assert(!STEmpty(ps));
  62. return ps->a[ps->top - 1];
  63. }

test.c

  1. #include "Stack.h"
  2. int main()
  3. {
  4. ST Stack;
  5. STInit(&Stack);
  6. STPush(&Stack, 1);
  7. STPush(&Stack, 2);
  8. STPush(&Stack, 3);
  9. STPush(&Stack, 4);
  10. STPush(&Stack, 5);
  11. /*while (!STEmpty(&Stack))
  12. {
  13. printf("%d->", Stack.a[--Stack.top]);
  14. }
  15. printf("NULL\n");*/
  16. while (!STEmpty(&Stack))
  17. {
  18. printf("%d ", STTop(&Stack));
  19. STPop(&Stack);
  20. }
  21. printf("\n");
  22. STDestroy(&Stack);
  23. return 0;
  24. }

测试结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/877305
推荐阅读
相关标签
  

闽ICP备14008679号