当前位置:   article > 正文

栈和队列&循环队列(C/C++)

栈和队列&循环队列(C/C++)

        本篇将用数组实现栈、链表实现队列以及使用数组实现循环队列,然后实现了用栈实现队列和用队列实现栈以及一些其他的栈和队列的习题,加深队栈和队列的理解。

        若只看对应所需,可直接看文章旁的目录。

1.栈

1.1栈的概念及结构

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

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

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

        上图就是出栈和压栈的示意图。 

1.2栈的实现

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

        所以本篇将使用数组实现栈。

        注:对于数组实现栈,我们可以将栈顶指针指向栈顶元素,也可以指向栈顶元素的上一个位置,这两种实现方式都是可以的。本篇的栈顶指针指向的是栈的上一个元素。两种的实现方式都有略微的不同。

        对于栈的实现,对于较难理解的将给出注释,如下:

1.2.1 Stack.h

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <assert.h>
  6. //定义元素数据类型
  7. typedef int STDataType;
  8. //栈的数据结构
  9. typedef struct Stack {
  10. STDataType* arr;
  11. int top;
  12. int capacity;
  13. }Stack;
  14. //栈的初始化以及销毁
  15. void STInit(Stack* ps);
  16. void STDestroy(Stack* ps);
  17. //压栈与出栈
  18. void STPush(Stack* ps, STDataType x);
  19. void STPop(Stack* ps);
  20. //返回栈顶元素/栈的尺寸/栈是否为NULL
  21. STDataType STTop(Stack* ps);
  22. int STSize(Stack* ps);
  23. bool STEmpty(Stack* ps);

1.2.2 Stack.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "Stack.h"
  3. void STInit(Stack* ps) {
  4. assert(ps);
  5. ps->arr = NULL;
  6. ps->capacity = 0;
  7. ps->top = 0;
  8. }
  9. void STDestroy(Stack* ps) {
  10. assert(ps);
  11. free(ps->arr);
  12. ps->arr = NULL;
  13. ps->capacity = 0;
  14. ps->top = 0;
  15. ps = NULL;
  16. }
  17. void STPush(Stack* ps, STDataType x) {
  18. assert(ps);
  19. //判断栈是否需要扩容
  20. if (STSize(ps) == ps->capacity) {
  21. //三目操作符进行扩容,第一次的容量为4,以后每次扩容一倍
  22. int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
  23. STDataType* tmp = (STDataType*)realloc(ps->arr, newcapacity * sizeof(STDataType));
  24. if (tmp == NULL) {
  25. perror("realloc:");
  26. exit(1);
  27. }
  28. ps->arr = tmp;
  29. ps->capacity = newcapacity;
  30. }
  31. ps->arr[ps->top] = x;
  32. ps->top++;
  33. }
  34. void STPop(Stack* ps) {
  35. assert(ps);
  36. assert(!STEmpty(ps));
  37. //出栈只需要将栈的尺寸减小,下次压栈的元素直接进行覆盖
  38. ps->top--;
  39. }
  40. STDataType STTop(Stack* ps) {
  41. assert(ps);
  42. assert(!STEmpty(ps));
  43. STDataType ret = ps->arr[ps->top - 1];
  44. return ret;
  45. }
  46. int STSize(Stack* ps) {
  47. assert(ps);
  48. //栈顶的编号其实就是栈的尺寸
  49. return ps->top;
  50. }
  51. bool STEmpty(Stack* ps) {
  52. assert(ps);
  53. return ps->top == 0;
  54. }

1.2.3 All of Code

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <assert.h>
  6. //定义元素数据类型
  7. typedef int STDataType;
  8. //栈的数据结构
  9. typedef struct Stack {
  10. STDataType* arr;
  11. int top;
  12. int capacity;
  13. }Stack;
  14. //栈的初始化以及销毁
  15. void STInit(Stack* ps);
  16. void STDestroy(Stack* ps);
  17. //压栈与出栈
  18. void STPush(Stack* ps, STDataType x);
  19. void STPop(Stack* ps);
  20. //返回栈顶元素/栈的尺寸/栈是否为NULL
  21. STDataType STTop(Stack* ps);
  22. int STSize(Stack* ps);
  23. bool STEmpty(Stack* ps);
  24. void STInit(Stack* ps) {
  25. assert(ps);
  26. ps->arr = NULL;
  27. ps->capacity = 0;
  28. ps->top = 0;
  29. }
  30. void STDestroy(Stack* ps) {
  31. assert(ps);
  32. free(ps->arr);
  33. ps->arr = NULL;
  34. ps->capacity = 0;
  35. ps->top = 0;
  36. ps = NULL;
  37. }
  38. void STPush(Stack* ps, STDataType x) {
  39. assert(ps);
  40. //判断栈是否需要扩容
  41. if (STSize(ps) == ps->capacity) {
  42. //三目操作符进行扩容,第一次的容量为4,以后每次扩容一倍
  43. int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
  44. STDataType* tmp = (STDataType*)realloc(ps->arr, newcapacity * sizeof(STDataType));
  45. if (tmp == NULL) {
  46. perror("realloc:");
  47. exit(1);
  48. }
  49. ps->arr = tmp;
  50. ps->capacity = newcapacity;
  51. }
  52. ps->arr[ps->top] = x;
  53. ps->top++;
  54. }
  55. void STPop(Stack* ps) {
  56. assert(ps);
  57. assert(!STEmpty(ps));
  58. //出栈只需要将栈的尺寸减小,下次压栈的元素直接进行覆盖
  59. ps->top--;
  60. }
  61. STDataType STTop(Stack* ps) {
  62. assert(ps);
  63. assert(!STEmpty(ps));
  64. STDataType ret = ps->arr[ps->top - 1];
  65. return ret;
  66. }
  67. int STSize(Stack* ps) {
  68. assert(ps);
  69. //栈顶的编号其实就是栈的尺寸
  70. return ps->top;
  71. }
  72. bool STEmpty(Stack* ps) {
  73. assert(ps);
  74. return ps->top == 0;
  75. }
  76. int main() {
  77. Stack st;
  78. STInit(&st);
  79. STPush(&st, 1);
  80. STPush(&st, 2);
  81. STPush(&st, 3);
  82. while (!STEmpty(&st)) {
  83. STDataType top = STTop(&st);
  84. STPop(&st);
  85. printf("%d ", top);
  86. }
  87. STPush(&st, 4);
  88. STPush(&st, 5);
  89. while (!STEmpty(&st)) {
  90. STDataType top = STTop(&st);
  91. STPop(&st);
  92. printf("%d ", top);
  93. }
  94. STDestroy(&st);
  95. return 0;
  96. }

        测试结果: 

2.队列

2.1 队列的概念及结构

        队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out)的性质。

        入队列:进行插入操作的一端称为队尾。

        出队列:进行删除操作的一端称为队头。 

2.2队列的实现 

        对于队列的实现,都可以使用数组以及链表结构,但是使用链表结构实现出队以及入队将更方便,同时还会节省更多的空间。

        如上图所示,当使用数组实现队列时,由于开辟的空间是固定的,所以出队时将会浪费一些空间。 

2.2.1 Queue.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. typedef int QDataType;
  7. //队列节点的数据结构
  8. typedef struct Node {
  9. QDataType val;
  10. struct Node* next;
  11. }QNode;
  12. //队列的队首指针、队尾指针、尺寸
  13. typedef struct Queue {
  14. int size;
  15. QNode* phead;
  16. QNode* ptail;
  17. }Queue;
  18. //队列的初始化/销毁
  19. void QueueInit(Queue* pQ);
  20. void QueueDestory(Queue* pQ);
  21. //队列的入队/出队
  22. void QueuePush(Queue* pQ, QDataType x);
  23. void QueuePop(Queue* pQ);
  24. //返回队首元素/队尾元素
  25. QDataType QueueFront(Queue* pQ);
  26. QDataType QueueBack(Queue* pQ);
  27. //判断队列是否为NULL/返回队列的大小
  28. bool QueueEmpty(Queue* pQ);
  29. int QueueSize(Queue* pQ);

2.2.2 Queue.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "Queue.h"
  3. void QueueInit(Queue* pQ) {
  4. assert(pQ);
  5. pQ->phead = pQ->ptail = NULL;
  6. pQ->size = 0;
  7. }
  8. void QueueDestory(Queue* pQ) {
  9. assert(pQ);
  10. QNode* cur = pQ->phead;
  11. while (cur) {
  12. QNode* next = cur->next;
  13. free(cur);
  14. cur = next;
  15. }
  16. pQ->phead = pQ->ptail = NULL;
  17. pQ->size = 0;
  18. }
  19. void QueuePush(Queue* pQ, QDataType x) {
  20. assert(pQ);
  21. QNode* newnode = (QNode*)malloc(sizeof(QNode));
  22. if (newnode == NULL) {
  23. perror("malloc:");
  24. exit(1);
  25. }
  26. newnode->val = x;
  27. newnode->next = NULL;
  28. if (pQ->phead == NULL) {
  29. pQ->phead = pQ->ptail = newnode;
  30. }
  31. else {
  32. pQ->ptail->next = newnode;
  33. pQ->ptail = newnode;
  34. }
  35. pQ->size++;
  36. }
  37. void QueuePop(Queue* pQ) {
  38. assert(pQ);
  39. assert(pQ->phead);
  40. QNode* cur = pQ->phead;
  41. pQ->phead = pQ->phead->next;
  42. free(cur);
  43. cur = NULL;
  44. pQ->size--;
  45. }
  46. QDataType QueueFront(Queue* pQ) {
  47. assert(pQ);
  48. assert(pQ->phead);
  49. return pQ->phead->val;
  50. }
  51. QDataType QueueBack(Queue* pQ) {
  52. assert(pQ);
  53. assert(pQ->phead);
  54. return pQ->ptail->val;
  55. }
  56. bool QueueEmpty(Queue* pQ) {
  57. assert(pQ);
  58. return pQ->phead == NULL;
  59. }
  60. int QueueSize(Queue* pQ) {
  61. assert(pQ);
  62. return pQ->size;
  63. }

2.2.3 All of Code

 

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #include <stdbool.h>
  6. typedef int QDataType;
  7. //队列节点的数据结构
  8. typedef struct Node {
  9. QDataType val;
  10. struct Node* next;
  11. }QNode;
  12. //队列的队首指针、队尾指针、尺寸
  13. typedef struct Queue {
  14. int size;
  15. QNode* phead;
  16. QNode* ptail;
  17. }Queue;
  18. //队列的初始化/销毁
  19. void QueueInit(Queue* pQ);
  20. void QueueDestory(Queue* pQ);
  21. //队列的入队/出队
  22. void QueuePush(Queue* pQ, QDataType x);
  23. void QueuePop(Queue* pQ);
  24. //返回队首元素/队尾元素
  25. QDataType QueueFront(Queue* pQ);
  26. QDataType QueueBack(Queue* pQ);
  27. //判断队列是否为NULL/返回队列的大小
  28. bool QueueEmpty(Queue* pQ);
  29. int QueueSize(Queue* pQ);
  30. void QueueInit(Queue* pQ) {
  31. assert(pQ);
  32. pQ->phead = pQ->ptail = NULL;
  33. pQ->size = 0;
  34. }
  35. void QueueDestory(Queue* pQ) {
  36. assert(pQ);
  37. QNode* cur = pQ->phead;
  38. while (cur) {
  39. QNode* next = cur->next;
  40. free(cur);
  41. cur = next;
  42. }
  43. pQ->phead = pQ->ptail = NULL;
  44. pQ->size = 0;
  45. }
  46. void QueuePush(Queue* pQ, QDataType x) {
  47. assert(pQ);
  48. QNode* newnode = (QNode*)malloc(sizeof(QNode));
  49. if (newnode == NULL) {
  50. perror("malloc:");
  51. exit(1);
  52. }
  53. newnode->val = x;
  54. newnode->next = NULL;
  55. if (pQ->phead == NULL) {
  56. pQ->phead = pQ->ptail = newnode;
  57. }
  58. else {
  59. pQ->ptail->next = newnode;
  60. pQ->ptail = newnode;
  61. }
  62. pQ->size++;
  63. }
  64. void QueuePop(Queue* pQ) {
  65. assert(pQ);
  66. assert(pQ->phead);
  67. QNode* cur = pQ->phead;
  68. pQ->phead = pQ->phead->next;
  69. free(cur);
  70. cur = NULL;
  71. pQ->size--;
  72. }
  73. QDataType QueueFront(Queue* pQ) {
  74. assert(pQ);
  75. assert(pQ->phead);
  76. return pQ->phead->val;
  77. }
  78. QDataType QueueBack(Queue* pQ) {
  79. assert(pQ);
  80. assert(pQ->phead);
  81. return pQ->ptail->val;
  82. }
  83. bool QueueEmpty(Queue* pQ) {
  84. assert(pQ);
  85. return pQ->phead == NULL;
  86. }
  87. int QueueSize(Queue* pQ) {
  88. assert(pQ);
  89. return pQ->size;
  90. }
  91. int main() {
  92. Queue queue;
  93. QueueInit(&queue);
  94. QueuePush(&queue, 1);
  95. QueuePush(&queue, 2);
  96. printf("%d ", QueueFront(&queue));
  97. QueuePop(&queue);
  98. QueuePush(&queue, 3);
  99. QueuePush(&queue, 4);
  100. printf("%d ", QueueFront(&queue));
  101. QueuePop(&queue);
  102. QueuePush(&queue, 5);
  103. while (!QueueEmpty(&queue)) {
  104. printf("%d ", QueueFront(&queue));
  105. QueuePop(&queue);
  106. }
  107. QueueDestory(&queue);
  108. return 0;
  109. }

        测试结果:

2.3循环队列的实现 

        在上文中我们提到,当我们使用数组实现队列的时候,会出现一些空间浪费的情况,这是因为出队时,出队下标将向后移动,前面的空间则浪费,当我们使用循环链表就可以很好的解决该问题。

        循环链表:将数组的首尾相连,组成的一个特殊结构。我们用两个指针来表示队列的队首和队尾,front 表示队首,back表示队尾的下一个元素。

        为了能使Q.front=Q.back来区别队列是否为空还是满,我们常常认为出现作图时的情况即为队满的情况,此时:Q.front=Q.back+1;

        对于循环队列的代码实现如下,对于代码的解释在注释给出。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. typedef struct {
  5. int* arr;
  6. int front;
  7. int back;
  8. int k;
  9. } MyCircularQueue;
  10. //循环队列的初始化
  11. MyCircularQueue* myCircularQueueCreate(int k) {
  12. //堆上分配空间,才能在出函数时仍然存在
  13. MyCircularQueue* Queue=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
  14. if(Queue==NULL){
  15. perror("malloc:");
  16. exit(1);
  17. }
  18. //分配k+1个空间,便于我们判断是否为满队列还是空队列
  19. Queue->arr=(int*)malloc(sizeof(int)*(k+1));
  20. Queue->front=Queue->back=0;
  21. Queue->k=k;
  22. return Queue;
  23. }
  24. //判断队列是否为NULL队列
  25. bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
  26. return obj->front==obj->back;
  27. }
  28. //判断队列是否为满队列
  29. bool myCircularQueueIsFull(MyCircularQueue* obj) {
  30. return obj->front==(obj->back+1)%(obj->k+1);
  31. }
  32. //在队列中加入元素
  33. bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
  34. if(myCircularQueueIsFull(obj)){
  35. return false;
  36. }
  37. else{
  38. obj->arr[obj->back]=value;
  39. obj->back++;
  40. obj->back%=(obj->k+1);
  41. return true;
  42. }
  43. }
  44. //队列中删除元素
  45. bool myCircularQueueDeQueue(MyCircularQueue* obj) {
  46. if(myCircularQueueIsEmpty(obj)){
  47. return false;
  48. }
  49. else{
  50. obj->front++;
  51. obj->front%=(obj->k+1);
  52. return true;
  53. }
  54. }
  55. //返回队首元素
  56. int myCircularQueueFront(MyCircularQueue* obj) {
  57. if(myCircularQueueIsEmpty(obj)){
  58. return -1;
  59. }
  60. return obj->arr[obj->front];
  61. }
  62. //返回队尾元素
  63. int myCircularQueueRear(MyCircularQueue* obj) {
  64. if(myCircularQueueIsEmpty(obj)){
  65. return -1;
  66. }
  67. //对于队尾元素的返回,需要注意是否为队列中第k+1个元素,有以下两种写法
  68. //return obj->arr[(obj->back+obj->k)%(obj->k+1)];
  69. if(obj->back==0){
  70. return obj->arr[obj->k];
  71. }else{
  72. return obj->arr[obj->back-1];
  73. }
  74. }
  75. //释放空间
  76. void myCircularQueueFree(MyCircularQueue* obj) {
  77. free(obj->arr);
  78. free(obj);
  79. }

        对于以上循环队列的实现,其实是一个习题,链接如下:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。icon-default.png?t=N7T8https://leetcode.cn/problems/design-circular-queue/description/         测试结果:

3.栈与队列相关习题

        注:在习题中使用的函数都是以上的代码函数。

3.1队列实现栈

        使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppop 和 empty)。 

        习题链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。icon-default.png?t=N7T8https://leetcode.cn/problems/implement-stack-using-queues        若要使用两个队列实现栈的操作,最难的两个操作及就是出栈和压栈。

        对于压栈,我们只需将要压栈的元素压入一个非空队列,另一个队列保存空。

        对于出栈,我们只需要除最后一个元素的队列进入另一个空队列,然后在删除最后一个元素及就实现了出栈。代码如下:

  1. typedef struct {
  2. Queue p1;
  3. Queue p2;
  4. } MyStack;
  5. MyStack* myStackCreate() {
  6. MyStack* stack=(MyStack*)malloc(sizeof(MyStack));
  7. if(stack==NULL){
  8. perror("stack's malloc:");
  9. exit(1);
  10. }
  11. QueueInit(&stack->p2);
  12. QueueInit(&stack->p1);
  13. return stack;
  14. }
  15. void myStackPush(MyStack* obj, int x) {
  16. if(!QueueEmpty(&obj->p1))
  17. QueuePush(&obj->p1,x);
  18. else
  19. QueuePush(&obj->p2,x);
  20. }
  21. int myStackPop(MyStack* obj) {
  22. Queue* empty=&obj->p1;
  23. Queue* nonempty=&obj->p2;
  24. if(!QueueEmpty(empty)){
  25. empty=&obj->p2;
  26. nonempty=&obj->p1;
  27. }
  28. while(QueueSize(nonempty)>1){
  29. QueuePush(empty,QueueFront(nonempty));
  30. QueuePop(nonempty);
  31. }
  32. int top=QueueFront(nonempty);
  33. QueuePop(nonempty);
  34. return top;
  35. }
  36. int myStackTop(MyStack* obj) {
  37. if(!QueueEmpty(&obj->p1)){
  38. return QueueBack(&obj->p1);
  39. }else{
  40. return QueueBack(&obj->p2);
  41. }
  42. }
  43. bool myStackEmpty(MyStack* obj) {
  44. return QueueEmpty(&obj->p1)&&QueueEmpty(&obj->p2);
  45. }
  46. void myStackFree(MyStack* obj) {
  47. QueueDestory(&obj->p1);
  48. QueueDestory(&obj->p2);
  49. free(obj);
  50. obj=NULL;
  51. }

        测试结果:

3.2栈实现队列 

        使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

        习题链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。icon-default.png?t=N7T8https://leetcode.cn/problems/implement-queue-using-stacks/         使用两个栈实现队列,主要思想为:一组数据进栈两次即可实现先进先出。

        一个栈作为入队栈,只用来将数据压入栈;

        另一个栈作为出队栈,只用来将数据弹出栈;

        当我们需要将数据出队时,我们将入队栈的数据全部压入出队栈,此时出栈的顺序正确,注意:这里只有当出队栈为空时才能将入队栈的数据压入出队栈,要不然会导致出队的顺序错乱。

  1. typedef struct {
  2. Stack pushst;
  3. Stack popst;
  4. } MyQueue;
  5. MyQueue* myQueueCreate() {
  6. MyQueue* Queue=(MyQueue*)malloc(sizeof(MyQueue));
  7. if(Queue==NULL){
  8. perror("queue malloc:");
  9. exit(1);
  10. }
  11. STInit(&Queue->pushst);
  12. STInit(&Queue->popst);
  13. return Queue;
  14. }
  15. void myQueuePush(MyQueue* obj, int x) {
  16. //将元素压入push栈
  17. STPush(&obj->pushst,x);
  18. }
  19. int myQueuePeek(MyQueue* obj) {
  20. if(STEmpty(&obj->popst)){
  21. while(!STEmpty(&obj->pushst)){
  22. STPush(&obj->popst,STTop(&obj->pushst));
  23. STPop(&obj->pushst);
  24. }
  25. }
  26. int front=STTop(&obj->popst);
  27. return front;
  28. }
  29. int myQueuePop(MyQueue* obj) {
  30. int front=myQueuePeek(obj);
  31. STPop(&obj->popst);
  32. return front;
  33. }
  34. bool myQueueEmpty(MyQueue* obj) {
  35. return STEmpty(&obj->popst)&&STEmpty(&obj->pushst);
  36. }
  37. void myQueueFree(MyQueue* obj) {
  38. STDestroy(&obj->popst);
  39. STDestroy(&obj->pushst);
  40. free(obj);
  41. obj=NULL;
  42. }

        测试结果: 

3.3有效的括号匹配

        给定一个只包括 '('')''{''}''['']' 的字符串 s ,判断字符串是否有效。

        习题链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。icon-default.png?t=N7T8https://leetcode.cn/problems/valid-parentheses/description/         对于该题的实现,主要思想是将所有的左括号压入栈,只要遇到右括号,将栈的中左括号弹出,判断是否配对,若不配对则返回ERROR,一直配对和判断,若最后栈为NULL,说明完全配对,则我们可以判断括号匹配。代码如下:

  1. bool isValid(char* s) {
  2. Stack st;
  3. STInit(&st);
  4. while(*s){
  5. if(*s=='['||*s=='('||*s=='{'){
  6. STPush(&st,*s);
  7. }else{
  8. if(STEmpty(&st)){
  9. STDestroy(&st);
  10. return false;
  11. }
  12. char ch=STTop(&st);
  13. STPop(&st);
  14. if((ch=='('&&*s!=')')||(ch=='{'&&*s!='}')||(ch=='['&&*s!=']')){
  15. return false;
  16. }
  17. }
  18. s++;
  19. }
  20. bool ret=STEmpty(&st);
  21. STDestroy(&st);
  22. return ret;
  23. }

        测试结果:

 

 

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

闽ICP备14008679号