2024-06-21 05:25:01 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "list.h"
|
2024-06-21 07:21:42 +00:00
|
|
|
#include "queue.h"
|
2024-12-16 15:40:13 +00:00
|
|
|
#include "hash_table.h"
|
2024-06-21 05:25:01 +00:00
|
|
|
|
|
|
|
typedef struct test {
|
|
|
|
int val1;
|
|
|
|
float val2;
|
|
|
|
} test_t;
|
|
|
|
|
|
|
|
void handle(void *data) {
|
|
|
|
test_t *test = (test_t *) data;
|
|
|
|
printf("val1:%d, val2:%f\n", test->val1, test->val2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare_int(const void *s1, const void *s2) {
|
|
|
|
test_t *data1 = (test_t *) s1;
|
|
|
|
int *data2 = (int *) s2;
|
|
|
|
|
|
|
|
return (data1->val1 - *data2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int compare_int_sort(const void *s1, const void *s2) {
|
|
|
|
test_t *data1 = (test_t *) s1;
|
|
|
|
test_t *data2 = (test_t *) s2;
|
|
|
|
|
|
|
|
return (data1->val1 - data2->val1);
|
|
|
|
}
|
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
void print(List_t *list) {
|
2024-06-21 05:25:01 +00:00
|
|
|
printf("list len = %ld\n", list_get_lenth(list));
|
|
|
|
if (!list_is_empty(list)) {
|
|
|
|
//test list_reverse
|
|
|
|
list_traverse(list, handle);
|
|
|
|
} else {
|
|
|
|
printf("\tthe list is empty\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-21 15:07:22 +00:00
|
|
|
int Test_List(void *pVoid) {
|
2024-06-21 07:21:42 +00:00
|
|
|
List_t list;
|
|
|
|
list_init(&list); // 初始化链表
|
|
|
|
|
2024-06-21 05:25:01 +00:00
|
|
|
test_t test1 = {10, 10.5};
|
|
|
|
test_t test2 = {20, 20.5};
|
|
|
|
test_t test3 = {30, 30.5};
|
|
|
|
test_t test4 = {40, 40.5};
|
|
|
|
test_t test5 = {50, 50.5};
|
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
// 测试插入操作
|
2024-06-21 05:25:01 +00:00
|
|
|
printf("------insert(_at_tail)----\n");
|
2024-06-21 07:21:42 +00:00
|
|
|
list_insert(&list, &test1); // 在尾部插入test1
|
|
|
|
list_insert(&list, &test2); // 在尾部插入test2
|
|
|
|
list_insert(&list, &test3); // 在尾部插入test3
|
|
|
|
print(&list); // 打印链表
|
2024-06-21 05:25:01 +00:00
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
// 测试删除操作
|
2024-06-21 05:25:01 +00:00
|
|
|
printf("------delete----\n");
|
2024-06-21 07:21:42 +00:00
|
|
|
list_delete(&list, &test1.val1, compare_int); // 删除值为test1.val1的节点
|
|
|
|
print(&list); // 打印链表
|
2024-06-21 05:25:01 +00:00
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
// 测试在头部插入操作
|
2024-06-21 05:25:01 +00:00
|
|
|
printf("------insert_at_head----\n");
|
2024-06-21 07:21:42 +00:00
|
|
|
list_insert_at_head(&list, &test4); // 在头部插入test4
|
|
|
|
print(&list); // 打印链表
|
2024-06-21 05:25:01 +00:00
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
// 测试在指定位置插入操作
|
2024-06-21 05:25:01 +00:00
|
|
|
printf("------insert_at_index(2)----\n");
|
2024-06-21 07:21:42 +00:00
|
|
|
list_insert_at_index(&list, &test5, 2); // 在索引2处插入test5
|
|
|
|
print(&list); // 打印链表
|
2024-06-21 05:25:01 +00:00
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
// 测试链表反转操作
|
2024-06-21 05:25:01 +00:00
|
|
|
printf("------reverse----\n");
|
2024-06-21 07:21:42 +00:00
|
|
|
list_reverse(&list); // 反转链表
|
|
|
|
print(&list); // 打印链表
|
2024-06-21 05:25:01 +00:00
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
// 测试搜索操作
|
2024-06-21 05:25:01 +00:00
|
|
|
int key = 20;
|
|
|
|
test_t *ret;
|
|
|
|
printf("------search----\n");
|
2024-06-21 07:21:42 +00:00
|
|
|
ret = list_search(&list, &key, compare_int); // 搜索值为key的节点
|
|
|
|
printf("%d:%f\n", ret->val1, ret->val2); // 打印搜索结果
|
2024-06-21 05:25:01 +00:00
|
|
|
key = 50;
|
2024-06-21 07:21:42 +00:00
|
|
|
ret = list_search(&list, &key, compare_int); // 搜索值为key的节点
|
|
|
|
printf("%d:%f\n", ret->val1, ret->val2); // 打印搜索结果
|
2024-06-21 05:25:01 +00:00
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
// 测试获取指定位置元素操作
|
2024-06-21 05:25:01 +00:00
|
|
|
printf("------list_get_element----\n");
|
2024-06-21 07:21:42 +00:00
|
|
|
ret = list_get_element(&list, 2); // 获取索引为2的元素
|
|
|
|
printf("%d:%f\n", ret->val1, ret->val2); // 打印获取的元素
|
|
|
|
ret = list_get_element(&list, 3); // 获取索引为3的元素
|
|
|
|
printf("%d:%f\n", ret->val1, ret->val2); // 打印获取的元素
|
2024-06-21 05:25:01 +00:00
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
// 测试排序操作
|
2024-06-21 05:25:01 +00:00
|
|
|
printf("-----sort----\n");
|
2024-06-21 07:21:42 +00:00
|
|
|
list_sort(&list, compare_int_sort); // 按照整数值排序
|
|
|
|
print(&list); // 打印排序后的链表
|
2024-06-21 05:25:01 +00:00
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
// 测试销毁操作
|
2024-06-21 05:25:01 +00:00
|
|
|
printf("-----destroy----\n");
|
2024-06-21 07:21:42 +00:00
|
|
|
list_destroy(&list, NULL); // 销毁链表
|
2024-12-16 15:40:13 +00:00
|
|
|
return 0;
|
2024-06-21 07:21:42 +00:00
|
|
|
}
|
|
|
|
|
2024-09-21 15:07:22 +00:00
|
|
|
int Test_Queue(void *pVoid) {
|
2024-06-21 07:21:42 +00:00
|
|
|
Queue_List_t *deque = newQueue_List(sizeof(int)); // 创建一个int类型的双端队列
|
|
|
|
|
|
|
|
// 测试入队操作
|
|
|
|
int val1 = 10;
|
|
|
|
pushFirst(deque, &val1); // 将10插入队首
|
|
|
|
char val2 = 'A';
|
|
|
|
pushLast(deque, &val2); // 将字符'A'插入队尾
|
|
|
|
|
|
|
|
// 测试出队操作
|
|
|
|
int *popVal1 = (int *) popFirst(deque); // 从队首出队
|
|
|
|
char *popVal2 = (char *) popLast(deque); // 从队尾出队
|
2024-06-21 05:25:01 +00:00
|
|
|
|
2024-06-21 07:21:42 +00:00
|
|
|
// 打印出队的值
|
|
|
|
printf("Pop value from front: %d\n", *popVal1);
|
|
|
|
printf("Pop value from rear: %c\n", *popVal2);
|
2024-06-21 07:33:39 +00:00
|
|
|
|
|
|
|
float val3 = 10.5;
|
|
|
|
pushFirst(deque, &val3); // 将10.5插入队首
|
|
|
|
double val4 = 20.5;
|
|
|
|
pushLast(deque, &val4); // 将20.5插入队尾
|
|
|
|
float *popVal3 = (float *) popFirst(deque); // 从队首出队
|
|
|
|
double *popVal4 = (double *) popLast(deque); // 从队尾出队
|
|
|
|
printf("Pop value from front: %f\n", *popVal3);
|
|
|
|
printf("Pop value from rear: %lf\n", *popVal4);
|
|
|
|
|
|
|
|
int nums[] = {10, 20, 30, 40, 50};
|
|
|
|
// 测试入队操作
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
pushFirst(deque, &nums[i]); // 将数组元素插入队首
|
|
|
|
}
|
|
|
|
|
|
|
|
// 测试出队操作
|
|
|
|
int *popVal;
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
popVal = (int *) popFirst(deque); // 从队首出队
|
|
|
|
printf("Pop value from front: %d\n", *popVal);
|
|
|
|
}
|
|
|
|
delQueue_List(deque);
|
2024-12-16 15:40:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Test_Hash(void* pVoid) {
|
|
|
|
Hash_Table_t* ht = ht_new();
|
|
|
|
ht_insert(ht, "name", "lydxh");
|
|
|
|
ht_insert(ht, "age", "18");
|
|
|
|
printf("Name:%s\nAge:%s", ht_search(ht, "name"), ht_search(ht, "age"));
|
|
|
|
ht_delete(ht, "name");
|
|
|
|
ht_delete(ht, "age");
|
|
|
|
ht_del_hash_table(ht);
|
|
|
|
return 0;
|
2024-09-21 08:07:31 +00:00
|
|
|
}
|