• 力扣用队列实现栈


    自己写的栈,再让其他函数去调用自己写的栈

    1. typedef int QDataType;
    2. typedef struct QueueNode
    3. {
    4. struct QueueNode* next;//单链表
    5. QDataType data;//放数据
    6. }QNode;
    7. typedef struct Queue
    8. {
    9. QNode* phead;//头节点
    10. QNode* ptail;//尾节点
    11. QDataType size; //统计有多少节点
    12. }Queue;
    13. void QueueInit(Queue *pq);//初始化
    14. void QueueDestroy(Queue*pq);//销毁
    15. void QueuePush(Queue*pq,QDataType x);//插入数据
    16. void QueuePop(Queue* pq);//队头出数据就是删队头
    17. QDataType QueueFront(Queue* pq);//返回对头数据
    18. QDataType QueueBack(Queue* pq);//返回队尾数据
    19. int QueueSize(Queue* pq);//返回总的数据个数
    20. bool QueueEmpty(Queue* pq);//是空的返回真
    21. void QueueInit(Queue* pq)//初始化
    22. {
    23. assert(pq);
    24. pq->phead = NULL;
    25. pq->ptail = NULL;
    26. pq->size = 0;
    27. }
    28. void QueueDestroy(Queue* pq)//销毁
    29. {
    30. assert(pq);
    31. //第一个结构体
    32. QNode* cur = pq->phead;
    33. while (cur)
    34. {
    35. QNode* next = cur->next;
    36. free(cur);
    37. cur = next;
    38. }
    39. pq->phead = NULL;
    40. pq->ptail = NULL;
    41. pq->size = 0;
    42. }
    43. void QueuePush(Queue* pq, QDataType x)//插入数据
    44. {
    45. QNode* newnode = (QNode*)malloc(sizeof(QNode));//扩大的是第一个结构体
    46. if (newnode == NULL)
    47. {
    48. perror("malloc");
    49. return;
    50. }
    51. newnode->data = x;
    52. newnode->next = NULL;
    53. if (pq->phead == NULL)
    54. {
    55. pq->phead = pq->ptail = newnode;
    56. }
    57. else
    58. {
    59. pq->ptail->next = newnode;
    60. pq->ptail = newnode;
    61. }
    62. pq->size++;
    63. }
    64. void QueuePop(Queue* pq)//队头出数据就是删队头
    65. {
    66. assert(pq);
    67. assert(!QueueEmpty(pq));
    68. if (pq->phead->next == NULL)
    69. {
    70. free(pq->phead);
    71. pq->phead = pq->ptail = NULL;
    72. }
    73. else
    74. {
    75. QNode* next = pq->phead->next;
    76. free(pq->phead);
    77. pq->phead = next;
    78. }
    79. pq->size--;
    80. }
    81. QDataType QueueFront(Queue* pq)//返回对头数据
    82. {
    83. assert(pq);
    84. assert(!QueueEmpty(pq));
    85. return pq->phead->data;
    86. }
    87. QDataType QueueBack(Queue* pq)//返回队尾数据
    88. {
    89. assert(pq);
    90. assert(!QueueEmpty(pq));
    91. return pq->ptail->data;
    92. }
    93. int QueueSize(Queue* pq)//返回总的数据个数
    94. {
    95. assert(pq);
    96. return pq->size;
    97. }
    98. bool QueueEmpty(Queue* pq)//是空的返回真
    99. {
    100. assert(pq);
    101. return pq->phead == NULL && pq->ptail == NULL;
    102. }

    1.一个结构体包含俩个队列

    1. typedef struct {
    2. Queue q1;//第一个队列
    3. Queue q2;//第二个队列
    4. } MyStack;

    2.希望创造一个包含两个队列的结构体,并且把这样的结构返回去,通过MyStack结构体一把molloc两个队列结构体

    1. MyStack* myStackCreate() {
    2. MyStack*obj = (MyStack*)malloc(sizeof(MyStack));
    3. if(obj == NULL)
    4. {
    5. perror("molloc");
    6. }
    7. QueueInit(&obj->q1);
    8. QueueInit(&obj->q2);
    9. return obj;
    10. }

    3.插入数据,q1队列有数据就进入(if语句)把新数据插入q1队列,q1队列没数据就把新数据插入q2队列,若是两个队列都是空就把新数据随便入一个队列

    1. //插入数据
    2. void myStackPush(MyStack* obj, int x) {
    3. if(!QueueEmpty(&obj->q1))
    4. {
    5. QueuePush(&obj->q1, x);
    6. }
    7. else
    8. {
    9. QueuePush(&obj->q2, x);
    10. }
    11. }

    4.用两个队列像一个要像一个栈一样出数据取数据,把q1队列的数据倒q2,倒到了最后一个元素再去返回和删除元素

    1. //移除并返回栈顶元素
    2. int myStackPop(MyStack* obj) {
    3. //空数据队列
    4. Queue* PEmptyQ = &obj->q1;
    5. //有数据队列
    6. Queue* PNonEmptyQ = &obj->q2;
    7. //假设空数据和有数据队列假设赋值错了
    8. if(!QueueEmpty(&obj->q1))
    9. {
    10. PEmptyQ = &obj->q2;
    11. PNonEmptyQ = &obj->q1;
    12. }
    13. //倒数据
    14. while(QueueSize(PNonEmptyQ) > 1)
    15. {
    16. QueuePush(PEmptyQ,QueueFront(PNonEmptyQ));
    17. QueuePop(PNonEmptyQ);
    18. }
    19. int top = QueueFront(PNonEmptyQ);//调用了取对头数据函数
    20. QueuePop(PNonEmptyQ);//调用了删除队头数据函数
    21. return top;
    22. }

     5.返回栈顶数据,就是返回队列的队尾数据
    1. //取栈顶元素
    2. int myStackTop(MyStack* obj) {
    3. if(!QueueEmpty(&obj->q1))
    4. {
    5. return QueueBack(&obj->q1);//调用了取队尾的数据的函数
    6. }
    7. else
    8. {
    9. return QueueBack(&obj->q2);//调用了取队尾的数据的函数
    10. }
    11. }

    6.判创建的MyStack结构体q1和q2地址是不是空,如果是空的则标题2创建结构体失败了molloc也失败了
    1. bool myStackEmpty(MyStack* obj) {
    2. return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
    3. }

    7.程序结束销毁所有建立的空间避免内存泄漏

    1. //释放数据
    2. void myStackFree(MyStack* obj) {
    3. QueueDestroy(&obj->q1);
    4. QueueDestroy(&obj->q2);
    5. free(obj);
    6. }

  • 相关阅读:
    csp-202203(在更)
    码蹄集oj赛第25周(cup,查询,捕鱼,射线的交,平衡)
    在flutter中添加video_player【视频播放插件】
    【Java 进阶篇】HTML DOM 事件详解
    【SpringBoot】YAML 配置文件
    公众号配置调试“errMsg“:“config:fail,invalid signature
    一个c程序的内存分布
    快手本地生活服务商系统怎么操作?
    食品饮料行业数智化采购管理系统规范供应商管理,完善企业协同体系
    60 权限提升-MY&MS&ORA等SQL数据库提权
  • 原文地址:https://blog.csdn.net/m0_57383688/article/details/133430233