当前位置:   article > 正文

【C语言】用队列实现栈

【C语言】用队列实现栈

用两个队列(先进先出)实现一个栈(后进先出

题目链接:https://leetcode.cn/problems/implement-stack-using-queues/description/

1.C语言首先要造一个队列出来

2.两个队列实现栈,始终保持一个队列为空,一个队列非空的状态

3.根据栈先进先出的特性,入栈要把数据放入非空队列中

4.取栈顶元素则取非空队列的队尾即可

5.出栈,并返回栈顶元素的值,则需要把非空队列中n个数据的前n-1个数据导入空队列中,剩下的唯一一个数据就是要出栈的数据。

  1. typedef int QDataType;
  2. typedef struct QueueNode
  3. {
  4. QDataType val;
  5. struct QueueNode* next;
  6. }QNode;
  7. typedef struct Queue
  8. {
  9. QNode* phead;
  10. QNode* ptail;
  11. int size;
  12. }Queue;
  13. //初始化
  14. void QueueInit(Queue* pq)
  15. {
  16. assert(pq);
  17. pq->phead = pq->ptail = NULL;
  18. pq->size = 0;
  19. }
  20. //销毁
  21. void QueueDestroy(Queue* pq)
  22. {
  23. assert(pq);
  24. QNode* cur = pq->phead;
  25. while (cur)
  26. {
  27. QNode* next = cur->next;
  28. free(cur);
  29. cur = next;
  30. }
  31. pq->phead = pq->ptail = NULL;
  32. pq->size = 0;
  33. }
  34. //入队列
  35. void QueuePush(Queue* pq, QDataType x)
  36. {
  37. assert(pq);
  38. //在队尾入,尾插
  39. //创建新节点
  40. QNode* newnode = (QNode*)malloc(sizeof(QNode));
  41. if (newnode == NULL)
  42. {
  43. perror("malloc fail");
  44. return;
  45. }
  46. newnode->val = x;
  47. newnode->next = NULL;
  48. //空链表
  49. if (pq->phead==NULL)
  50. {
  51. pq->phead = pq->ptail = newnode;
  52. }
  53. else
  54. {
  55. pq->ptail->next = newnode;
  56. pq->ptail = newnode;
  57. }
  58. pq->size++;
  59. }
  60. //出队列
  61. void QueuePop(Queue* pq)
  62. {
  63. assert(pq);
  64. //零个节点
  65. assert(pq->phead);
  66. //一个节点
  67. if (pq->phead->next == NULL)
  68. {
  69. free(pq->phead);
  70. pq->phead = pq->ptail = NULL;
  71. }
  72. else
  73. {
  74. QNode* next = pq->phead->next;
  75. free(pq->phead);
  76. pq->phead = next;
  77. }
  78. pq->size--;
  79. }
  80. //判空
  81. bool QueueEmpty(Queue* pq)
  82. {
  83. assert(pq);
  84. return pq->size == 0;
  85. }
  86. //取队头
  87. QDataType QueueFront(Queue* pq)
  88. {
  89. assert(pq);
  90. assert(pq->phead);
  91. return pq->phead->val;
  92. }
  93. //取队尾
  94. QDataType QueueBack(Queue* pq)
  95. {
  96. assert(pq);
  97. assert(pq->ptail);
  98. return pq->ptail->val;
  99. }
  100. //队列长度
  101. size_t QueueLength(Queue* pq)
  102. {
  103. assert(pq);
  104. return pq->size;
  105. }
  106. typedef struct {
  107. Queue obj1;
  108. Queue obj2;
  109. } MyStack;
  110. MyStack* myStackCreate() {
  111. MyStack* p=(MyStack*)malloc(sizeof(MyStack));
  112. QueueInit(&p->obj1);
  113. QueueInit(&p->obj2);
  114. return p;
  115. }
  116. void myStackPush(MyStack* obj, int x) {
  117. //入栈在非空队列
  118. if(QueueEmpty(&obj->obj1))
  119. {
  120. QueuePush(&obj->obj2,x);
  121. }
  122. else
  123. {
  124. QueuePush(&obj->obj1,x);
  125. }
  126. }
  127. int myStackPop(MyStack* obj) {
  128. Queue* EmptyQ=&obj->obj1;
  129. Queue* NoEmptyQ=&obj->obj2;
  130. if(!QueueEmpty(&obj->obj1))
  131. {
  132. EmptyQ=&obj->obj2;
  133. NoEmptyQ=&obj->obj1;
  134. }
  135. while(QueueLength(NoEmptyQ)>1)
  136. {
  137. int front=QueueFront(NoEmptyQ);
  138. QueuePush(EmptyQ,front);
  139. QueuePop(NoEmptyQ);
  140. }
  141. int front=QueueFront(NoEmptyQ);
  142. QueuePop(NoEmptyQ);
  143. return front;
  144. }
  145. int myStackTop(MyStack* obj) {
  146. Queue* EmptyQ=&obj->obj1;
  147. Queue* NoEmptyQ=&obj->obj2;
  148. if(!QueueEmpty(EmptyQ))
  149. {
  150. EmptyQ=&obj->obj2;
  151. NoEmptyQ=&obj->obj1;
  152. }
  153. return QueueBack(NoEmptyQ);
  154. }
  155. bool myStackEmpty(MyStack* obj) {
  156. return QueueEmpty(&obj->obj1)&&QueueEmpty(&obj->obj2);
  157. }
  158. void myStackFree(MyStack* obj) {
  159. QueueDestroy(&obj->obj1);
  160. QueueDestroy(&obj->obj2);
  161. free(obj);
  162. }
  163. /**
  164. * Your MyStack struct will be instantiated and called as such:
  165. * MyStack* obj = myStackCreate();
  166. * myStackPush(obj, x);
  167. * int param_2 = myStackPop(obj);
  168. * int param_3 = myStackTop(obj);
  169. * bool param_4 = myStackEmpty(obj);
  170. * myStackFree(obj);
  171. */

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

闽ICP备14008679号