mirror of
https://gitee.com/apaki/unicstl.git
synced 2026-05-29 07:04:20 +08:00
- 移除 list 源码、头文件及测试用例 - 将 demo 及测试迁移至 arraylist - 在 arraylist 中实现 slice 功能并支持负索引 - 修复 arraylist 负数索引计算逻辑 - 修复 darray 打印函数参数错误 - 优化 mempool 日志输出格式 BREAKING CHANGE: 移除 list 模块;arraylist::slice 返回值改为新列表实例
306 lines
6.9 KiB
C
306 lines
6.9 KiB
C
/**
|
|
* @file demo_arraylist.c
|
|
* @author wenjf (Orig5826@163.com)
|
|
* @brief
|
|
* @version 0.1
|
|
* @date 2026-05-17
|
|
*
|
|
* @copyright Copyright (c) 2026
|
|
*
|
|
*/
|
|
#include "demo.h"
|
|
|
|
static void demo_arraylist_num(void)
|
|
{
|
|
int i = 0;
|
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
|
int temp = 0;
|
|
int index = 0;
|
|
int len = sizeof(data) / sizeof(data[0]);
|
|
|
|
arraylist_t list = arraylist_new(sizeof(int), 64);
|
|
list->print_obj = print_num;
|
|
|
|
printf("\n\n----- list_demo_num -----\n");
|
|
printf("----- push -----\n");
|
|
for (i = 0; i < len; i++)
|
|
{
|
|
list->append(list, &data[i]);
|
|
}
|
|
printf("----- print -----\n");
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
printf("----- pop -----\n");
|
|
list->remove(list, 9, NULL);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
list->remove(list, 0, NULL);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
list->remove(list, 4, NULL);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
printf("----- clear -----\n");
|
|
list->clear(list);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
printf("----- push -----\n");
|
|
for (i = 0; i < len; i++)
|
|
{
|
|
list->append(list, &data[i]);
|
|
}
|
|
printf("----- print -----\n");
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
printf("----- get -----\n");
|
|
index = 0;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
index = 4;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
index = 9;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
|
|
printf("----- set -----\n");
|
|
index = 0;
|
|
temp = 11;
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
index = 4;
|
|
temp = 22;
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
index = 9;
|
|
temp = 33;
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
printf("----- print -----\n");
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
|
|
printf("----- at like python -----\n");
|
|
index = -1;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
index = -6;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
index = -10;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
printf("----- set like python -----\n");
|
|
index = -1;
|
|
temp = 99;
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
index = -6;
|
|
temp = 98;
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
index = -10;
|
|
temp = 97;
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = %2d\n", index, temp);
|
|
|
|
printf("----- print -----\n");
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
printf("----- pop -----\n");
|
|
for (i = 0; i < len + 1; i++)
|
|
{
|
|
list->pop(list, &temp);
|
|
|
|
if (list->empty(list))
|
|
{
|
|
printf("----- empty -----\n");
|
|
break;
|
|
}
|
|
}
|
|
|
|
arraylist_free(&list);
|
|
}
|
|
|
|
static void demo_arraylist_struct(void)
|
|
{
|
|
int i = 0;
|
|
struct _student data[] = {
|
|
"zhao", 1001, "qian", 1002, "sun", 1003, "li", 1004,
|
|
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
|
|
"feng", 1009, "cheng",1010,
|
|
};
|
|
struct _student temp = {0};
|
|
int index = 0;
|
|
int len = sizeof(data) / sizeof(data[0]);
|
|
|
|
arraylist_t list = arraylist_new(sizeof(struct _student), 64);
|
|
list->print_obj = print_struct;
|
|
|
|
printf("\n\n----- list_demo_num -----\n");
|
|
printf("----- push -----\n");
|
|
for (i = 0; i < len; i++)
|
|
{
|
|
list->append(list, &data[i]);
|
|
}
|
|
printf("----- print -----\n");
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
printf("----- pop -----\n");
|
|
list->remove(list, 9, NULL);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
list->remove(list, 0, NULL);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
list->remove(list, 4, NULL);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
printf("----- clear -----\n");
|
|
list->clear(list);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
printf("----- push -----\n");
|
|
for (i = 0; i < len; i++)
|
|
{
|
|
list->append(list, &data[i]);
|
|
}
|
|
printf("----- print -----\n");
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
printf("----- at -----\n");
|
|
index = 0;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
index = 4;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
index = 9;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
|
|
printf("----- set -----\n");
|
|
index = 0;
|
|
temp.id = 11;
|
|
sprintf(temp.name, "robot_%02d", temp.id);
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
index = 4;
|
|
temp.id = 22;
|
|
sprintf(temp.name, "robot_%02d", temp.id);
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
index = 9;
|
|
temp.id = 33;
|
|
sprintf(temp.name, "robot_%02d", temp.id);
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
printf("----- print -----\n");
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
|
|
printf("----- at like python -----\n");
|
|
index = -1;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
index = -6;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
index = -10;
|
|
list->get(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
printf("----- set like python -----\n");
|
|
index = -1;
|
|
temp.id = 99;
|
|
sprintf(temp.name, "robot_%02d", temp.id);
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
index = -6;
|
|
temp.id = 98;
|
|
sprintf(temp.name, "robot_%02d", temp.id);
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
index = -10;
|
|
//temp.id = 97;
|
|
//sprintf(temp.name, "robot_%02d", temp.id);
|
|
// temp = data[0];
|
|
// struct _student robot = {"robot", 97};
|
|
// temp = robot;
|
|
temp = (struct _student){"robot", 97};
|
|
list->set(list, index, &temp);
|
|
printf("list[%4d] = ", index);
|
|
list->print_obj(&temp); printf("\n");
|
|
|
|
printf("----- print -----\n");
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
printf("----- pop -----\n");
|
|
for (i = 0; i < len + 1; i++)
|
|
{
|
|
list->pop(list, &temp);
|
|
|
|
if (list->empty(list))
|
|
{
|
|
printf("----- empty -----\n");
|
|
break;
|
|
}
|
|
}
|
|
|
|
arraylist_free(&list);
|
|
}
|
|
|
|
void demo_arraylist(void)
|
|
{
|
|
demo_arraylist_num();
|
|
demo_arraylist_struct();
|
|
}
|