• C/C++:双向队列的实现


    1. /**
    2. *
    3. * Althor:Hacker Hao
    4. * Create:2023.10.11
    5. *
    6. */
    7. #include
    8. using namespace std;
    9. #define MAXSIZE 200
    10. typedef struct Deque
    11. {
    12. int front; //头
    13. int rear; //尾
    14. int num; //队列中的元素数量
    15. int arr[MAXSIZE]; //队列中存储的数字
    16. }Deque;
    17. Deque* Init() //初始化队列
    18. {
    19. Deque* q = (Deque*)malloc(sizeof(Deque));
    20. if (!q)
    21. {
    22. cout << "建立错误!" << endl;
    23. exit(0);
    24. }
    25. q->front = q->rear = -1;
    26. q->num = 0;
    27. return q;
    28. }
    29. void Clean(Deque* q) //清空队列
    30. {
    31. if (!q)
    32. {
    33. cout << "队列不存在! " << endl;
    34. return;
    35. }
    36. //由于队列只是用了一次动态内存分配,所以直接free
    37. q->front = q->rear = -1;
    38. q->num = 0;
    39. }
    40. void Destroy(Deque* q) //销毁队列
    41. {
    42. if (!q)
    43. {
    44. cout << "队列不存在!" << endl;
    45. return;
    46. }
    47. free(q);
    48. q = NULL;
    49. }
    50. bool IsEmpty(Deque* q) //判断是否为空
    51. {
    52. if (q == NULL)
    53. {
    54. cout << "队伍不存在!" << endl;
    55. return true; //为了防止队列空时对空指针操作
    56. }
    57. return q->num == 0;
    58. }
    59. bool IsFull(Deque* q) //判断是否队满
    60. {
    61. if (q == NULL)
    62. {
    63. cout << "队伍不存在!" << endl;
    64. return true; //为了防止在队不满的情况下对空指针操作
    65. }
    66. return q->num == MAXSIZE;
    67. }
    68. int GetHead(Deque* q) //返回队首元素
    69. {
    70. if (q == NULL)
    71. {
    72. cout << "队列不存在!" << endl;
    73. return -1;
    74. }
    75. if (IsEmpty(q))
    76. {
    77. cout << "队列为空,没有头元素" << endl;
    78. return -1;
    79. }
    80. return q->arr[q->front];
    81. }
    82. int GetTail(Deque* q) //返回队尾元素
    83. {
    84. if (q == NULL)
    85. {
    86. cout << "队列不存在" << endl;
    87. return -1;
    88. }
    89. if (IsEmpty(q))
    90. {
    91. cout << "队列为空!" << endl;
    92. return -1;
    93. }
    94. return q->arr[q->rear];
    95. }
    96. int GetHeadIndex(Deque* q, int size) {
    97. if (IsEmpty(q)) {
    98. q->rear = 0;//如果从对头入队的是第一个元素,则需要将队头队尾指针都设为0
    99. return 0;
    100. }
    101. if (q->front - 1 < 0) {
    102. return (q->front - 1) + size;
    103. }
    104. else {
    105. return q->front - 1;
    106. }
    107. }
    108. void PushFromHead(Deque* q, int k)
    109. {
    110. if (q == NULL)
    111. {
    112. cout << "队列不存在" << endl;
    113. return;
    114. }
    115. if (IsFull(q))
    116. {
    117. cout << "队列已满,无法入队" << endl;
    118. return;
    119. }
    120. q->front = GetHeadIndex(q, MAXSIZE);
    121. q->num++;
    122. q->arr[q->front] = k;
    123. }
    124. //获得从队尾入队的下标
    125. int GetTailIndex(Deque* q, int size)
    126. {
    127. if (IsEmpty(q))
    128. {
    129. q->front = 0;
    130. return 0;
    131. }
    132. return (q->rear + 1) % size;
    133. }
    134. //从队尾入队
    135. void PushFromTail(Deque* q, int k)
    136. {
    137. if (!q)
    138. {
    139. cout << "队列不存在" << endl;
    140. return;
    141. }
    142. if (IsFull(q))
    143. {
    144. cout << "队列已满,无法入队" << endl;
    145. return;
    146. }
    147. q->rear = GetTailIndex(q, MAXSIZE);
    148. q->num++;
    149. q->arr[q->rear] = k;
    150. }
    151. //从队头出队
    152. int PopFromHead(Deque* q) {
    153. if (q == NULL) {
    154. cout << "队列不存在" << endl;
    155. return -1;
    156. }
    157. if (IsEmpty(q)) {
    158. cout << "队列为空,无法出队" << endl;
    159. return -1;
    160. }
    161. q->num--;
    162. int ret = q->arr[q->front];
    163. q->front = (q->front + 1) % MAXSIZE;
    164. return ret;
    165. }
    166. //从队尾出队
    167. int PopFromTail(Deque* q)
    168. {
    169. if (q == NULL) {
    170. cout << "队列不存在" << endl;
    171. return -1;
    172. }
    173. if (IsEmpty(q)) {
    174. cout << "队列为空,无法出队" << endl;
    175. return -1;
    176. }
    177. int ret = q->arr[q->rear];
    178. q->num--;
    179. if (q->rear - 1 < 0) {
    180. q->rear = q->rear - 1 + MAXSIZE;
    181. }
    182. else {
    183. q->rear -= 1;
    184. }
    185. return ret;
    186. }
    187. //从队头开始打印队列数据
    188. void Print(Deque* q)
    189. {
    190. if (q == NULL) {
    191. cout << "队列不存在" << endl;
    192. return;
    193. }
    194. if (IsEmpty(q)) {
    195. cout << "队列为空,无法打印" << endl;
    196. return;
    197. }
    198. int count = q->num;
    199. int p = q->front;
    200. while (count--) {
    201. cout << q->arr[p] << " ";
    202. p = (p + 1) % MAXSIZE;
    203. }
    204. cout << endl;
    205. }
    206. int main()
    207. {
    208. cin.tie(0), cout.tie(0);
    209. Deque* q = Init();
    210. cout << "从队首入队:" << endl;
    211. for (int i = 0; i < 10; i++)
    212. {
    213. PushFromHead(q, i);
    214. }
    215. Print(q);
    216. cout << "从队尾入队:" << endl;
    217. for (int i = 20; i < 35; i++) {
    218. PushFromTail(q, i);
    219. }
    220. Print(q);
    221. cout << "从队头删除的元素是:" << PopFromHead(q) << endl;
    222. Print(q);
    223. cout << "从队尾删除的元素是:" << PopFromTail(q) << endl;
    224. Print(q);
    225. cout << "队首元素是:" << GetHead(q) << endl;
    226. cout << "队尾元素是:" << GetTail(q) << endl;
    227. return 0;
    228. }

  • 相关阅读:
    OP-TEE中的线程管理(四)
    算法与数据结构 --- 栈的表示和操作的实现
    新能源汽车行业资讯-2022-9-15
    前端周刊第三十七期
    单向链表模版实现(c++)
    Spring整合mybatis
    关于序列化与反序列化解题
    许可分析 license 分析 第十三章
    算法题 — 接雨水
    【 OpenGauss源码学习 —— 列存储(update)】
  • 原文地址:https://blog.csdn.net/Hacker_Hao/article/details/133846112