0


【Leetcode】队列实现栈和栈实现队列

一.【Leetcode225】队列实现栈

1.链接

队列实现栈

2.题目再现

3.解法

这道题给了我们两个队列,要求去实现栈;

首先,我们要知道栈和队列的特征:

栈:后进先出,只能从栈顶入数据和出数据;

队列:先进先出,从队尾入数据,队头出数据;

根据这些特点,我们可以采用两边倒的方法来实现;

具体来说:

1.入栈时就是在不为空的队列插入数据,若两个队列都为空,就随便插入到一个队列中;

2.出栈时将不为空的队列的数据倒入为空的队列中,当不为空的队列就剩一个数据时,就停止向空队列倒数据,然后再删点那最后一个数据;

3.判空时,需要两个队列都为空,才算栈为空;

4.取栈顶元素即取不为空的队列的队尾元素,在取栈顶元素前要判断栈是否为空;

5.销毁栈时,要先销毁其中的两个队列,然后再销毁栈。

因为是用C语言实现的,所以得自己手搓个队列。

  1. typedef int Qdatatype;
  2. typedef struct QueueNode
  3. {
  4. struct QueeuNode* next;
  5. Qdatatype data;
  6. }QueueNode;
  7. typedef struct Queue
  8. {
  9. QueueNode* head;
  10. QueueNode* tail;
  11. }Queue;
  12. void Queueinit(Queue* pq)
  13. {
  14. assert(pq);
  15. pq->head = NULL;
  16. pq->tail = NULL;
  17. }
  18. void Queuedestroy(Queue* pq)
  19. {
  20. assert(pq);
  21. QueueNode* cur = pq->head;
  22. while (cur != pq->tail)
  23. {
  24. QueueNode* next = cur->next;
  25. free(cur);
  26. cur = next;
  27. }
  28. pq->head = pq->tail = NULL;
  29. }
  30. void Queuepush(Queue* pq, Qdatatype x)
  31. {
  32. assert(pq);
  33. QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
  34. if (newnode == NULL)
  35. {
  36. perror("malloc fail");
  37. exit(-1);
  38. }
  39. newnode->data = x;
  40. newnode->next = NULL;
  41. if (pq->head == NULL)
  42. {
  43. pq->head = pq->tail = newnode;
  44. }
  45. else
  46. {
  47. pq->tail->next = newnode;
  48. pq->tail = newnode;
  49. }
  50. }
  51. void Queuepop(Queue* pq)
  52. {
  53. assert(pq);
  54. assert(pq->head);
  55. QueueNode* next = pq->head->next;
  56. if (pq->head->next == NULL)
  57. {
  58. free(pq->head);
  59. pq->head = pq->tail = NULL;
  60. }
  61. else
  62. {
  63. free(pq->head);
  64. pq->head = next;
  65. }
  66. }
  67. Qdatatype Queuefront(Queue* pq)
  68. {
  69. assert(pq);
  70. assert(pq->head);
  71. return pq->head->data;
  72. }
  73. Qdatatype Queueback(Queue* pq)
  74. {
  75. assert(pq);
  76. assert(Queuesize(pq) > 0);
  77. return pq->tail->data;
  78. }
  79. int Queuesize(Queue* pq)
  80. {
  81. assert(pq);
  82. int size = 0;
  83. QueueNode* cur = pq->head;
  84. while (cur != pq->tail->next)
  85. {
  86. size++;
  87. cur = cur->next;
  88. }
  89. return size;
  90. }
  91. bool Queueempty(Queue* pq)
  92. {
  93. assert(pq);
  94. return pq->head == NULL;
  95. }
  96. typedef struct
  97. {
  98. Queue q1;
  99. Queue q2;
  100. } MyStack;
  101. MyStack* myStackCreate() {
  102. MyStack*obj=(MyStack*)malloc(sizeof(MyStack));
  103. if(obj==NULL)
  104. exit(-1);
  105. Queueinit(&obj->q1);
  106. Queueinit(&obj->q2);
  107. return obj;
  108. }
  109. void myStackPush(MyStack* obj, int x)
  110. {
  111. if(!Queueempty(&obj->q1))
  112. {
  113. Queuepush(&obj->q1,x);
  114. }
  115. else
  116. {
  117. Queuepush(&obj->q2,x);
  118. }
  119. }
  120. int myStackPop(MyStack* obj) {
  121. Queue*empty=&obj->q1;
  122. Queue*noempty=&obj->q2;
  123. if(!Queueempty(&obj->q1))
  124. {
  125. empty=&obj->q2;
  126. noempty=&obj->q1;
  127. }
  128. while(Queuesize(noempty)>1)
  129. {
  130. Queuepush(empty,Queuefront(noempty));
  131. Queuepop(noempty);
  132. }
  133. int front=Queuefront(noempty);
  134. Queuepop(noempty);
  135. return front;
  136. }
  137. int myStackTop(MyStack* obj) {
  138. if(!Queueempty(&obj->q1))
  139. {
  140. return Queueback(&obj->q1);
  141. }
  142. else
  143. {
  144. return Queueback(&obj->q2);
  145. }
  146. }
  147. bool myStackEmpty(MyStack* obj) {
  148. return Queueempty(&obj->q1)&&Queueempty(&obj->q2);
  149. }
  150. void myStackFree(MyStack* obj) {
  151. Queuedestroy(&obj->q1);
  152. Queuedestroy(&obj->q2);
  153. free(obj);
  154. }

二.【Leetcode232】栈实现队列

1.链接

栈实现队列

2.题目再现

3.解法

这个的解法和上面的类似,只不过这个不用总是来回倒;

根据栈和队列的特征,我们会发现将一个栈中的数据倒入另一个栈时,数据的顺序刚好符合队列的要求,不需要再重复地倒数据,所以我们可以让一个栈专门用来入数据(Pushst),一个栈专门用来出数据(Popst),当我们要出数据而这个栈为空时,我们才将用来入数据的栈中的数据倒入用来出数据的栈 。

如图:

1.判空时,需要两个栈都为空,队列才为空;

2.返回队头数据时,和出数据的操作类似,只是不需要删除队头的数据,还有在之前要判断队列是否为空;

3.销毁队列前,要先销毁两个栈。

同样,因为是C语言,得先手搓个栈。

  1. #define MR_CAP 5
  2. typedef int STdatatype;
  3. typedef struct Stack
  4. {
  5. STdatatype* arr;
  6. int top;
  7. int capacity;
  8. }ST;
  9. void Stackinit(ST* ps)
  10. {
  11. assert(ps);
  12. ps->arr = (STdatatype*)malloc(MR_CAP * sizeof(STdatatype));
  13. if (ps->arr == NULL)
  14. {
  15. perror("Stackinit malloc");
  16. exit(-1);
  17. }
  18. ps->top = 0;
  19. ps->capacity = MR_CAP;
  20. }
  21. void Stackdestroy(ST* ps)
  22. {
  23. assert(ps);
  24. free(ps->arr);
  25. ps->arr = NULL;
  26. ps->top = 0;
  27. ps->capacity = 0;
  28. }
  29. void Stackpush(ST* ps, STdatatype x)
  30. {
  31. assert(ps);
  32. if (ps->top == ps->capacity)
  33. {
  34. STdatatype* tmp = (STdatatype*)realloc(ps->arr, ps->capacity * 2 * sizeof(STdatatype));
  35. if (tmp == NULL)
  36. {
  37. perror("Stackpush realloc");
  38. exit(-1);
  39. }
  40. else
  41. {
  42. ps->arr = tmp;
  43. ps->capacity *= 2;
  44. }
  45. }
  46. ps->arr[ps->top] = x;
  47. ps->top++;
  48. }
  49. void Stackpop(ST* ps)
  50. {
  51. assert(ps);
  52. assert(ps->top > 0);
  53. ps->top--;
  54. }
  55. STdatatype Stacktop(ST* ps)
  56. {
  57. assert(ps);
  58. return ps->arr[ps->top - 1];
  59. }
  60. int Stacksize(ST* ps)
  61. {
  62. assert(ps);
  63. return ps->top;
  64. }
  65. bool Stackempty(ST* ps)
  66. {
  67. assert(ps);
  68. if (ps->top == 0)
  69. {
  70. return true;
  71. }
  72. else
  73. return false;
  74. }
  75. typedef struct {
  76. ST Pushst;
  77. ST Popst;
  78. } MyQueue;
  79. MyQueue* myQueueCreate() {
  80. MyQueue*obj=(MyQueue*)malloc(sizeof(MyQueue));
  81. if(obj==NULL)
  82. exit(-1);
  83. Stackinit(&obj->Pushst);
  84. Stackinit(&obj->Popst);
  85. return obj;
  86. }
  87. void myQueuePush(MyQueue* obj, int x) {
  88. Stackpush(&obj->Pushst,x);
  89. }
  90. int myQueuePeek(MyQueue* obj) {
  91. if(Stackempty(&obj->Popst))
  92. {
  93. while(!Stackempty(&obj->Pushst))
  94. {
  95. Stackpush(&obj->Popst,Stacktop(&obj->Pushst));
  96. Stackpop(&obj->Pushst);
  97. }
  98. }
  99. return Stacktop(&obj->Popst);
  100. }
  101. int myQueuePop(MyQueue* obj) {
  102. int front=myQueuePeek(obj);
  103. Stackpop(&obj->Popst);
  104. return front;
  105. }
  106. bool myQueueEmpty(MyQueue* obj) {
  107. return Stackempty(&obj->Pushst)&&Stackempty(&obj->Popst);
  108. }
  109. void myQueueFree(MyQueue* obj) {
  110. Stackdestroy(&obj->Pushst);
  111. Stackdestroy(&obj->Popst);
  112. free(obj);
  113. }

🐲👻这两道题的讲解就到这里了,若有错误或是建议欢迎小伙伴们指出。🐯🤖

🥰🤩希望小伙伴们可以多多支持博主哦。😍😃

😁😄谢谢你的阅读。😼😸


本文转载自: https://blog.csdn.net/m0_73868817/article/details/129691409
版权归原作者 sukuni 所有, 如有侵权,请联系我们删除。

“【Leetcode】队列实现栈和栈实现队列”的评论:

还没有评论