mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
306 lines
6.8 KiB
C
306 lines
6.8 KiB
C
/**
|
|
* @file demo_list->c
|
|
* @author wenjf (Orig5826@163.com)
|
|
* @brief
|
|
* @version 0.1
|
|
* @date 2024-08-27
|
|
*
|
|
* @copyright Copyright (c) 2024
|
|
*
|
|
*/
|
|
#include "demo.h"
|
|
|
|
static void demo_list_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]);
|
|
|
|
list_t list = list_new2(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->delete(list, 9, NULL);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
list->delete(list, 0, NULL);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
list->delete(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;
|
|
}
|
|
}
|
|
|
|
list_free(&list);
|
|
}
|
|
|
|
static void demo_list_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]);
|
|
|
|
list_t list = list_new2(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->delete(list, 9, NULL);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
list->delete(list, 0, NULL);
|
|
list->print(list);
|
|
printf("\n");
|
|
|
|
list->delete(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;
|
|
}
|
|
}
|
|
|
|
list_free(&list);
|
|
}
|
|
|
|
void demo_list(void)
|
|
{
|
|
demo_list_num();
|
|
demo_list_struct();
|
|
}
|