当前位置:   article > 正文

DS:顺序栈的实现_顺序栈的实现代码printf

顺序栈的实现代码printf

                                                    创作不易,友友们给个三连吧!!

一、栈的概念及结构

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

二、顺序栈的实现

数组实现栈:

首元素当栈低,栈顶是数组的尾元素,压栈就是尾插,出栈就是尾删

链表实现栈:

链表的最后一个结点当栈底,栈顶是链表的头结点,压栈就是头插,出栈就是头删

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

由于这些操作和顺序表的实现基本上是一样的,所以以下的介绍不做详细讲解。

建议大家看看博主关于顺序表的实现,再来看下面代码就易如反掌了!!

DS:顺序表的实现-CSDN博客

2.1 栈相关结构体

下面是定长的静态栈的结构,实际中一般不实用,因为设置得太小容易不够,设置得太大容易浪费

  1. typedef int STDataType;
  2. #define N 10
  3. typedef struct Stack
  4. {
  5.  STDataType _a[N];
  6.  int _top; // 栈顶
  7. }Stack;

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

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

2.2 初始化栈

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

     ps->top并不指向栈顶元素,而是指向栈顶元素的下一个位置,如果想要指向栈顶元素,则需要给top赋值-1.但是给top赋值0也有好处,就是top的值就相当于是顺序表中的size,即表示栈中的有效数据个数

2.3 压栈

  1. void StackPush(Stack* ps, STDataType x)
  2. {
  3. assert(ps);
  4. //判断是否需要扩容
  5. if (ps->top == ps->capacity)
  6. {
  7. int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
  8. STDataType* temp = (STDataType*)realloc(ps->a,sizeof(STDataType) * newcapacity);
  9. if (temp == NULL)
  10. {
  11. perror("realloc fail");
  12. exit(1);
  13. }
  14. ps->a = temp;
  15. ps->capacity = newcapacity;
  16. }
  17. //压栈
  18. ps->a[ps->top++] = x;
  19. }

2.4 出栈

  1. void StackPop(Stack* ps)
  2. {
  3. assert(ps);
  4. //如果栈为空,则没有删除的必要
  5. assert(!StackEmpty(ps));
  6. ps->top--;
  7. }

2.5 获取栈顶元素

  1. STDataType StackTop(Stack* ps)
  2. {
  3. assert(ps);
  4. //如果栈为空,不可能有栈顶元素
  5. assert(!StackEmpty(ps));
  6. return ps->a[ps->top - 1];
  7. }

要注意:因为我们初始化的top是0,所以top指向的是栈顶元素的下一个位置! 

2.6 获取栈中有效元素个数

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

因为top初始赋值为0, 所以top其实就相当于栈中的有效数据个数,专门封装一个函数只是想提高可读性!

2.7 检测栈是否为空

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

      在顺序表中,是否为空只需要看有效容量个数是不是0即可,但是在顺序栈中有效数据个数size被替换成了 top,虽然我们知道top和size的意思差不多,但是如果在代码里直接用的话可读性就没有size这么好,所以单独设置一个检测栈是否为空的函数。

2.8 销毁栈

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

2.9 打印栈 

     栈相比较于顺序表,并不具备随机访问的特点,因为栈是后进先出的,也就是说如果我们要遍历栈去访问栈中的每个元素,那么就需要一边获取栈顶元素一边出栈,这其实就会破坏原先栈的结构了,一般只能使用一次,不具备复用性,因此没必要单独封装一个函数。如果实在想打印栈,那么就在main函数中这样测试一下

  1. #include"Stack.h"
  2. int main()
  3. {
  4. Stack sk;
  5. StackInit(&sk);
  6. StackPush(&sk, 1);
  7. StackPush(&sk, 2);
  8. StackPush(&sk, 3);
  9. StackPush(&sk, 4);
  10. while (!StackEmpty(&sk))
  11. {
  12. printf("%d ", StackTop(&sk));//一边打印栈顶元素
  13. StackPop(&sk);//一边出栈
  14. }
  15. }

三、顺序栈实现的所有代码 

3.1 Stack.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<stdbool.h>
  4. #include<stdlib.h>
  5. #include<assert.h>
  6. typedef int STDataType;
  7. //支持动态增长的栈
  8. typedef struct Stack
  9. {
  10. STDataType* a;
  11. int top;//栈顶
  12. int capacity;//栈容量
  13. }Stack;
  14. void StackInit(Stack* ps);//初始化栈
  15. void StackPush(Stack* ps, STDataType x);//入栈
  16. void StackPop(Stack* ps);//出栈
  17. STDataType StackTop(Stack* ps);//获取栈顶元素
  18. int StackSize(Stack* ps);//获取栈中有效元素个数
  19. bool StackEmpty(Stack* ps);//检测栈是否为空,为空返回true
  20. void StackDestory(Stack* ps);//销毁栈

3.2 Stack.c

  1. #include"Stack.h"
  2. void StackInit(Stack* ps)
  3. {
  4. ps->a = NULL;
  5. ps->top = ps->capacity = 0;
  6. }
  7. void StackPush(Stack* ps, STDataType x)
  8. {
  9. assert(ps);
  10. //判断是否需要扩容
  11. if (ps->top == ps->capacity)
  12. {
  13. int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
  14. STDataType* temp = (STDataType*)realloc(ps->a,sizeof(STDataType) * newcapacity);
  15. if (temp == NULL)
  16. {
  17. perror("realloc fail");
  18. exit(1);
  19. }
  20. ps->a = temp;
  21. ps->capacity = newcapacity;
  22. }
  23. //压栈
  24. ps->a[ps->top++] = x;
  25. }
  26. void StackPop(Stack* ps)
  27. {
  28. assert(ps);
  29. //如果栈为空,则没有删除的必要
  30. assert(!StackEmpty(ps));
  31. ps->top--;
  32. }
  33. STDataType StackTop(Stack* ps)
  34. {
  35. assert(ps);
  36. //如果栈为空,不可能有栈顶元素
  37. assert(!StackEmpty(ps));
  38. return ps->a[ps->top - 1];
  39. }
  40. int StackSize(Stack* ps)
  41. {
  42. assert(ps);
  43. return ps->top;
  44. }
  45. bool StackEmpty(Stack* ps)
  46. {
  47. assert(ps);
  48. return ps->top == 0;
  49. }
  50. void StackDestory(Stack* ps)
  51. {
  52. free(ps->a);
  53. ps->a = NULL;
  54. ps->top = ps->capacity = 0;
  55. }

3.3 test.c(测试)

  1. #include"Stack.h"
  2. int main()
  3. {
  4. Stack sk;
  5. StackInit(&sk);
  6. StackPush(&sk, 1);
  7. StackPush(&sk, 2);
  8. StackPush(&sk, 3);
  9. StackPush(&sk, 4);
  10. while (!StackEmpty(&sk))
  11. {
  12. printf("%d ", StackTop(&sk));//一边打印栈顶元素
  13. StackPop(&sk);//一边出栈
  14. }
  15. }

 四、栈的相关oj题

4.1 选择题

1、一个栈的初始状态为空。现将元素1、2、3、4、5、A、B、C、D、E依次入栈,然后再依次出栈,则元素出
栈的顺序是(B)。
A  12345ABCDE
B  EDCBA54321
C  ABCDE12345
D  54321EDCBA
解析:后进先出的特点,进栈过程中如果没有出栈,入栈和出栈的顺序是相反的。

2.若进栈序列为 1,2,3,4 ,进栈过程中可以出栈,则下列不可能的一个出栈序列是(C)
A 1,4,3,2
B 2,3,4,1
C 3,1,4,2
D 3,4,2,1
解析:虽然是后进先出,但是入栈和出栈顺序相反是相对的,重点就是要判断进栈过程中是否有出栈,题目有明确提出这一点,所以这题最好同过画图去排除可能性,比如C,3出栈说明1和2都在栈内,下一个要出栈的话只能是2不能是1,1不可能在2的前面出栈。

4.2 有效的括号(OJ题) 

有效的括号(力扣)

条件的意思是:括号可以相互包含,放不能参差摆放

思路:利用栈结构后进先出的特点,让左括号入栈,右括号出栈匹配,当左右括号相等的前提下,如果最后栈为空,则符合题目要求,左括号大于右括号和右括号大于左括号要分开讨论。

  1. typedef char STDataType;
  2. //支持动态增长的栈
  3. typedef struct Stack
  4. {
  5. STDataType* a;
  6. int top;//栈顶
  7. int capacity;//栈容量
  8. }Stack;
  9. void StackInit(Stack* ps);//初始化栈
  10. void StackPush(Stack* ps, STDataType x);//入栈
  11. void StackPop(Stack* ps);//出栈
  12. STDataType StackTop(Stack* ps);//获取栈顶元素
  13. int StackSize(Stack* ps);//获取栈中有效元素个数
  14. bool StackEmpty(Stack* ps);//检测栈是否为空,为空返回true
  15. void StackDestory(Stack* ps);//销毁栈
  16. void StackInit(Stack* ps)
  17. {
  18. ps->a = NULL;
  19. ps->top = ps->capacity = 0;
  20. }
  21. void StackPush(Stack* ps, STDataType x)
  22. {
  23. assert(ps);
  24. //判断是否需要扩容
  25. if (ps->top == ps->capacity)
  26. {
  27. int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
  28. STDataType* temp = (STDataType*)realloc(ps->a,sizeof(STDataType) * newcapacity);
  29. if (temp == NULL)
  30. {
  31. perror("realloc fail");
  32. exit(1);
  33. }
  34. ps->a = temp;
  35. ps->capacity = newcapacity;
  36. }
  37. //压栈
  38. ps->a[ps->top++] = x;
  39. }
  40. void StackPop(Stack* ps)
  41. {
  42. assert(ps);
  43. //如果栈为空,则没有删除的必要
  44. assert(!StackEmpty(ps));
  45. ps->top--;
  46. }
  47. STDataType StackTop(Stack* ps)
  48. {
  49. assert(ps);
  50. //如果栈为空,不可能有栈顶元素
  51. assert(!StackEmpty(ps));
  52. return ps->a[ps->top - 1];
  53. }
  54. int StackSize(Stack* ps)
  55. {
  56. assert(ps);
  57. return ps->top;
  58. }
  59. bool StackEmpty(Stack* ps)
  60. {
  61. assert(ps);
  62. return ps->top == 0;
  63. }
  64. void StackDestory(Stack* ps)
  65. {
  66. free(ps->a);
  67. ps->a = NULL;
  68. ps->top = ps->capacity = 0;
  69. }
  70. bool isValid(char* s)
  71. {
  72. Stack st;
  73. StackInit(&st);
  74. while(*s)
  75. {
  76. if(*s=='('||*s=='{'||*s=='[')
  77. StackPush(&st,*s);
  78. else
  79. {
  80. //如果右比左多,栈为空
  81. if(StackEmpty(&st))
  82. {
  83. StackDestory(&st);
  84. return false;
  85. }
  86. char top=StackTop(&st);
  87. StackPop(&st);
  88. //不匹配
  89. if((*s==')'&&top!='(')||(*s=='}'&&top!='{')||(*s==']'&&top!='['))
  90. {
  91. StackDestory(&st);
  92. return false;
  93. }
  94. }
  95. ++s;
  96. }
  97. //如果左括号比右括号多,栈也有元素
  98. bool ret=StackEmpty(&st);
  99. StackDestory(&st);
  100. return ret;
  101. }

要注意中间的条件判断!! 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号