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


    1. #include "stdio.h"
    2. #include "stdbool.h"
    3. #include "string.h"
    4. #include "stdlib.h"
    5. #include "assert.h"
    6. //初始化队列
    7. typedef int QueueDataType;
    8. typedef struct queue {
    9. QueueDataType val;
    10. struct queue* next;
    11. }Qnode;
    12. typedef struct Queue {
    13. Qnode* phead;//队头
    14. Qnode* ptail;//队尾
    15. int size;//队列中的有效数据
    16. }Queue;
    17. //初始化队
    18. void QueueInit(Queue* pq);
    19. //入队
    20. void QueuePush(Queue* pq,QueueDataType x);
    21. //出队
    22. void QueuePop(Queue* pq);
    23. //获取队首元素
    24. QueueDataType QueueFront(Queue* pq);
    25. //获取队尾元素
    26. QueueDataType QueueBack(Queue* pq);
    27. //获取队列有效个数
    28. int QueueSize(Queue* pq);
    29. //判断队列是否为空,为空返回1,非空返回0
    30. bool QueueEmpty(Queue* pq);
    31. //打印队列
    32. void QueuePrint(Queue* pq);
    33. //销毁队列
    34. void QueueDestory(Queue* pq);
    35. #define _CRT_SECURE_NO_WARNINGS 1
    36. //初始化队
    37. void QueueInit(Queue* pq)
    38. {
    39. assert(pq);
    40. pq->size=0;
    41. pq->phead=pq->ptail=NULL;
    42. }
    43. //入队
    44. void QueuePush(Queue* pq, QueueDataType x)
    45. {
    46. assert(pq);
    47. assert(pq);
    48. Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
    49. if (newnode == NULL)
    50. {
    51. printf("malloc fail\n");
    52. exit(-1);
    53. }
    54. pq->size++;
    55. newnode->val = x;
    56. newnode->next = NULL;
    57. if (pq->phead == NULL)
    58. {
    59. pq->phead = pq->ptail = newnode;
    60. }
    61. else
    62. {
    63. pq->ptail->next = newnode;
    64. pq->ptail = newnode;
    65. }
    66. }
    67. //出队
    68. void QueuePop(Queue* pq) {
    69. assert(pq);
    70. Qnode* next = pq->phead->next;
    71. free(pq->phead);
    72. pq->size--;
    73. pq->phead = next;
    74. }
    75. //获取队首元素
    76. QueueDataType QueueFront(Queue* pq) {
    77. assert(pq);
    78. assert(pq->size > 0);
    79. assert(pq->phead);
    80. return pq->phead->val;
    81. }
    82. //获取队尾元素
    83. QueueDataType QueueBack(Queue* pq) {
    84. assert(pq);
    85. assert(pq->ptail);
    86. assert(pq->size > 0);
    87. return pq->ptail->val;
    88. }
    89. //获取队列有效个数
    90. int QueueSize(Queue* pq) {
    91. assert(pq);
    92. return pq->size;
    93. }
    94. //判断队列是否为空,为空返回1,非空返回0
    95. bool QueueEmpty(Queue* pq) {
    96. assert(pq);
    97. return pq->size == 0 ? 1 : 0;
    98. }
    99. //打印队列
    100. void QueuePrint(Queue* pq) {
    101. assert(pq);
    102. assert(pq->phead);
    103. while (!QueueEmpty(pq))
    104. {
    105. printf("%d\n", pq->phead->val);
    106. QueuePop(pq);
    107. }
    108. }
    109. //销毁队列
    110. void QueueDestory(Queue* pq) {
    111. assert(pq);
    112. Qnode* cur = pq->phead;
    113. while (cur) {
    114. Qnode* next = cur->next;
    115. free(cur);
    116. cur = next;
    117. }
    118. pq->phead = pq->ptail = NULL;
    119. }
    120. typedef struct {
    121. Queue pq1;
    122. Queue pq2;
    123. } MyStack;
    124. MyStack* myStackCreate() {
    125. MyStack* ST=(MyStack*)malloc(sizeof(MyStack));
    126. QueueInit(&ST->pq1);
    127. QueueInit(&ST->pq2);
    128. return ST;
    129. }
    130. void myStackPush(MyStack* obj, int x) {
    131. if(QueueEmpty(&obj->pq1))
    132. {
    133. QueuePush(&obj->pq2,x);
    134. }else{
    135. QueuePush(&obj->pq1,x);
    136. }
    137. }
    138. int myStackPop(MyStack* obj) {
    139. if(QueueEmpty(&obj->pq1)){
    140. while((&obj->pq2)->phead!=(&obj->pq2)->ptail)
    141. {
    142. QueuePush(&obj->pq1,QueueFront(&obj->pq2));
    143. QueuePop(&obj->pq2);
    144. }
    145. int vala=QueueFront(&obj->pq2);
    146. QueuePop(&obj->pq2);
    147. return vala;
    148. }
    149. if(QueueEmpty(&obj->pq2)){
    150. while((&obj->pq1)->phead!=(&obj->pq1)->ptail)
    151. {
    152. QueuePush(&obj->pq2,QueueFront(&obj->pq1));
    153. QueuePop(&obj->pq1);
    154. }
    155. }
    156. int valb=QueueFront(&obj->pq1);
    157. QueuePop(&obj->pq1);
    158. return valb;
    159. }
    160. int myStackTop(MyStack* obj) {
    161. if(QueueEmpty(&obj->pq1)){
    162. return (&obj->pq2)->ptail->val;
    163. }
    164. return (&obj->pq1)->ptail->val;
    165. }
    166. bool myStackEmpty(MyStack* obj) {
    167. if(QueueEmpty(&obj->pq1)&&QueueEmpty(&obj->pq2)){
    168. return true;
    169. }
    170. return false;
    171. }
    172. void myStackFree(MyStack* obj) {
    173. Qnode* cur1 = (&obj->pq1)->phead;
    174. while(cur1){
    175. Qnode* next=cur1->next;
    176. free(cur1);
    177. cur1=next;
    178. }
    179. (&obj->pq1)->phead=(&obj->pq1)->ptail=NULL;
    180. Qnode* cur2 = (&obj->pq2)->phead;
    181. while(cur2){
    182. Qnode* next=cur2->next;
    183. free(cur2);
    184. cur2=next;
    185. }
    186. (&obj->pq2)->phead=(&obj->pq2)->ptail=NULL;
    187. (&obj->pq2)->phead=(&obj->pq2)->ptail=NULL;
    188. free(obj);
    189. obj=NULL;
    190. }
    191. /**
    192. * Your MyStack struct will be instantiated and called as such:
    193. * MyStack* obj = myStackCreate();
    194. * myStackPush(obj, x);
    195. * int param_2 = myStackPop(obj);
    196. * int param_3 = myStackTop(obj);
    197. * bool param_4 = myStackEmpty(obj);
    198. * myStackFree(obj);
    199. */

     

  • 相关阅读:
    开源WIFI继电器之方案介绍
    前端实战|React18极客园——编辑文章模块(文案适配、回显数据)
    [问题记录.诡异的循环文件夹“...“]名称为三个点的文件夹
    并查集与最小生成树
    Weblogic(CVE-2017-10271)与 Struts2(s2-045) 反序列化漏洞复现
    移植BusyBox根文件系统到野火开发板
    Command SwiftCompile failed with a nonzero exit code
    Redis入门完整教程:Redis Shell
    Tomcat生产部署多个应用
    Visual Prompt Tuning 笔记
  • 原文地址:https://blog.csdn.net/2301_79819250/article/details/137979408