2023-03-13 20:40:25 +08:00
|
|
|
/**
|
|
|
|
* File: array_deque.c
|
|
|
|
* Created Time: 2023-03-13
|
|
|
|
* Author: Gonglja (glj0@outlook.com)
|
|
|
|
*/
|
|
|
|
|
2023-04-24 04:11:18 +08:00
|
|
|
#include "../utils/common.h"
|
2023-03-13 20:40:25 +08:00
|
|
|
|
2023-04-18 20:21:31 +08:00
|
|
|
/* 基于环形数组实现的双向队列 */
|
2023-10-18 02:16:26 +08:00
|
|
|
typedef struct {
|
2023-03-13 20:40:25 +08:00
|
|
|
int *nums; // 用于存储队列元素的数组
|
|
|
|
int front; // 队首指针,指向队首元素
|
|
|
|
int queSize; // 尾指针,指向队尾 + 1
|
|
|
|
int queCapacity; // 队列容量
|
2023-10-18 02:16:26 +08:00
|
|
|
} ArrayDeque;
|
2023-03-13 20:40:25 +08:00
|
|
|
|
2023-04-18 20:21:31 +08:00
|
|
|
/* 构造函数 */
|
2023-10-18 02:16:26 +08:00
|
|
|
ArrayDeque *newArrayDeque(int capacity) {
|
|
|
|
ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));
|
2023-03-13 20:40:25 +08:00
|
|
|
// 初始化数组
|
|
|
|
deque->queCapacity = capacity;
|
2023-04-17 21:13:15 +08:00
|
|
|
deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);
|
2023-03-13 20:40:25 +08:00
|
|
|
deque->front = deque->queSize = 0;
|
|
|
|
return deque;
|
|
|
|
}
|
|
|
|
|
2023-04-18 20:21:31 +08:00
|
|
|
/* 析构函数 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void delArrayDeque(ArrayDeque *deque) {
|
2023-03-13 20:40:25 +08:00
|
|
|
free(deque->nums);
|
2023-11-01 05:14:22 +08:00
|
|
|
free(deque);
|
2023-03-13 20:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取双向队列的容量 */
|
2023-10-18 02:16:26 +08:00
|
|
|
int capacity(ArrayDeque *deque) {
|
2023-03-13 20:40:25 +08:00
|
|
|
return deque->queCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取双向队列的长度 */
|
2023-10-18 02:16:26 +08:00
|
|
|
int size(ArrayDeque *deque) {
|
2023-03-13 20:40:25 +08:00
|
|
|
return deque->queSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 判断双向队列是否为空 */
|
2023-10-18 02:16:26 +08:00
|
|
|
bool empty(ArrayDeque *deque) {
|
2023-03-13 20:40:25 +08:00
|
|
|
return deque->queSize == 0;
|
|
|
|
}
|
|
|
|
|
2023-06-06 02:03:27 +08:00
|
|
|
/* 计算环形数组索引 */
|
2023-10-18 02:16:26 +08:00
|
|
|
int dequeIndex(ArrayDeque *deque, int i) {
|
2023-03-13 20:40:25 +08:00
|
|
|
// 通过取余操作实现数组首尾相连
|
|
|
|
// 当 i 越过数组尾部时,回到头部
|
|
|
|
// 当 i 越过数组头部后,回到尾部
|
|
|
|
return ((i + capacity(deque)) % capacity(deque));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 队首入队 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void pushFirst(ArrayDeque *deque, int num) {
|
2023-03-13 20:40:25 +08:00
|
|
|
if (deque->queSize == capacity(deque)) {
|
|
|
|
printf("双向队列已满\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 队首指针向左移动一位
|
2023-12-28 18:06:09 +08:00
|
|
|
// 通过取余操作实现 front 越过数组头部回到尾部
|
2023-03-13 20:40:25 +08:00
|
|
|
deque->front = dequeIndex(deque, deque->front - 1);
|
|
|
|
// 将 num 添加到队首
|
|
|
|
deque->nums[deque->front] = num;
|
|
|
|
deque->queSize++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 队尾入队 */
|
2023-10-18 02:16:26 +08:00
|
|
|
void pushLast(ArrayDeque *deque, int num) {
|
2023-03-13 20:40:25 +08:00
|
|
|
if (deque->queSize == capacity(deque)) {
|
|
|
|
printf("双向队列已满\r\n");
|
|
|
|
return;
|
|
|
|
}
|
2023-12-28 18:06:09 +08:00
|
|
|
// 计算队尾指针,指向队尾索引 + 1
|
2023-03-13 20:40:25 +08:00
|
|
|
int rear = dequeIndex(deque, deque->front + deque->queSize);
|
|
|
|
// 将 num 添加至队尾
|
|
|
|
deque->nums[rear] = num;
|
|
|
|
deque->queSize++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
2023-10-18 02:16:26 +08:00
|
|
|
int peekFirst(ArrayDeque *deque) {
|
2023-03-13 20:40:25 +08:00
|
|
|
// 访问异常:双向队列为空
|
|
|
|
assert(empty(deque) == 0);
|
2023-04-17 21:13:15 +08:00
|
|
|
return deque->nums[deque->front];
|
2023-03-13 20:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 访问队尾元素 */
|
2023-10-18 02:16:26 +08:00
|
|
|
int peekLast(ArrayDeque *deque) {
|
2023-03-13 20:40:25 +08:00
|
|
|
// 访问异常:双向队列为空
|
|
|
|
assert(empty(deque) == 0);
|
|
|
|
int last = dequeIndex(deque, deque->front + deque->queSize - 1);
|
2023-04-17 21:13:15 +08:00
|
|
|
return deque->nums[last];
|
2023-03-13 20:40:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 队首出队 */
|
2023-10-18 02:16:26 +08:00
|
|
|
int popFirst(ArrayDeque *deque) {
|
2023-03-13 20:40:25 +08:00
|
|
|
int num = peekFirst(deque);
|
|
|
|
// 队首指针向后移动一位
|
|
|
|
deque->front = dequeIndex(deque, deque->front + 1);
|
|
|
|
deque->queSize--;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 队尾出队 */
|
2023-10-18 02:16:26 +08:00
|
|
|
int popLast(ArrayDeque *deque) {
|
2023-03-13 20:40:25 +08:00
|
|
|
int num = peekLast(deque);
|
|
|
|
deque->queSize--;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2024-12-04 19:38:30 +08:00
|
|
|
/* 返回数组用于打印 */
|
|
|
|
int *toArray(ArrayDeque *deque, int *queSize) {
|
|
|
|
*queSize = deque->queSize;
|
|
|
|
int *res = (int *)calloc(deque->queSize, sizeof(int));
|
|
|
|
int j = deque->front;
|
|
|
|
for (int i = 0; i < deque->queSize; i++) {
|
|
|
|
res[i] = deque->nums[j % deque->queCapacity];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-03-13 20:40:25 +08:00
|
|
|
/* Driver Code */
|
|
|
|
int main() {
|
|
|
|
/* 初始化队列 */
|
|
|
|
int capacity = 10;
|
2024-12-04 19:38:30 +08:00
|
|
|
int queSize;
|
2023-10-18 02:16:26 +08:00
|
|
|
ArrayDeque *deque = newArrayDeque(capacity);
|
2023-03-13 20:40:25 +08:00
|
|
|
pushLast(deque, 3);
|
|
|
|
pushLast(deque, 2);
|
|
|
|
pushLast(deque, 5);
|
|
|
|
printf("双向队列 deque = ");
|
2024-12-04 19:38:30 +08:00
|
|
|
printArray(toArray(deque, &queSize), queSize);
|
2023-03-13 20:40:25 +08:00
|
|
|
|
|
|
|
/* 访问元素 */
|
|
|
|
int peekFirstNum = peekFirst(deque);
|
|
|
|
printf("队首元素 peekFirst = %d\r\n", peekFirstNum);
|
|
|
|
int peekLastNum = peekLast(deque);
|
|
|
|
printf("队尾元素 peekLast = %d\r\n", peekLastNum);
|
|
|
|
|
|
|
|
/* 元素入队 */
|
|
|
|
pushLast(deque, 4);
|
|
|
|
printf("元素 4 队尾入队后 deque = ");
|
2024-12-04 19:38:30 +08:00
|
|
|
printArray(toArray(deque, &queSize), queSize);
|
2023-03-13 20:40:25 +08:00
|
|
|
pushFirst(deque, 1);
|
|
|
|
printf("元素 1 队首入队后 deque = ");
|
2024-12-04 19:38:30 +08:00
|
|
|
printArray(toArray(deque, &queSize), queSize);
|
2023-03-13 20:40:25 +08:00
|
|
|
|
|
|
|
/* 元素出队 */
|
2023-03-13 22:39:45 +08:00
|
|
|
int popLastNum = popLast(deque);
|
2023-09-17 01:13:15 +08:00
|
|
|
printf("队尾出队元素 = %d ,队尾出队后 deque= ", popLastNum);
|
2024-12-04 19:38:30 +08:00
|
|
|
printArray(toArray(deque, &queSize), queSize);
|
2023-03-13 22:39:45 +08:00
|
|
|
int popFirstNum = popFirst(deque);
|
2023-09-17 01:13:15 +08:00
|
|
|
printf("队首出队元素 = %d ,队首出队后 deque= ", popFirstNum);
|
2024-12-04 19:38:30 +08:00
|
|
|
printArray(toArray(deque, &queSize), queSize);
|
2023-03-13 20:40:25 +08:00
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
int dequeSize = size(deque);
|
|
|
|
printf("双向队列长度 size = %d\r\n", dequeSize);
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
bool isEmpty = empty(deque);
|
|
|
|
printf("队列是否为空 = %s\r\n", isEmpty ? "true" : "false");
|
|
|
|
|
|
|
|
// 释放内存
|
|
|
|
delArrayDeque(deque);
|
2023-04-17 21:13:15 +08:00
|
|
|
|
2023-03-13 20:40:25 +08:00
|
|
|
return 0;
|
2024-12-04 19:38:30 +08:00
|
|
|
}
|