深圳幻海软件技术有限公司 欢迎您!

【LeetCode训练营】用栈来实现队列+用队列来实现栈 详解

2023-05-23

💯 博客内容:【LeetCode训练营】用栈来实现队列+用队列来实现栈详解😀作者:陈大大陈🚀个人简介:一个正在努力学技术的准前端,专注基础和实战分享,欢迎私信!💖欢迎大家:这里是CSDN,我总结知识和写笔记的地方,喜欢的话请三连,有问题请私信😘😘😘目录用栈实现队列 
  • 💯 博客内容:【LeetCode训练营】用栈来实现队列+用队列来实现栈 详解

  • 😀 作  者:陈大大陈

  • 🚀 个人简介:一个正在努力学技术的准前端,专注基础和实战分享 ,欢迎私信!

  • 💖 欢迎大家:这里是CSDN,我总结知识和写笔记的地方,喜欢的话请三连,有问题请私信 😘 😘 😘

目录

用栈实现队列 

思路分享

源码 

用队列实现栈

 思路分享

源码 

后记

用栈实现队列 

232. 用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

说明:

  • 你 只能 使用标准的栈操作 —— 也就是只有 push to toppeek/pop from topsize, 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

示例 1:

输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]

解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

提示:

  • 1 <= x <= 9
  • 最多调用 100 次 pushpoppeek 和 empty
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)

进阶:

  • 你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。

思路分享

栈是先进后出的,但是队列是先进先出。

用栈实现队列,就是在俩个栈之间不断倒元素来实现队列的先进先出。

我们先用一个栈来存入元素(这时最先进入的元素在栈底),然后再将第一个栈中的元素移动到新栈中,此时最先进入的元素就在栈顶了。

之后我们再用第二个栈出栈时,整个执行的顺序就变成了先进先出。

定义一个Pushst专门用来入栈,一个Popst专门用来出栈。

需要注意的是每次栈Popst出栈时都要把所有的元素都出完之后,才能从Pushst中追加新数据,当Popst的数据没有全部出栈完成时,不能将Pushst的元素入栈到Popst,这样会导致元素的执行顺序混乱。

源码 

  1. #include<stdlib.h>
  2. #include<assert.h>
  3. #include<stdio.h>
  4. #include<stdbool.h>
  5. typedef int STDataType;
  6. typedef struct Stack
  7. {
  8. STDataType* a;
  9. int top;
  10. int capacity;
  11. }ST;
  12. void STInit(ST* pst);
  13. void STPush(ST* pst, STDataType x);
  14. void STPop(ST* pst);
  15. void STDestroy(ST* pst);
  16. STDataType STTop(ST* pst);
  17. bool STEmpty(ST* pst);
  18. void STInit(ST* pst)
  19. {
  20. assert(pst);
  21. pst->a = NULL;
  22. pst->top = 0;//top指向栈顶数据的下一个位置
  23. //pst->top=-1;top指向栈顶数据
  24. pst->capacity = 0;
  25. }
  26. void STDestroy(ST* pst)
  27. {
  28. assert(pst);
  29. free(pst->a);
  30. pst->a = NULL;
  31. pst->top = 0;
  32. pst->capacity = 0;
  33. }
  34. bool STEmpty(ST* pst)
  35. {
  36. return pst->top == 0;
  37. }
  38. void STPush(ST* pst, STDataType x)
  39. {
  40. if (pst->capacity == pst->top)
  41. {
  42. int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
  43. STDataType* tmp = (STDataType*)realloc(pst->a, sizeof(STDataType) * newcapacity);
  44. pst->a = tmp;
  45. pst->capacity = newcapacity;
  46. }
  47. pst->a[pst->top] = x;
  48. pst->top++;
  49. }
  50. void STPop(ST* pst)
  51. {
  52. assert(pst);
  53. assert(!STEmpty(pst));
  54. pst->top--;
  55. }
  56. STDataType STTop(ST* pst)
  57. {
  58. assert(pst);
  59. assert(!STEmpty(pst));
  60. return pst->a[pst->top - 1];
  61. }
  62. int STSize(ST* pst)
  63. {
  64. assert(pst);
  65. assert(!(STEmpty(pst)));
  66. return pst->top;
  67. }
  68. typedef struct {
  69. ST Pushst;
  70. ST Popst;
  71. } MyQueue;
  72. MyQueue* myQueueCreate() {
  73. MyQueue * obj=(MyQueue*)malloc(sizeof(MyQueue));
  74. if(obj==NULL)
  75. {
  76. perror("malloc fail");
  77. return NULL;
  78. }
  79. STInit(&obj->Pushst);
  80. STInit(&obj->Popst);
  81. return obj;
  82. }
  83. bool myQueueEmpty(MyQueue* obj) {
  84. return STEmpty(&obj->Popst)&&STEmpty(&obj->Pushst);
  85. }
  86. void myQueuePush(MyQueue* obj, int x) {
  87. assert(&obj);
  88. STPush(&obj->Pushst,x);
  89. }
  90. int myQueuePop(MyQueue* obj) {
  91. int front=myQueuePeek(obj);
  92. STPop(&obj->Popst);
  93. return front;
  94. }
  95. int myQueuePeek(MyQueue* obj) {
  96. if(STEmpty(&obj->Popst))
  97. {
  98. while(!STEmpty(&obj->Pushst))
  99. {
  100. STPush(&obj->Popst,STTop(&obj->Pushst));
  101. STPop(&obj->Pushst);
  102. }
  103. }
  104. return STTop(&obj->Popst);
  105. }
  106. void myQueueFree(MyQueue* obj) {
  107. STDestroy(&obj->Pushst);
  108. STDestroy(&obj->Popst);
  109. free(obj);
  110. }
  111. /**
  112. * Your MyQueue struct will be instantiated and called as such:
  113. * MyQueue* obj = myQueueCreate();
  114. * myQueuePush(obj, x);
  115. * int param_2 = myQueuePop(obj);
  116. * int param_3 = myQueuePeek(obj);
  117. * bool param_4 = myQueueEmpty(obj);
  118. * myQueueFree(obj);
  119. */

用队列实现栈

 思路分享

队列是先进先出,栈是先进后出。

先进先出的特性决定了当一个队列向另一个队列倒元素时,被导入的元素顺序不变。

原先的队列是什么样子,被导入的另一个队列就是什么样子。

那么,删除元素的思路就很明显了,将队列1中的数据挨个导入队列2,直到队列1剩下最后一个元素。

我们知道,这个元素实际上是队尾元素,而对应到栈就是栈顶元素。

我们将其删除,就模拟实现了出栈操作。 

入栈操作就更为简单,进入不为空的那个队列,不然会影响数据的排列顺序。

源码 

  1. typedef int QDataType;
  2. typedef struct QueueNode
  3. {
  4. struct QueueNode* next;
  5. QDataType data;
  6. }Node;
  7. typedef struct Queue
  8. {
  9. Node* phead;
  10. Node* ptail;
  11. int size;
  12. }Queue;
  13. bool QueueEmpty(Queue* pq)
  14. {
  15. assert(pq);
  16. //return pq->phead == NULL && pq->ptail == NULL;
  17. return pq->size==0;
  18. }
  19. void QueueInit(Queue* pq)
  20. {
  21. assert(pq);
  22. pq->phead = NULL;
  23. pq->ptail = NULL;
  24. pq->size = 0;
  25. }
  26. void QueueDestroy(Queue* pq)
  27. {
  28. assert(pq);
  29. Node* cur = pq->phead;
  30. while (cur!=NULL)
  31. {
  32. Node* next = cur->next;
  33. free(cur);
  34. cur = next;
  35. }
  36. pq->phead = pq->ptail = NULL;
  37. pq->size = 0;
  38. }
  39. void QueuePush(Queue* pq, QDataType x)
  40. {
  41. assert(pq);
  42. Node* newnode = (Node*)malloc(sizeof(Node));
  43. if (newnode == NULL)
  44. {
  45. perror("malloc fail\n");
  46. return;
  47. }
  48. newnode->data = x;
  49. newnode->next = NULL;
  50. if (pq->ptail == NULL)
  51. {
  52. assert(pq->phead==NULL);
  53. pq->phead = pq->ptail = newnode;
  54. }
  55. else
  56. {
  57. pq->ptail->next = newnode;
  58. pq->ptail = newnode;
  59. }
  60. pq->size++;
  61. }
  62. void QueuePop(Queue* pq)
  63. {
  64. assert(pq);
  65. assert(!QueueEmpty(pq));//也可以直接用phead
  66. if (pq->phead->next==NULL)
  67. {
  68. free(pq->phead);
  69. pq->phead = pq->ptail = NULL;
  70. }
  71. else
  72. {
  73. Node* next = pq->phead->next;
  74. free(pq->phead);
  75. pq->phead = next;
  76. }
  77. pq->size--;
  78. }
  79. QDataType QueueFront(Queue* pq)
  80. {
  81. assert(pq);
  82. assert(!QueueEmpty(pq));
  83. return pq->phead->data;
  84. }
  85. QDataType QueueBack(Queue* pq)
  86. {
  87. assert(pq);
  88. assert(!QueueEmpty(pq));
  89. return pq->ptail->data;
  90. }
  91. int QueueSize(Queue* pq)
  92. {
  93. assert(pq);
  94. return pq->size;
  95. }
  96. typedef struct {
  97. Queue q1;
  98. Queue q2;
  99. } MyStack;
  100. MyStack* myStackCreate() {
  101. MyStack *obj=(MyStack*)malloc(sizeof(MyStack));
  102. QueueInit(&obj->q1);
  103. QueueInit(&obj->q2);
  104. return obj;
  105. }
  106. void myStackPush(MyStack* obj, int x) {
  107. if(!QueueEmpty(&obj->q1))
  108. {
  109. QueuePush(&obj->q1,x);
  110. }
  111. else
  112. {
  113. QueuePush(&obj->q2,x);
  114. }
  115. }
  116. int myStackPop(MyStack* obj) {
  117. Node*pEmptyQ=&obj->q1;
  118. Node*pNonEmptyQ=&obj->q2;
  119. if(!QueueEmpty(&obj->q1))
  120. {
  121. pEmptyQ=&obj->q2;
  122. pNonEmptyQ=&obj->q1;
  123. }
  124. while(QueueSize(pNonEmptyQ)>1)
  125. {
  126. QueuePush(pEmptyQ,QueueFront(pNonEmptyQ));
  127. QueuePop(pNonEmptyQ);
  128. }
  129. int top=QueueFront(pNonEmptyQ);
  130. QueuePop(pNonEmptyQ);
  131. return top;
  132. }
  133. int myStackTop(MyStack* obj) {
  134. if(!QueueEmpty(&obj->q1))
  135. {
  136. return QueueBack(&obj->q1);
  137. }
  138. else{
  139. return QueueBack(&obj->q2);
  140. }
  141. }
  142. bool myStackEmpty(MyStack* obj) {
  143. return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
  144. }
  145. void myStackFree(MyStack* obj) {
  146. QueueDestroy(&obj->q1);
  147. QueueDestroy(&obj->q2);
  148. free(obj);
  149. }
  150. /**
  151. * Your MyStack struct will be instantiated and called as such:
  152. * MyStack* obj = myStackCreate();
  153. * myStackPush(obj, x);
  154. * int param_2 = myStackPop(obj);
  155. * int param_3 = myStackTop(obj);
  156. * bool param_4 = myStackEmpty(obj);
  157. * myStackFree(obj);
  158. */

后记

哎,成为大牛任重而道远,小僧仍需继续努力。。。

各位施主共勉。。。 

 

文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览46879 人正在系统学习中