V0.1
This commit is contained in:
77
src/main.c
Executable file
77
src/main.c
Executable file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* @file main.c
|
||||
* @author guishenking (guishenking@outlook.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2025-08-15
|
||||
*
|
||||
* @copyright Copyright (c) 2025
|
||||
*
|
||||
*/
|
||||
#include "queue.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
void print_queue(const queue_t *queue) {
|
||||
printf("Queue(size=%zu): ", queue_length(queue));
|
||||
for (size_t i = 0; i < queue_length(queue); ++i) {
|
||||
int val;
|
||||
queue_peek_at(queue, i, &val);
|
||||
printf("%d ", val);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
queue_t q;
|
||||
int ret;
|
||||
// 初始化队列,存储int类型
|
||||
if (!queue_init(&q, sizeof(int))) {
|
||||
printf("Init failed!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 入队
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
ret = queue_enqueue(&q, &i);
|
||||
printf("Enqueue %d: %s\n", i, ret ? "OK" : "FAIL");
|
||||
}
|
||||
print_queue(&q);
|
||||
|
||||
// 查询第3个元素
|
||||
int val = 0;
|
||||
if (queue_peek_at(&q, 2, &val)) {
|
||||
printf("Peek index 2: %d\n", val);
|
||||
}
|
||||
|
||||
// 从头部出队
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
if (queue_dequeue_head(&q, &val)) {
|
||||
printf("Dequeue head: %d\n", val);
|
||||
}
|
||||
}
|
||||
print_queue(&q);
|
||||
|
||||
// 从尾部出队
|
||||
if (queue_dequeue_tail(&q, &val)) {
|
||||
printf("Dequeue tail: %d\n", val);
|
||||
}
|
||||
print_queue(&q);
|
||||
|
||||
// 抛弃前3个
|
||||
size_t dropped = queue_drop_head(&q, 3);
|
||||
printf("Drop head 3, actually dropped: %zu\n", dropped);
|
||||
print_queue(&q);
|
||||
|
||||
// 抛弃尾部10个(超出队列长度,实际清空)
|
||||
dropped = queue_drop_tail(&q, 10);
|
||||
printf("Drop tail 10, actually dropped: %zu\n", dropped);
|
||||
print_queue(&q);
|
||||
|
||||
// 销毁队列
|
||||
queue_destroy(&q);
|
||||
printf("Queue destroyed.\n");
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user