当前位置:   article > 正文

栈和队列及其应用_int isemptyqueue(queue

int isemptyqueue(queue

栈和队列基本接口的实现

Stack.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<assert.h>
  5. typedef int _STdatatype;
  6. typedef struct Stack
  7. {
  8. _STdatatype * _a;
  9. int _top;
  10. int _capacity;
  11. }Stack;
  12. void InitStack(Stack * ps);
  13. void DestoryStack(Stack * ps);
  14. void PushStack(Stack * ps, _STdatatype x);
  15. void PopStack(Stack * ps);
  16. _STdatatype TopStack(Stack * ps);
  17. int isEmptyStack(Stack * ps);
  18. int SizeStack(Stack * ps);

Stack.c

  1. #include"Stack.h"
  2. void InitStack(Stack * ps)
  3. {
  4. assert(ps);
  5. ps->_a = (_STdatatype *)malloc(sizeof(_STdatatype) * 4);
  6. assert(ps->_a);
  7. ps->_top = 0;
  8. ps->_capacity = 4;
  9. }
  10. void DestoryStack(Stack * ps)
  11. {
  12. assert(ps);
  13. if (ps->_a)
  14. {
  15. free(ps->_a);
  16. ps->_a = NULL;
  17. ps->_top = 0;
  18. ps->_capacity = 0;
  19. }
  20. }
  21. void PushStack(Stack * ps, _STdatatype x)
  22. {
  23. assert(ps);
  24. if (ps->_top == ps->_capacity)
  25. {
  26. ps->_a = (_STdatatype *)realloc(ps->_a, sizeof(_STdatatype) * 2 * ps->_capacity);
  27. ps->_capacity *= 2;
  28. }
  29. ps->_a[ps->_top] = x;
  30. ps->_top++;
  31. }
  32. void PopStack(Stack * ps)
  33. {
  34. assert(ps && ps->_top > 0);
  35. ps->_top--;
  36. }
  37. _STdatatype TopStack(Stack * ps)
  38. {
  39. assert(ps && ps->_top > 0);
  40. return ps->_a[ps->_top - 1];
  41. }
  42. int isEmptyStack(Stack * ps)
  43. {
  44. assert(ps);
  45. return ps->_top == 0 ? 0 : 1;
  46. }
  47. int SizeStack(Stack * ps)
  48. {
  49. assert(ps);
  50. return ps->_top;
  51. }

队列

Queue.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<assert.h>
  5. typedef int _QTdatatype;
  6. typedef struct QueueNode
  7. {
  8. _QTdatatype _data;
  9. struct QueueNode * next;
  10. }QueueNode;
  11. typedef struct Queue
  12. {
  13. QueueNode * _front;
  14. QueueNode * _back;
  15. }Queue;
  16. QueueNode * BuyQueueNode(_QTdatatype x);
  17. void InitQueue(Queue * pq);
  18. void DestoryQueue(Queue * pq);
  19. void PushQueue(Queue * pq, _QTdatatype x);
  20. void PopQueue(Queue * pq);
  21. _QTdatatype FrontQueue(Queue * pq);
  22. _QTdatatype BackQueue(Queue * pq);
  23. int isEmptyQueue(Queue * pq);
  24. int SizeQueue(Queue * pq);

Queue.c

  1. #include"Queue.h"
  2. QueueNode * BuyQueueNode(_QTdatatype x)
  3. {
  4. QueueNode * node = (QueueNode *)malloc(sizeof(QueueNode));
  5. assert(node);
  6. node->next = NULL;
  7. node->_data = x;
  8. return node;
  9. }
  10. void InitQueue(Queue * pq)
  11. {
  12. assert(pq);
  13. pq->_front = NULL;
  14. pq->_back = NULL;
  15. }
  16. void DestoryQueue(Queue * pq)
  17. {
  18. QueueNode * cur;
  19. assert(pq);
  20. cur = pq->_front;
  21. while (cur)
  22. {
  23. free(cur);
  24. cur = cur->next;
  25. }
  26. free(cur);
  27. pq->_front = pq->_back = NULL;
  28. }
  29. void PushQueue(Queue * pq, _QTdatatype x)
  30. {
  31. QueueNode * node = BuyQueueNode(x);
  32. assert(pq);
  33. if (pq->_front == NULL)
  34. {
  35. pq->_front = node;
  36. pq->_back = node;
  37. }
  38. else
  39. {
  40. pq->_back->next = node;
  41. pq->_back = node;
  42. }
  43. }
  44. void PopQueue(Queue * pq)
  45. {
  46. QueueNode * next;
  47. assert(pq);
  48. next = pq->_front->next;
  49. free(pq->_front);
  50. pq->_front = next;
  51. if (next == NULL)
  52. {
  53. pq->_back = NULL;
  54. }
  55. }
  56. _QTdatatype FrontQueue(Queue * pq)
  57. {
  58. assert(pq);
  59. return pq->_front->_data;
  60. }
  61. _QTdatatype BackQueue(Queue * pq)
  62. {
  63. assert(pq);
  64. return pq->_back->_data;
  65. }
  66. int isEmptyQueue(Queue * pq)
  67. {
  68. assert(pq);
  69. return pq->_front == NULL ? 0 : 1;
  70. }
  71. int SizeQueue(Queue * pq)
  72. {
  73. int size = 0;
  74. QueueNode * cur;
  75. assert(pq);
  76. cur = pq->_front;
  77. while (cur)
  78. {
  79. size++;
  80. cur = cur->next;
  81. }
  82. return size;
  83. }

用两个栈实现一个队列

QueueByTwoStack.h

  1. #pragma once
  2. #include"Stack.h"
  3. typedef struct QueueByTwoStack
  4. {
  5. Stack s1;
  6. Stack s2;
  7. }QueueByTwoStack;
  8. void QueueByTwoStackInit(QueueByTwoStack * qts);
  9. void QueueByTwoStackDestory(QueueByTwoStack * qts);
  10. void QueueByTwoStackPush(QueueByTwoStack * qts, _STdatatype x);
  11. void QueueByTwoStackPop(QueueByTwoStack * qts);
  12. _STdatatype QueueByTwoStackFront(QueueByTwoStack * qts);
  13. int QueueByTwoStackEmpty(QueueByTwoStack * qts);
  14. int QueueByTwoStackSize(QueueByTwoStack * qts);

QueueByTwoStack.c

  1. #include"QueueByTwoStack.h"
  2. void QueueByTwoStackInit(QueueByTwoStack * qts)
  3. {
  4. assert(qts);
  5. InitStack(&qts->s1);
  6. InitStack(&qts->s2);
  7. }
  8. void QueueByTwoStackDestory(QueueByTwoStack * qts)
  9. {
  10. assert(qts);
  11. DestoryStack(&qts->s1);
  12. DestoryStack(&qts->s2);
  13. }
  14. void QueueByTwoStackPush(QueueByTwoStack * qts, _STdatatype x)
  15. {
  16. assert(qts);
  17. PushStack(&qts->s1, x);
  18. }
  19. void QueueByTwoStackPop(QueueByTwoStack * qts)
  20. {
  21. assert(qts);
  22. if (isEmptyStack(&qts->s2) == 0)
  23. {
  24. while (isEmptyStack(&qts->s1))
  25. {
  26. PushStack(&qts->s2, TopStack(&qts->s1));
  27. PopStack(&qts->s1);
  28. }
  29. }
  30. PopStack(&qts->s2);
  31. }
  32. _STdatatype QueueByTwoStackFront(QueueByTwoStack * qts)
  33. {
  34. assert(qts);
  35. if (isEmptyStack(&qts->s2) == 0)
  36. {
  37. while (isEmptyStack(&qts->s1))
  38. {
  39. PushStack(&qts->s2, TopStack(&qts->s1));
  40. PopStack(&qts->s1);
  41. }
  42. }
  43. return TopStack(&qts->s2);
  44. }
  45. int QueueByTwoStackEmpty(QueueByTwoStack * qts)
  46. {
  47. assert(qts);
  48. return isEmptyStack(&qts->s1) | isEmptyStack(&qts->s2);
  49. }
  50. int QueueByTwoStackSize(QueueByTwoStack * qts)
  51. {
  52. assert(qts);
  53. return SizeStack(&qts->s1) + SizeStack(&qts->s2);
  54. }

用两个队列实现一个栈

StackByTwoQueue.h

  1. #pragma once
  2. #include"Queue.h"
  3. typedef struct StackByTwoQueue
  4. {
  5. Queue q1;
  6. Queue q2;
  7. }StackByTwoQueue;
  8. void StackByTwoQueueInit(StackByTwoQueue * stq);
  9. void StackByTwoQueueDestory(StackByTwoQueue * stq);
  10. void StackByTwoQueuePush(StackByTwoQueue * stq, _QTdatatype x);
  11. void StackByTwoQueuePop(StackByTwoQueue * stq);
  12. _QTdatatype StackByTwoQueueTop(StackByTwoQueue * stq);
  13. int StackByTwoQueueEmpty(StackByTwoQueue * stq);
  14. int StackByTwoQueueSize(StackByTwoQueue * stq);

StackByTwoQueue.c

  1. #include"StackByTwoQueue.h"
  2. void StackByTwoQueueInit(StackByTwoQueue * stq)
  3. {
  4. assert(stq);
  5. InitQueue(&stq->q1);
  6. InitQueue(&stq->q2);
  7. }
  8. void StackByTwoQueueDestory(StackByTwoQueue * stq)
  9. {
  10. assert(stq);
  11. DestoryQueue(&stq->q1);
  12. DestoryQueue(&stq->q2);
  13. }
  14. void StackByTwoQueuePush(StackByTwoQueue * stq, _QTdatatype x)
  15. {
  16. assert(stq);
  17. if (isEmptyQueue(&stq->q1) != 0)
  18. {
  19. PushQueue(&stq->q1, x);
  20. }
  21. else
  22. {
  23. PushQueue(&stq->q2, x);
  24. }
  25. }
  26. void StackByTwoQueuePop(StackByTwoQueue * stq)
  27. {
  28. Queue * empty = &stq->q1, *noempty = &stq->q2;
  29. assert(stq);
  30. if (isEmptyQueue(&stq->q1) != 0)
  31. {
  32. empty = &stq->q2;
  33. noempty = &stq->q1;
  34. }
  35. while (SizeQueue(noempty) > 1)
  36. {
  37. PushQueue(empty, FrontQueue(noempty));
  38. PopQueue(noempty);
  39. }
  40. PopQueue(noempty);
  41. }
  42. _QTdatatype StackByTwoQueueTop(StackByTwoQueue * stq)
  43. {
  44. assert(stq);
  45. if (isEmptyQueue(&stq->q1))
  46. {
  47. return BackQueue(&stq->q1);
  48. }
  49. else
  50. {
  51. return BackQueue(&stq->q2);
  52. }
  53. }
  54. int StackByTwoQueueEmpty(StackByTwoQueue * stq)
  55. {
  56. assert(stq);
  57. return isEmptyQueue(&stq->q1) | isEmptyQueue(&stq->q2);
  58. }
  59. int StackByTwoQueueSize(StackByTwoQueue * stq)
  60. {
  61. assert(stq);
  62. return SizeQueue(&stq->q1) + SizeQueue(&stq->q2);
  63. }

测试代码

test.c

  1. #include"Stack.h"
  2. #include"Queue.h"
  3. #include"QueueByTwoStack.h"
  4. #include"StackByTwoQueue.h"
  5. void testStack()
  6. {
  7. Stack s;
  8. InitStack(&s);
  9. PushStack(&s, 1);
  10. PushStack(&s, 2);
  11. PushStack(&s, 3);
  12. PushStack(&s, 4);
  13. PushStack(&s, 5);
  14. PushStack(&s, 6);
  15. printf("%d %d\n", isEmptyStack(&s), SizeStack(&s));
  16. while (isEmptyStack(&s))
  17. {
  18. printf("%d ",TopStack(&s));
  19. PopStack(&s);
  20. }
  21. printf("\n");
  22. printf("%d %d\n", isEmptyStack(&s), SizeStack(&s));
  23. DestoryStack(&s);
  24. }
  25. void testQueue()
  26. {
  27. Queue q;
  28. InitQueue(&q);
  29. PushQueue(&q, 1);
  30. PushQueue(&q, 2);
  31. PushQueue(&q, 3);
  32. PushQueue(&q, 4);
  33. PushQueue(&q, 5);
  34. PushQueue(&q, 6);
  35. printf("%d %d\n", isEmptyQueue(&q), SizeQueue(&q));
  36. while (isEmptyQueue(&q))
  37. {
  38. printf("%d ", FrontQueue(&q));
  39. PopQueue(&q);
  40. }
  41. printf("\n");
  42. printf("%d %d\n", isEmptyQueue(&q), SizeQueue(&q));
  43. DestoryQueue(&q);
  44. }
  45. void testStackByTwoQueue()
  46. {
  47. QueueByTwoStack qts;
  48. QueueByTwoStackInit(&qts);
  49. QueueByTwoStackPush(&qts, 1);
  50. QueueByTwoStackPush(&qts, 2);
  51. QueueByTwoStackPush(&qts, 3);
  52. QueueByTwoStackPush(&qts, 4);
  53. printf("%d %d\n", QueueByTwoStackEmpty(&qts), QueueByTwoStackSize(&qts));
  54. while (QueueByTwoStackEmpty(&qts))
  55. {
  56. printf("%d ",QueueByTwoStackFront(&qts));
  57. QueueByTwoStackPop(&qts);
  58. }
  59. printf("\n");
  60. QueueByTwoStackPush(&qts, 5);
  61. QueueByTwoStackPush(&qts, 6);
  62. printf("%d %d\n", QueueByTwoStackEmpty(&qts), QueueByTwoStackSize(&qts));
  63. while (QueueByTwoStackEmpty(&qts))
  64. {
  65. printf("%d ", QueueByTwoStackFront(&qts));
  66. QueueByTwoStackPop(&qts);
  67. }
  68. printf("\n");
  69. printf("%d %d\n", QueueByTwoStackEmpty(&qts), QueueByTwoStackSize(&qts));
  70. QueueByTwoStackDestory(&qts);
  71. }
  72. void testQueueByTwoStack()
  73. {
  74. StackByTwoQueue stq;
  75. StackByTwoQueueInit(&stq);
  76. StackByTwoQueuePush(&stq, 1);
  77. StackByTwoQueuePush(&stq, 2);
  78. StackByTwoQueuePush(&stq, 3);
  79. StackByTwoQueuePush(&stq, 4);
  80. printf("%d %d\n",StackByTwoQueueEmpty(&stq),StackByTwoQueueSize(&stq));
  81. while (StackByTwoQueueEmpty(&stq))
  82. {
  83. printf("%d ", StackByTwoQueueTop(&stq));
  84. StackByTwoQueuePop(&stq);
  85. }
  86. printf("\n");
  87. StackByTwoQueuePush(&stq, 5);
  88. StackByTwoQueuePush(&stq, 6);
  89. printf("%d %d\n", StackByTwoQueueEmpty(&stq), StackByTwoQueueSize(&stq));
  90. while (StackByTwoQueueEmpty(&stq))
  91. {
  92. printf("%d ", StackByTwoQueueTop(&stq));
  93. StackByTwoQueuePop(&stq);
  94. }
  95. printf("\n");
  96. printf("%d %d\n", StackByTwoQueueEmpty(&stq), StackByTwoQueueSize(&stq));
  97. StackByTwoQueueDestory(&stq);
  98. }
  99. int main()
  100. {
  101. testStack();
  102. testQueue();
  103. testStackByTwoQueue();
  104. testQueueByTwoStack();
  105. return 0;
  106. }

 

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

闽ICP备14008679号