当前位置:   article > 正文

数据结构之队列_数据结构队列

数据结构队列

目录

一、队列的概念

二、队列的实现

 (1)问题1:结构体和指针变量传参

(2)初始化

(3)销毁队列

(4)入队

(5)出队

(6)获取队头元素

(7)获取队尾元素

(8)获取队列长度

(9)判断是否为空队列

(10)创建新节点

(11)相当于打印队列中的元素

 总代码:

Queue.h

Queue.c

test.c

三、用队列实现栈

 四、用栈实现队列


一、队列的概念

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出(FIFO)的原则。
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为对头

如图:

二、队列的实现

队列既可以用数组和也可以用链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,每次删除队头,后面的元素就要向前移动,所以效率比较低

 (1)问题1:结构体和指针变量传参

  1.当我们创建的只是一个指针变量的时候,如果我们要改变这个指针变量,那么我们需要传二级指针

比如:单链表,初始创建的是一个节点的指针变量,那我们如果需要改变这个指针变量,那么传的就是这个指针变量的地址。

2.但如果我们在初始定义一个结构体,里面存放着俩个指针, 那当我们需要改变这个结构体的内容时,我们传的是这个结构体的地址。

比如:   

 

 个人总结:改变指针所指向内容和指针方向的传地址,不改变指针所指向的内容和方向的传本身。即改变什么就要传它的地址,比如,要改变结构体,那么就要传结构体的地址。

(2)初始化

  1. void QueueInit(Queue* pq)
  2. {
  3. assert(pq);
  4. pq->front = pq->rear = NULL;
  5. }

(3)销毁队列

  1. //销毁队列
  2. void QueueDestroy(Queue* pq)
  3. {
  4. assert(pq);//此处说明传过来的队列不能为空,为空说明传错了
  5. QueueNode* cur= pq->front;
  6. //删除,直到为NULL
  7. while (cur != NULL)
  8. {
  9. //先保存下一个节点
  10. QueueNode* next = cur->next;
  11. free(cur);
  12. cur = next;
  13. }
  14. //到这,说明删除成功
  15. pq->front = pq->rear = NULL;
  16. }

(4)入队

分为俩种情况:

第一种情况:对头和队尾都指向NULL

第二种情况:一个节点或者多个节点时,可以直接插入在队尾后面

  1. void QueuePush(Queue* pq, DataType x)
  2. {
  3. assert(pq);
  4. QueueNode* newnode = BuyNode(x);
  5. //对头和队尾都指向NULL的情况
  6. if (pq->front == NULL && pq->rear == NULL)
  7. {
  8. pq->front = newnode;
  9. pq->rear = newnode;
  10. }
  11. //如果只有一个节点的情况或者多个节点的情况
  12. else
  13. {
  14. pq->rear->next = newnode;
  15. pq->rear = newnode;
  16. }
  17. }

(5)出队

思路:

1.先保存对头的下一个节点,然后将对头删除,再将保存的节点赋值给对头,并且只有当对头不为NULL的时候才可以继续删除
2.当队头为NULL的情况下,则会有问题
3.特殊情况:如果front指向rear时,将rear的空间释放了,front指向了空,而rear还是指向那个空间,则会造成野指针,所以这时的处理是当front为空时,将front也置为空

  1. void QueuePop(Queue* pq)
  2. {
  3. assert(pq);
  4. //如果队列为空的情况
  5. //方式一:
  6. /*assert(pq->front);*/
  7. //方式二:
  8. assert(!QueueEmpty(pq));
  9. //只有对头不为NULL的时候才可以继续删除
  10. QueueNode* Next = pq->front->next;
  11. free(pq->front);
  12. pq->front = Next;
  13. //特殊情况:
  14. if (pq->front == NULL)
  15. {
  16. pq->rear = NULL;
  17. }
  18. }

(6)获取队头元素

  1. //获取队头的元素
  2. DataType QueueFront(Queue* pq)
  3. {
  4. assert(pq);
  5. assert(!QueueEmpty(pq));
  6. return pq->front->data;
  7. }

(7)获取队尾元素

  1. //获取队尾的元素
  2. DataType QueueBack(Queue* pq)
  3. {
  4. assert(pq);
  5. assert(!QueueEmpty(pq));
  6. return pq->rear->data;
  7. }

(8)获取队列长度

  1. //获取队列的长度
  2. int QueueSize(Queue* pq)
  3. {
  4. assert(pq);
  5. int count = 0;
  6. QueueNode* cur = pq->front;
  7. while (cur != NULL)
  8. {
  9. count++;
  10. cur = cur->next;
  11. }
  12. return count;
  13. }

(9)判断是否为空队列

  1. //队列是否为空
  2. bool QueueEmpty(Queue* pq)
  3. {
  4. assert(pq);
  5. return pq->front == NULL;
  6. }

(10)创建新节点

  1. //创建新节点
  2. QueueNode* BuyNode(DataType x)
  3. {
  4. QueueNode* temp= (QueueNode*)malloc(sizeof(QueueNode));
  5. if (temp == NULL)
  6. {
  7. printf("BuyNode failed");
  8. exit(-1);
  9. }
  10. temp->data = x;
  11. temp->next = NULL;
  12. return temp;
  13. }

(11)相当于打印队列中的元素

  1. //相当于打印队列中的元素
  2. //1.队列不为空,打印队头元素,删除多头元素,然后队头指向下一个节点,直到队列为空
  3. void QueuePrint(Queue* pq)
  4. {
  5. while (!QueueEmpty(pq))
  6. {
  7. //获取队头元素
  8. DataType front = QueueFront(pq);
  9. //打印队头元素
  10. printf("%d\n",front);
  11. //删除队头,即出队
  12. QueuePop(pq);
  13. }
  14. }

 初始值:

 运行结果:

 总代码:

Queue.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<assert.h>
  4. #include<stdlib.h>
  5. #include<malloc.h>
  6. #include<stdbool.h>
  7. typedef int DataType;
  8. typedef struct QueueNode
  9. {
  10. DataType data;
  11. struct QueueNode* next;
  12. }QueueNode;
  13. //再给队列定义一个对头和队尾
  14. typedef struct Queue
  15. {
  16. QueueNode* front;
  17. QueueNode* rear;
  18. }Queue;
  19. //创建新节点
  20. QueueNode* BuyNode(DataType x);
  21. void QueueInit(Queue* pq);//传过去的是结构体的地址
  22. void QueueDestroy(Queue* pq);
  23. void QueuePush(Queue* pq,DataType x);
  24. void QueuePop(Queue* pq);
  25. //获取对头的元素
  26. DataType QueueFront(Queue* pq);
  27. //获取队尾的元素
  28. DataType QueueBack(Queue* pq);
  29. //获取队列的长度
  30. int QueueSize(Queue* pq);
  31. //队列是否为空
  32. bool QueueEmpty(Queue* pq);

Queue.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include"Queue.h"
  3. //创建新节点
  4. QueueNode* BuyNode(DataType x)
  5. {
  6. QueueNode* temp= (QueueNode*)malloc(sizeof(QueueNode));
  7. if (temp == NULL)
  8. {
  9. printf("BuyNode failed");
  10. exit(-1);
  11. }
  12. temp->data = x;
  13. temp->next = NULL;
  14. return temp;
  15. }
  16. //初始化
  17. void QueueInit(Queue* pq)
  18. {
  19. assert(pq);
  20. pq->front = pq->rear = NULL;
  21. }
  22. //销毁队列
  23. void QueueDestroy(Queue* pq)
  24. {
  25. assert(pq);//此处说明传过来的队列不能为空,为空说明传错了
  26. QueueNode* cur= pq->front;
  27. //删除,直到为NULL
  28. while (cur != NULL)
  29. {
  30. //先保存下一个节点
  31. QueueNode* next = cur->next;
  32. free(cur);
  33. cur = next;
  34. }
  35. //到这,说明删除成功
  36. pq->front = pq->rear = NULL;
  37. }
  38. //入队
  39. void QueuePush(Queue* pq, DataType x)
  40. {
  41. assert(pq);
  42. QueueNode* newnode = BuyNode(x);
  43. //对头和队尾都指向NULL的情况
  44. if (pq->front == NULL && pq->rear == NULL)
  45. {
  46. pq->front = newnode;
  47. pq->rear = newnode;
  48. }
  49. //如果只有一个节点的情况或者多个节点的情况
  50. else
  51. {
  52. pq->rear->next = newnode;
  53. pq->rear = newnode;
  54. }
  55. }
  56. //出队
  57. void QueuePop(Queue* pq)
  58. {
  59. assert(pq);
  60. //如果队列为空的情况
  61. //方式一:
  62. /*assert(pq->front);*/
  63. //方式二:
  64. assert(!QueueEmpty(pq));
  65. //只有对头不为NULL的时候才可以继续删除
  66. QueueNode* Next = pq->front->next;
  67. free(pq->front);
  68. pq->front = Next;
  69. //特殊情况
  70. if (pq->front == NULL)
  71. {
  72. pq->rear = NULL;
  73. }
  74. }
  75. //获取队头的元素
  76. DataType QueueFront(Queue* pq)
  77. {
  78. assert(pq);
  79. assert(!QueueEmpty(pq));
  80. return pq->front->data;
  81. }
  82. //获取队尾的元素
  83. DataType QueueBack(Queue* pq)
  84. {
  85. assert(pq);
  86. assert(!QueueEmpty(pq));
  87. return pq->rear->data;
  88. }
  89. //获取队列的长度
  90. int QueueSize(Queue* pq)
  91. {
  92. assert(pq);
  93. int count = 0;
  94. QueueNode* cur = pq->front;
  95. while (cur != NULL)
  96. {
  97. count++;
  98. cur = cur->next;
  99. }
  100. return count;
  101. }
  102. //队列是否为空
  103. bool QueueEmpty(Queue* pq)
  104. {
  105. assert(pq);
  106. return pq->front == NULL;
  107. }
  108. //相当于打印队列中的元素
  109. //1.队列不为空,打印队头元素,删除多头元素,然后队头指向下一个节点,直到队列为空
  110. void QueuePrint(Queue* pq)
  111. {
  112. while (!QueueEmpty(pq))
  113. {
  114. //获取队头元素
  115. DataType front = QueueFront(pq);
  116. //打印队头元素
  117. printf("%d\n",front);
  118. //删除队头,即出队
  119. QueuePop(pq);
  120. }
  121. }

test.c

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include"Queue.h"
  3. void QueueTest()
  4. {
  5. Queue q ;//创建一个结构体
  6. //那么想要改变这个结构体,就要传结构体的指针
  7. QueueInit(&q);
  8. QueuePush(&q,1);
  9. QueuePush(&q,2);
  10. QueuePush(&q,3);
  11. QueuePush(&q,4);
  12. QueuePrint(&q);
  13. //printf("%d", QueueSize(&q));
  14. //QueuePop(&q);
  15. //printf("%d\n", QueueFront(&q));
  16. //QueuePop(&q);
  17. //printf("%d\n", QueueFront(&q));
  18. //QueuePop(&q);
  19. //printf("%d\n", QueueFront(&q));
  20. QueuePop(&q);
  21. printf("%d", QueueFront(&q));
  22. //QueuePush(&q, 3);
  23. //printf("%d\n", QueueBack(&q));
  24. //QueuePush(&q, 4);
  25. //printf("%d\n", QueueBack(&q));
  26. QueueDestroy(&q);
  27. }
  28. int main()
  29. {
  30. QueueTest();
  31. return 0;
  32. }

三、用队列实现栈

225. 用队列实现栈 - 力扣(LeetCode) (leetcode-cn.com)

题目:请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppop 和 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语言不能直接使用队列及队列的相关操作,所以必须先创建一个队列及其操作函数

  1. //栈的相关接口函数
  2. typedef int DataType;
  3. typedef struct QueueNode
  4. {
  5. DataType data;
  6. struct QueueNode* next;
  7. }QueueNode;
  8. //再给队列定义一个对头和队尾
  9. typedef struct Queue
  10. {
  11. QueueNode* front;
  12. QueueNode* rear;
  13. }Queue;
  14. //创建新节点
  15. QueueNode* BuyNode(DataType x);
  16. void QueueInit(Queue* pq);//传过去的是结构体的地址
  17. void QueueDestroy(Queue* pq);
  18. void QueuePush(Queue* pq,DataType x);
  19. void QueuePop(Queue* pq);
  20. //获取对头的元素
  21. DataType QueueFront(Queue* pq);
  22. //获取队尾的元素
  23. DataType QueueBack(Queue* pq);
  24. //获取队列的长度
  25. int QueueSize(Queue* pq);
  26. //队列是否为空
  27. bool QueueEmpty(Queue* pq);
  28. //创建新节点
  29. QueueNode* BuyNode(DataType x)
  30. {
  31. QueueNode* temp= (QueueNode*)malloc(sizeof(QueueNode));
  32. if (temp == NULL)
  33. {
  34. printf("BuyNode failed");
  35. exit(-1);
  36. }
  37. temp->data = x;
  38. temp->next = NULL;
  39. return temp;
  40. }
  41. //初始化
  42. void QueueInit(Queue* pq)
  43. {
  44. assert(pq);
  45. pq->front = pq->rear = NULL;
  46. }
  47. //销毁队列
  48. void QueueDestroy(Queue* pq)
  49. {
  50. assert(pq);//此处说明传过来的队列不能为空,为空说明传错了
  51. QueueNode* cur= pq->front;
  52. //删除,直到为NULL
  53. while (cur != NULL)
  54. {
  55. //先保存下一个节点
  56. QueueNode* next = cur->next;
  57. free(cur);
  58. cur = next;
  59. }
  60. //到这,说明删除成功
  61. pq->front = pq->rear = NULL;
  62. }
  63. //入队
  64. void QueuePush(Queue* pq, DataType x)
  65. {
  66. assert(pq);
  67. QueueNode* newnode = BuyNode(x);
  68. //对头和队尾都指向NULL的情况
  69. if (pq->front == NULL && pq->rear == NULL)
  70. {
  71. pq->front = newnode;
  72. pq->rear = newnode;
  73. }
  74. //如果只有一个节点的情况或者多个节点的情况
  75. else
  76. {
  77. pq->rear->next = newnode;
  78. pq->rear = newnode;
  79. }
  80. }
  81. //出队
  82. void QueuePop(Queue* pq)
  83. {
  84. assert(pq);
  85. //如果队列为空的情况
  86. //方式一:
  87. /*assert(pq->front);*/
  88. //方式二:
  89. assert(!QueueEmpty(pq));
  90. //只有对头不为NULL的时候才可以继续删除
  91. QueueNode* Next = pq->front->next;
  92. free(pq->front);
  93. pq->front = Next;
  94. //特殊情况
  95. if (pq->front == NULL)
  96. {
  97. pq->rear = NULL;
  98. }
  99. }
  100. //获取队头的元素
  101. DataType QueueFront(Queue* pq)
  102. {
  103. assert(pq);
  104. assert(!QueueEmpty(pq));
  105. return pq->front->data;
  106. }
  107. //获取队尾的元素
  108. DataType QueueBack(Queue* pq)
  109. {
  110. assert(pq);
  111. assert(!QueueEmpty(pq));
  112. return pq->rear->data;
  113. }
  114. //获取队列的长度
  115. int QueueSize(Queue* pq)
  116. {
  117. assert(pq);
  118. int count = 0;
  119. QueueNode* cur = pq->front;
  120. while (cur != NULL)
  121. {
  122. count++;
  123. cur = cur->next;
  124. }
  125. return count;
  126. }
  127. //队列是否为空
  128. bool QueueEmpty(Queue* pq)
  129. {
  130. assert(pq);
  131. return pq->front == NULL;
  132. }
  133. //相当于打印队列中的元素
  134. //1.队列不为空,打印队头元素,删除多头元素,然后队头指向下一个节点,直到队列为空
  135. void QueuePrint(Queue* pq)
  136. {
  137. while (!QueueEmpty(pq))
  138. {
  139. //获取队头元素
  140. DataType front = QueueFront(pq);
  141. //打印队头元素
  142. printf("%d\n",front);
  143. //删除队头,即出队
  144. QueuePop(pq);
  145. }
  146. }
  147. //建立栈结构体,栈中存放着俩个队列
  148. typedef struct
  149. {
  150. //创建俩个队列
  151. Queue q1;
  152. Queue q2;
  153. } MyStack;
  154. //创建栈的空间,为队列初始化,最后返回该栈
  155. MyStack* myStackCreate()
  156. {
  157. MyStack* st= (MyStack*)malloc(sizeof(MyStack));
  158. QueueInit(&st->q1);
  159. QueueInit(&st->q2);
  160. return st;
  161. }
  162. void myStackPush(MyStack* obj, int x)
  163. {
  164. //如果不为空就插入
  165. if(!QueueEmpty(&obj->q1))
  166. {
  167. QueuePush(&obj->q1,x);
  168. }
  169. else
  170. {
  171. QueuePush(&obj->q2,x);
  172. }
  173. }
  174. //将不为空的队列前n-1个转移到另一个队列,最后一个删除,并返回其元素,删除队列最后一个元素,就相当于删除栈顶元素
  175. int myStackPop(MyStack* obj)
  176. {
  177. Queue* empty = &obj->q1;
  178. Queue* noempty = &obj->q2;
  179. if(!QueueEmpty(&obj->q1))
  180. {
  181. empty = &obj->q2;
  182. noempty = &obj->q1;
  183. }
  184. //进行插入删除
  185. while(QueueSize(noempty) > 1)
  186. {
  187. QueuePush(empty,QueueFront(noempty));
  188. QueuePop(noempty);
  189. }
  190. //移除并返回栈顶元素
  191. int top = QueueFront(noempty);
  192. QueuePop(noempty);
  193. return top;
  194. }
  195. //q1和q2哪个队列不为空,就返回它的队尾元素,因为另一个队列为空
  196. int myStackTop(MyStack* obj)
  197. {
  198. if(!QueueEmpty(&obj->q1))
  199. {
  200. return QueueBack(&obj->q1);
  201. }
  202. else
  203. {
  204. return QueueBack(&obj->q2);
  205. }
  206. }
  207. //栈为空,将相当于q1和q2都为空时
  208. bool myStackEmpty(MyStack* obj)
  209. {
  210. return QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2);
  211. }
  212. //销毁栈,就相当于将malloc出的,free掉,q1和q2节点都是队列的对头,其每一个节点都是malloc出来的,并且栈也是malloc出来的,也要一并free
  213. void myStackFree(MyStack* obj)
  214. {
  215. QueueDestroy(&obj->q1);
  216. QueueDestroy(&obj->q2);
  217. free(obj);
  218. }
  219. /**
  220. * Your MyStack struct will be instantiated and called as such:
  221. * MyStack* obj = myStackCreate();
  222. * myStackPush(obj, x);
  223. * int param_2 = myStackPop(obj);
  224. * int param_3 = myStackTop(obj);
  225. * bool param_4 = myStackEmpty(obj);
  226. * myStackFree(obj);
  227. */

 运行结果:

 四、用栈实现队列

232. 用栈实现队列 - 力扣(LeetCode) (leetcode-cn.com)

题目:请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:
你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

思路:
将元素都入到PushST,出栈从PopST中出栈,当PopST中没有元素可以出栈时,将PushST中的元素导入到PopST中,再出栈,出栈的顺序与队列的出队顺序一致。

注意点:不能去改变栈的结构,只能调用栈提供的接口函数实现

图解:

 代码:

  1. typedef int DataType;
  2. typedef struct Stack
  3. {
  4. DataType* a;//a相当于一个数组
  5. int top;
  6. int capacity;
  7. }ST;
  8. //初始化
  9. void StackInit(ST* ps);
  10. //销毁栈
  11. void StackDestroy(ST* ps);
  12. //入栈
  13. void StackPush(ST* ps,DataType x);
  14. //出栈
  15. void StackPop(ST* ps);
  16. //获取栈顶元素
  17. DataType StackTop(ST* ps);
  18. //获取栈中有效元素
  19. int StackSize(ST* ps);
  20. //检测栈是否为空
  21. bool StackEmpty(ST* ps);
  22. //遍历栈
  23. void StackPrint(ST* ps);
  24. //初始化
  25. void StackInit(ST* ps)
  26. {
  27. assert(ps);
  28. ps->a = NULL;
  29. ps->top = 0;//ps->top = -1;
  30. ps->capacity = 0;
  31. }
  32. //入栈
  33. void StackPush(ST* ps, DataType x)
  34. {
  35. assert(ps);
  36. //考虑如果容量不够的情况
  37. if (ps->top == ps->capacity)
  38. {
  39. int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
  40. DataType* tmp =realloc(ps->a,newcapacity * sizeof(DataType));//就是a是一指针,每次访问4个字节,第一次扩了44字节
  41. if (tmp == NULL)
  42. {
  43. printf("realloc failed");
  44. exit(-1);
  45. }
  46. //更新
  47. ps->a = tmp;
  48. ps->capacity = newcapacity;
  49. }
  50. ps->a[ps->top] = x;
  51. ps->top++;
  52. }
  53. //销毁栈
  54. void StackDestroy(ST* ps)
  55. {
  56. //将数组的指针释放掉即可
  57. free(ps->a);
  58. ps->a = NULL;
  59. ps->capacity = ps->top = 0;
  60. }
  61. //删除数据
  62. void StackPop(ST* ps)
  63. {
  64. assert(ps);
  65. //写法一:
  66. /*assert(ps->top > 0);*///top == 1 表示,容量为1,栈顶指针指向0top == 0表示容量为0,栈顶指针为-1,栈为NULL
  67. //写法二:
  68. assert(!StackEmpty(ps));
  69. ps->top--;
  70. }
  71. //获取栈顶元素
  72. DataType StackTop(ST* ps)
  73. {
  74. assert(ps);
  75. //方式一:
  76. /*assert(ps->top > 0);*/
  77. //方式二:
  78. assert(!StackEmpty(ps));
  79. return ps->a[ps->top-1];
  80. }
  81. //检测栈是否为空
  82. bool StackEmpty(ST* ps)
  83. {
  84. assert(ps);
  85. //写法一:
  86. //if (ps->top == 0)
  87. //{
  88. // return true;
  89. //}
  90. //else
  91. // return false;
  92. //写法二:
  93. return ps->top == 0;//top == 0,为1,表示栈为空
  94. }
  95. //获取栈中有效元素
  96. int StackSize(ST* ps)
  97. {
  98. assert(ps);
  99. return ps->top;//top1,容量为1,如栈顶指向0top指向他的上一个,也就是1,此时容量为1
  100. }
  101. //遍历栈
  102. void StackPrint(ST* ps)
  103. {
  104. //遍历栈
  105. while (!StackEmpty(ps))//栈不为空
  106. {
  107. printf("%d ", StackTop(ps));
  108. //删除栈顶元素,top--,也就是先将栈顶元素出栈,然后top--,访问下一个
  109. StackPop(ps);
  110. }
  111. }
  112. typedef struct
  113. {
  114. //创建俩个栈
  115. ST PushST;
  116. ST PopST;
  117. } MyQueue;
  118. MyQueue* myQueueCreate()
  119. {
  120. MyQueue* queue = (MyQueue*)malloc(sizeof(MyQueue));
  121. StackInit(&queue->PushST);
  122. StackInit(&queue->PopST);
  123. return queue;
  124. }
  125. void myQueuePush(MyQueue* obj, int x)
  126. {
  127. StackPush(&obj->PushST,x);
  128. }
  129. int myQueuePop(MyQueue* obj)
  130. {
  131. //如果PopST没有数据,就PushST的数据导过去
  132. if(StackEmpty(&obj->PopST))
  133. {
  134. while(!StackEmpty(&obj->PushST))
  135. {
  136. StackPush(&obj->PopST,StackTop(&obj->PushST));
  137. StackPop(&obj->PushST);
  138. }
  139. }
  140. int front = StackTop(&obj->PopST);
  141. StackPop(&obj->PopST);
  142. return front;
  143. }
  144. int myQueuePeek(MyQueue* obj)
  145. {
  146. //如果PopST中没有数据了,就将PushST中的数据导入过去,就符合队列的顺序了
  147. if(StackEmpty(&obj->PopST))
  148. {
  149. while(!StackEmpty(&obj->PushST))
  150. {
  151. StackPush(&obj->PopST,StackTop(&obj->PushST));
  152. StackPop(&obj->PushST);
  153. }
  154. }
  155. return StackTop(&obj->PopST);
  156. }
  157. bool myQueueEmpty(MyQueue* obj)
  158. {
  159. return StackEmpty(&obj->PushST) && StackEmpty(&obj->PopST);
  160. }
  161. void myQueueFree(MyQueue* obj)
  162. {
  163. StackDestroy(&obj->PushST);
  164. StackDestroy(&obj->PopST);
  165. free(obj);
  166. }
  167. /**
  168. * Your MyQueue struct will be instantiated and called as such:
  169. * MyQueue* obj = myQueueCreate();
  170. * myQueuePush(obj, x);
  171. * int param_2 = myQueuePop(obj);
  172. * int param_3 = myQueuePeek(obj);
  173. * bool param_4 = myQueueEmpty(obj);
  174. * myQueueFree(obj);
  175. */

运行结果:

循环队列详见下一篇博客,本篇如有问题,请在评论区多多评论哈^_^

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/915519
推荐阅读
相关标签
  

闽ICP备14008679号