• 【力扣+C语言】225.用队列实现栈



    目录

    1.力扣 225.用队列实现栈

    1.1.题目

    1.2.题目分析

    1.2.1.题目思路分析:

    1.2.2.准备过程:创建结构体+实现函数myStackCreate

     1.2.3实现push函数,将元素 x 压入栈顶。

    1.2.4.实现pop函数:移除并返回栈顶元素

    1.2.5实现top函数: 返回栈顶元素

    1.2.6.实现empty函数:判断栈是否为空 

    1.2.7实现free函数,释放空间

    1.3.题目答案

    最后


    推荐阅读顺序:

    1.题目->3.答案->2.题目分析->4.题目知识点


    1.力扣 225.用队列实现栈

    225. 用队列实现栈 - 力扣(LeetCode)https://leetcode.cn/problems/implement-stack-using-queues/


    1.1.题目

    请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。

    实现 MyStack 类:

    void push(int x) 将元素 x 压入栈顶。
    int pop() 移除并返回栈顶元素。
    int top() 返回栈顶元素。
    boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
     

    注意:

    你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
    你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。


    1.2.题目分析

    由于C语言中没有对应的队列函数可以使用,就需要我们自己手动实现队列。详情可移步我的这篇博文:

    (6条消息) 【数据结构】队列、环形队列_vpurple__的博客-CSDN博客https://blog.csdn.net/vpurple_/article/details/126200057?spm=1001.2014.3001.5501


    1.2.1.题目思路分析:

    栈和队列特点对比:

    栈:后进先出

    队列:先进先出

    在使用两个队列模拟栈时,可以采用始终保持一个栈为空的思路,在进数据的时候就只往非空的那个队列进数据,在出数据的时候就把两个队列中的内容倒一下。本题目共需要实现4个主体函数:

    分别为:

    void push(int x) 将元素 x 压入栈顶。
    int pop() 移除并返回栈顶元素。
    int top() 返回栈顶元素。
    boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。


    1.2.2.准备过程:创建结构体+实现函数myStackCreate

    根据之前队列的定义,在MyStack中定义两个队列:

    1. typedef struct {
    2. Queue q1;
    3. Queue q2;
    4. } MyStack;

    实现myStackCreate函数:

    易错点:

    野指针问题

     正确写法:


     1.2.3实现push函数,将元素 x 压入栈顶。


    1.2.4.实现pop函数:移除并返回栈顶元素


    1.2.5实现top函数: 返回栈顶元素


    1.2.6.实现empty函数:判断栈是否为空 

    1.2.7实现free函数,释放空间


    1.3.题目答案

    1. typedef int QDataType;
    2. //用单链表实现队列
    3. // 链式结构:表示队列
    4. typedef struct QListNode
    5. {
    6. struct QListNode* next;
    7. QDataType data;
    8. }QNode;
    9. // 队列的结构
    10. typedef struct Queue
    11. {
    12. QNode* front;
    13. QNode* rear;
    14. }Queue;
    15. // 初始化队列
    16. void QueueInit(Queue* q)
    17. {
    18. assert(q);
    19. q->front =q->rear= NULL;
    20. }
    21. // 检测队列是否为空,如果为空返回非零结果,如果非空返回0
    22. bool QueueEmpty(Queue* q)
    23. {
    24. assert(q);
    25. return q->front == NULL&&q->rear==NULL;
    26. }
    27. QNode* AddNode(QDataType data)
    28. {
    29. QNode* p = (QNode*)malloc(sizeof(QNode));
    30. if (p == NULL)
    31. {
    32. perror("malloc error\n");
    33. exit(-1);
    34. }
    35. p->data = data;
    36. //队列申请新节点next要置为空
    37. p->next = NULL;
    38. return p;
    39. }
    40. //从队尾添加数据
    41. // 队尾入队列
    42. void QueuePush(Queue* q, QDataType data)
    43. {
    44. assert(q);
    45. if (q->front == NULL)
    46. {
    47. q->front = q->rear = AddNode(data);
    48. }
    49. else
    50. {
    51. QNode* tail = AddNode(data);
    52. q->rear->next = tail;
    53. q->rear = tail;
    54. q->rear->next = NULL;
    55. }
    56. }
    57. // 队头出队列
    58. void QueuePop(Queue* q)
    59. {
    60. assert(q);
    61. assert(q->front);
    62. //如果删除的是最后一个
    63. if (q->front->next == NULL)
    64. {
    65. free(q->front);
    66. q->front = q->rear = NULL;
    67. }
    68. QNode* head = q->front;
    69. //
    70. //
    71. if(q->front!=NULL)
    72. {
    73. q->front = q->front->next;
    74. }
    75. free(head);
    76. head = NULL;
    77. }
    78. // 获取队列头部元素
    79. QDataType QueueFront(Queue* q)
    80. {
    81. assert(q);
    82. assert( !QueueEmpty(q));
    83. return q->front->data;
    84. }
    85. // 获取队列队尾元素
    86. QDataType QueueBack(Queue* q)
    87. {
    88. assert(q);
    89. assert(!QueueEmpty(q));
    90. return q->rear->data;
    91. }
    92. // 获取队列中有效元素个数
    93. int QueueSize(Queue* q)
    94. {
    95. assert(q);
    96. int i = 0;
    97. QNode* head = q->front;
    98. while (head)
    99. {
    100. i++;
    101. head = head->next;
    102. }
    103. return i;
    104. }
    105. // 销毁队列
    106. void QueueDestroy(Queue* q)
    107. {
    108. assert(q);
    109. assert(QueueEmpty);
    110. QNode* head = q->front;
    111. QNode* next = NULL;
    112. while (head)
    113. {
    114. next = head->next;
    115. free(head);
    116. head=next;
    117. }
    118. q->front = q->rear = NULL;//销毁队列之后记得把头尾指针置为空
    119. }
    120. typedef struct {
    121. Queue q1;
    122. Queue q2;
    123. } MyStack;
    124. MyStack* myStackCreate() {
    125. MyStack* St=(MyStack*)malloc(sizeof(MyStack));
    126. QueueInit(&St->q1);
    127. QueueInit(&St->q2);
    128. return St;
    129. }
    130. void myStackPush(MyStack* obj, int x) {
    131. if(!QueueEmpty(&obj->q1))
    132. {
    133. QueuePush(&obj->q1,x);
    134. }
    135. else
    136. {
    137. QueuePush(&obj->q2,x);
    138. }
    139. }
    140. int myStackPop(MyStack* obj) {
    141. Queue* empty=&obj->q1;
    142. Queue* unempty=&obj->q2;
    143. if( !QueueEmpty(&obj->q1))
    144. {
    145. empty=&obj->q2;
    146. unempty=&obj->q1;
    147. }
    148. while(QueueSize(unempty)>1)
    149. {
    150. QueuePush(empty,QueueFront(unempty));
    151. QueuePop(unempty);
    152. }
    153. int pop=QueueFront(unempty);
    154. QueuePop(unempty);
    155. return pop;
    156. }
    157. int myStackTop(MyStack* obj) {
    158. Queue* empty=&obj->q1;
    159. Queue* unempty=&obj->q2;
    160. if( !QueueEmpty(&obj->q1))
    161. {
    162. empty=&obj->q2;
    163. unempty=&obj->q1;
    164. }
    165. return QueueBack(unempty);
    166. }
    167. bool myStackEmpty(MyStack* obj) {
    168. if(QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2))
    169. {
    170. return true;
    171. }
    172. return false;
    173. }
    174. void myStackFree(MyStack* obj) {
    175. QueueDestroy(&obj->q1);
    176. QueueDestroy(&obj->q2);
    177. free(obj);
    178. }
    179. /**
    180. * Your MyStack struct will be instantiated and called as such:
    181. * MyStack* obj = myStackCreate();
    182. * myStackPush(obj, x);
    183. * int param_2 = myStackPop(obj);
    184. * int param_3 = myStackTop(obj);
    185. * bool param_4 = myStackEmpty(obj);
    186. * myStackFree(obj);
    187. */

    最后

    大家好这里是媛仔!希望这篇解答对你可以有所帮助,和小狗狗一起祝你天天开心哦~

  • 相关阅读:
    python3
    微信小程序怎么隐藏顶部导航栏(navigationBar)变透明的解决方案
    C++ 递增/递减运算符重载
    py5_重要的缩进以及初识 Python 函数
    南大通用数据库-Gbase-8a-学习-19-Gbase8a从Kafka订阅Topic消费数据
    CSAPP Lab08——Proxy Lab完成思路
    Windows中睡眠和休眠的区别
    已知中序遍历数组和先序遍历数组,返回后序遗历数组
    2.6、物理层-真题
    LeetCode两个链表的第一个重合节点
  • 原文地址:https://blog.csdn.net/vpurple_/article/details/126491563