当前位置:   article > 正文

用栈实现队列(力扣第232题)

用栈实现队列(力扣第232题)

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "assert.h"
  3. #include "stdio.h"
  4. #include "stdbool.h"
  5. #include "stdlib.h"
  6. #include "string.h"
  7. #define N 10
  8. typedef int STDataType;
  9. int data;
  10. //静态栈
  11. //typedef struct Stack {
  12. // STDataType _a[N];
  13. // int _top;//栈顶元素
  14. //}Stack;
  15. //动态栈
  16. typedef struct ST {
  17. STDataType* _a;
  18. int _top;//栈顶元素
  19. int _capacity;//最大容量
  20. }Stack;
  21. //初始化栈
  22. void StackInit(Stack *pst);
  23. //入栈
  24. void StackPush(Stack* pst, STDataType x);
  25. //出栈
  26. void StackPop(Stack* pst);
  27. //获取栈顶元素
  28. STDataType StackTop(Stack* pst);
  29. //获取栈的有效元素个数
  30. int StackSize(Stack* pst);
  31. //判断栈是否为空,是返回1,非空返回0
  32. bool StackEmpty(Stack* pst);
  33. //打印栈同时销毁
  34. void StackPrint(Stack* pst);
  35. //销毁栈
  36. void StackDestory(Stack* pst);
  37. //初始化栈
  38. void StackInit(Stack* pst)
  39. {
  40. assert(pst);
  41. pst->_a = NULL;
  42. pst->_top = 0;
  43. pst->_capacity = 0;
  44. }
  45. //入栈
  46. void StackPush(Stack* pst, STDataType x)
  47. {
  48. assert(pst);
  49. if (pst->_top == pst->_capacity)
  50. {
  51. STDataType newcapacity = pst->_capacity == 0 ? 4 : (pst->_capacity * 2);
  52. STDataType* temp = (STDataType*)realloc(pst->_a, sizeof(STDataType) * newcapacity);
  53. if (temp == NULL)
  54. {
  55. printf("realloc fail\n");
  56. exit(-1);
  57. }
  58. pst->_a = temp;
  59. pst->_capacity = newcapacity;
  60. }
  61. pst->_a[pst->_top] = x;
  62. pst->_top++;
  63. }
  64. //出栈
  65. void StackPop(Stack* pst)
  66. {
  67. assert(pst);
  68. assert(pst->_top > 0);
  69. pst->_top--;
  70. }
  71. //获取栈顶元素
  72. STDataType StackTop(Stack* pst)
  73. {
  74. assert(pst);
  75. assert(pst->_top>0);
  76. return pst->_a[pst->_top-1];
  77. }
  78. //获取栈的有效元素个数
  79. int StackSize(Stack* pst)
  80. {
  81. assert(pst);
  82. return pst->_top;
  83. }
  84. //判断栈是否为空,是返回1,非空返回0
  85. bool StackEmpty(Stack* pst)
  86. {
  87. assert(pst);
  88. if (pst->_top == 0)
  89. return true;
  90. else
  91. return false;
  92. }
  93. //打印栈
  94. void StackPrint(Stack* pst)
  95. {
  96. while (!StackEmpty(pst))
  97. {
  98. printf("%d\n", StackTop(pst));
  99. StackPop(pst);
  100. }
  101. }
  102. //销毁栈
  103. void StackDestory(Stack* pst)
  104. {
  105. assert(pst);
  106. free(pst->_a);
  107. pst->_a = NULL;
  108. pst->_top = pst->_capacity = 0;
  109. }
  110. //队列先进先出,栈先进后出
  111. typedef struct {
  112. Stack st1;
  113. Stack st2;
  114. } MyQueue;
  115. MyQueue* myQueueCreate() {
  116. MyQueue* pq=(MyQueue*)malloc(sizeof(MyQueue));
  117. if(pq==NULL)
  118. return NULL;
  119. StackInit(&pq->st1);
  120. StackInit(&pq->st2);
  121. return pq;
  122. }
  123. void myQueuePush(MyQueue* obj, int x) {
  124. if(StackEmpty(&obj->st1))
  125. {
  126. StackPush(&obj->st2,x);
  127. }
  128. if(StackEmpty(&obj->st2))
  129. {
  130. StackPush(&obj->st1,x);
  131. }
  132. }
  133. int myQueuePop(MyQueue* obj) {
  134. if(StackEmpty(&obj->st1))
  135. {
  136. while((&obj->st2)->_top!=1){
  137. StackPush(&obj->st1,StackTop(&obj->st2));
  138. StackPop(&obj->st2);
  139. }
  140. data=StackTop(&obj->st2);
  141. StackPop(&obj->st2);
  142. while((&obj->st1)->_top!=0){
  143. StackPush(&obj->st2,StackTop(&obj->st1));
  144. StackPop(&obj->st1);
  145. }
  146. return data;
  147. }
  148. else if(StackEmpty(&obj->st2))
  149. {
  150. while((&obj->st1)->_top!=1){
  151. StackPush(&obj->st2,StackTop(&obj->st1));
  152. StackPop(&obj->st1);
  153. }
  154. data=StackTop(&obj->st1);
  155. StackPop(&obj->st1);
  156. while((&obj->st2)->_top!=0){
  157. StackPush(&obj->st1,StackTop(&obj->st2));
  158. StackPop(&obj->st2);
  159. }
  160. }
  161. return data;
  162. }
  163. int myQueuePeek(MyQueue* obj) {
  164. if(StackEmpty(&obj->st1))
  165. {
  166. while((&obj->st2)->_top!=0){
  167. StackPush(&obj->st1,StackTop(&obj->st2));
  168. StackPop(&obj->st2);
  169. }
  170. data=StackTop(&obj->st1);
  171. while((&obj->st1)->_top!=0){
  172. StackPush(&obj->st2,StackTop(&obj->st1));
  173. StackPop(&obj->st1);}
  174. return data;
  175. }
  176. if(StackEmpty(&obj->st2))
  177. {
  178. while((&obj->st1)->_top!=0){
  179. StackPush(&obj->st2,StackTop(&obj->st1));
  180. StackPop(&obj->st1);
  181. }
  182. data=StackTop(&obj->st2);
  183. while((&obj->st2)->_top!=0){
  184. StackPush(&obj->st1,StackTop(&obj->st2));
  185. StackPop(&obj->st2);
  186. }
  187. }
  188. return data;
  189. }
  190. bool myQueueEmpty(MyQueue* obj) {
  191. if(StackEmpty(&obj->st1)&&StackEmpty(&obj->st2))
  192. return true;
  193. return false;
  194. }
  195. void myQueueFree(MyQueue* obj) {
  196. StackDestory(&obj->st1);
  197. StackDestory(&obj->st2);
  198. free(obj);
  199. obj=NULL;
  200. }
  201. /**
  202. * Your MyQueue struct will be instantiated and called as such:
  203. * MyQueue* obj = myQueueCreate();
  204. * myQueuePush(obj, x);
  205. * int param_2 = myQueuePop(obj);
  206. * int param_3 = myQueuePeek(obj);
  207. * bool param_4 = myQueueEmpty(obj);
  208. * myQueueFree(obj);
  209. */

 

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

闽ICP备14008679号