mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 01:26:28 +08:00
Compare commits
2 commits
98d7d00146
...
89908581b7
Author | SHA1 | Date | |
---|---|---|---|
|
89908581b7 | ||
|
e41b0a3156 |
2 changed files with 36 additions and 10 deletions
|
@ -111,16 +111,29 @@ int popLast(ArrayDeque *deque) {
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 返回数组用于打印 */
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
/* 初始化队列 */
|
/* 初始化队列 */
|
||||||
int capacity = 10;
|
int capacity = 10;
|
||||||
|
int queSize;
|
||||||
ArrayDeque *deque = newArrayDeque(capacity);
|
ArrayDeque *deque = newArrayDeque(capacity);
|
||||||
pushLast(deque, 3);
|
pushLast(deque, 3);
|
||||||
pushLast(deque, 2);
|
pushLast(deque, 2);
|
||||||
pushLast(deque, 5);
|
pushLast(deque, 5);
|
||||||
printf("双向队列 deque = ");
|
printf("双向队列 deque = ");
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
|
|
||||||
/* 访问元素 */
|
/* 访问元素 */
|
||||||
int peekFirstNum = peekFirst(deque);
|
int peekFirstNum = peekFirst(deque);
|
||||||
|
@ -131,18 +144,18 @@ int main() {
|
||||||
/* 元素入队 */
|
/* 元素入队 */
|
||||||
pushLast(deque, 4);
|
pushLast(deque, 4);
|
||||||
printf("元素 4 队尾入队后 deque = ");
|
printf("元素 4 队尾入队后 deque = ");
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
pushFirst(deque, 1);
|
pushFirst(deque, 1);
|
||||||
printf("元素 1 队首入队后 deque = ");
|
printf("元素 1 队首入队后 deque = ");
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
|
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
int popLastNum = popLast(deque);
|
int popLastNum = popLast(deque);
|
||||||
printf("队尾出队元素 = %d ,队尾出队后 deque= ", popLastNum);
|
printf("队尾出队元素 = %d ,队尾出队后 deque= ", popLastNum);
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
int popFirstNum = popFirst(deque);
|
int popFirstNum = popFirst(deque);
|
||||||
printf("队首出队元素 = %d ,队首出队后 deque= ", popFirstNum);
|
printf("队首出队元素 = %d ,队首出队后 deque= ", popFirstNum);
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
|
|
||||||
/* 获取队列的长度 */
|
/* 获取队列的长度 */
|
||||||
int dequeSize = size(deque);
|
int dequeSize = size(deque);
|
||||||
|
|
|
@ -74,10 +74,23 @@ int pop(ArrayQueue *queue) {
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 返回数组用于打印 */
|
||||||
|
int *toArray(ArrayQueue *queue, int *queSize) {
|
||||||
|
*queSize = queue->queSize;
|
||||||
|
int *res = (int *)calloc(queue->queSize, sizeof(int));
|
||||||
|
int j = queue->front;
|
||||||
|
for (int i = 0; i < queue->queSize; i++) {
|
||||||
|
res[i] = queue->nums[j % queue->queCapacity];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
/* 初始化队列 */
|
/* 初始化队列 */
|
||||||
int capacity = 10;
|
int capacity = 10;
|
||||||
|
int queSize;
|
||||||
ArrayQueue *queue = newArrayQueue(capacity);
|
ArrayQueue *queue = newArrayQueue(capacity);
|
||||||
|
|
||||||
/* 元素入队 */
|
/* 元素入队 */
|
||||||
|
@ -87,7 +100,7 @@ int main() {
|
||||||
push(queue, 5);
|
push(queue, 5);
|
||||||
push(queue, 4);
|
push(queue, 4);
|
||||||
printf("队列 queue = ");
|
printf("队列 queue = ");
|
||||||
printArray(queue->nums, queue->queSize);
|
printArray(toArray(queue, &queSize), queSize);
|
||||||
|
|
||||||
/* 访问队首元素 */
|
/* 访问队首元素 */
|
||||||
int peekNum = peek(queue);
|
int peekNum = peek(queue);
|
||||||
|
@ -96,7 +109,7 @@ int main() {
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
peekNum = pop(queue);
|
peekNum = pop(queue);
|
||||||
printf("出队元素 pop = %d ,出队后 queue = ", peekNum);
|
printf("出队元素 pop = %d ,出队后 queue = ", peekNum);
|
||||||
printArray(queue->nums, queue->queSize);
|
printArray(toArray(queue, &queSize), queSize);
|
||||||
|
|
||||||
/* 获取队列的长度 */
|
/* 获取队列的长度 */
|
||||||
int queueSize = size(queue);
|
int queueSize = size(queue);
|
||||||
|
@ -111,7 +124,7 @@ int main() {
|
||||||
push(queue, i);
|
push(queue, i);
|
||||||
pop(queue);
|
pop(queue);
|
||||||
printf("第 %d 轮入队 + 出队后 queue = ", i);
|
printf("第 %d 轮入队 + 出队后 queue = ", i);
|
||||||
printArray(queue->nums, queue->queSize);
|
printArray(toArray(queue, &queSize), queSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 释放内存
|
// 释放内存
|
||||||
|
|
Loading…
Reference in a new issue