mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:46:29 +08:00
Merge 2f73e9b770
into 2a9db6d039
This commit is contained in:
commit
20b047b916
1 changed files with 14 additions and 7 deletions
|
@ -73,7 +73,14 @@ int pop(ArrayQueue *queue) {
|
||||||
queue->queSize--;
|
queue->queSize--;
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
void print_queue(ArrayQueue *queue)
|
||||||
|
{
|
||||||
|
int tmp =queue->front;
|
||||||
|
int rear =(queue->front+queue->queSize-1)%queue->queCapacity;
|
||||||
|
for(;tmp != rear;tmp++,tmp%=queue->queCapacity)
|
||||||
|
printf("%d,",queue->nums[tmp]);
|
||||||
|
printf("%d]\n",queue->nums[rear]);
|
||||||
|
}
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
/* 初始化队列 */
|
/* 初始化队列 */
|
||||||
|
@ -86,7 +93,7 @@ int main() {
|
||||||
push(queue, 2);
|
push(queue, 2);
|
||||||
push(queue, 5);
|
push(queue, 5);
|
||||||
push(queue, 4);
|
push(queue, 4);
|
||||||
printf("队列 queue = ");
|
printf("队列 queue = [");
|
||||||
printArray(queue->nums, queue->queSize);
|
printArray(queue->nums, queue->queSize);
|
||||||
|
|
||||||
/* 访问队首元素 */
|
/* 访问队首元素 */
|
||||||
|
@ -95,8 +102,8 @@ int main() {
|
||||||
|
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
peekNum = pop(queue);
|
peekNum = pop(queue);
|
||||||
printf("出队元素 pop = %d ,出队后 queue = ", peekNum);
|
printf("出队元素 pop = %d ,出队后 queue = [", peekNum);
|
||||||
printArray(queue->nums, queue->queSize);
|
print_queue(queue);
|
||||||
|
|
||||||
/* 获取队列的长度 */
|
/* 获取队列的长度 */
|
||||||
int queueSize = size(queue);
|
int queueSize = size(queue);
|
||||||
|
@ -110,12 +117,12 @@ int main() {
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
push(queue, i);
|
push(queue, i);
|
||||||
pop(queue);
|
pop(queue);
|
||||||
printf("第 %d 轮入队 + 出队后 queue = ", i);
|
printf("第 %d 轮入队 + 出队后 queue = [", i);
|
||||||
printArray(queue->nums, queue->queSize);
|
print_queue(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 释放内存
|
// 释放内存
|
||||||
delArrayQueue(queue);
|
delArrayQueue(queue);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in a new issue