diff --git a/APP_Framework/Applications/app_test/Kconfig b/APP_Framework/Applications/app_test/Kconfig index 45df5f5d5..1298d10b5 100644 --- a/APP_Framework/Applications/app_test/Kconfig +++ b/APP_Framework/Applications/app_test/Kconfig @@ -239,6 +239,10 @@ menu "test app" menuconfig USER_TEST_TIMER bool "Config test soft timer" default n + + menuconfig USER_TEST_HASH + bool "Config test hash" + default n endif endmenu diff --git a/APP_Framework/Applications/app_test/Makefile b/APP_Framework/Applications/app_test/Makefile index 1cf919846..c588f9203 100644 --- a/APP_Framework/Applications/app_test/Makefile +++ b/APP_Framework/Applications/app_test/Makefile @@ -101,5 +101,9 @@ ifeq ($(CONFIG_ADD_XIZI_FETURES),y) SRC_FILES += test_timer.c endif + ifeq ($(CONFIG_USER_TEST_HASH),y) + SRC_FILES += test_hash/test_hash.c + endif + include $(KERNEL_ROOT)/compiler.mk endif diff --git a/APP_Framework/Applications/app_test/readme.md b/APP_Framework/Applications/app_test/readme.md new file mode 100644 index 000000000..e69de29bb diff --git a/APP_Framework/Applications/app_test/test_hash/README.md b/APP_Framework/Applications/app_test/test_hash/README.md new file mode 100755 index 000000000..23d7754c7 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_hash/README.md @@ -0,0 +1,52 @@ +# ##**基于cortex-m3-emulator实现哈希表并测试验证**## + +## 1. 简介 + +基于矽璓模拟器cortex-m3-emulator,实现哈希表,并支持链地址法解决哈希冲突,编写测试程序进行验证 + +## 2. 数据结构设计说明 +``` +//定义哈希表的节点 +typedef struct HashNode{ + int val; + struct HashNode *next; +}hash_list; + +``` +``` +//定义哈希表的大小 +#define HashTableSize 10 +// 创建哈希表 + hash_list* hash_table[HashTableSize]; +``` +``` +//函数定义说明 +void InitHash();//创建哈希表 +int ComputeHash(int value);//哈希函数,返回在哈希表中的下标 +void InsertHash(int key, int value);//根据key寻找元素在哈希表中的位置,并且使用链地址法解决哈希冲突 +void DeleteHash(int key);//删除哈希表中某个元素 +int SearchHash(int key);//根据value寻找在哈希表中的位置,返回值为在取模后的第几个 +void PrintHash();//打印哈希表 +void TestHash(int argc, char *argv[]);//测试程序,函数参数为对哈希表的操作以及数字 +``` +## 3. 测试程序说明 +测试程序为TestHash,设置有有参函数,参数设置为字符串数组,可输入包括 insert、delete、search、print和clean命令,并可输入哈希表操作数字,根据输入参数完成哈希表的插入、删除、查找、输出和销毁操作,通过atoi()函数将字符数字转为int类型 + + +## 4. 运行结果(##需结合运行测试截图按步骤说明##) +哈希表插入数字9 +![哈希表插入数字9](image.png) +哈希表插入数字99 +![哈希表插入数字99](image-1.png) +哈希表插入6 +![哈希表插入6](image-2.png) +输出哈希表 +![输出哈希表](image-3.png) +删除元素9 +![删除元素9](image-4.png) +查找元素99在哈希表中的位置 +![查找元素99在哈希表中的位置](image-6.png) +输出删除元素9后的哈希表 +![输出删除元素9后的哈希表](image-5.png) +销毁哈希表并输出 +![销毁哈希表](image-7.png) diff --git a/APP_Framework/Applications/app_test/test_hash/image-1.png b/APP_Framework/Applications/app_test/test_hash/image-1.png new file mode 100644 index 000000000..27d907f6d Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/image-1.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/image-2.png b/APP_Framework/Applications/app_test/test_hash/image-2.png new file mode 100644 index 000000000..3016fd7cd Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/image-2.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/image-3.png b/APP_Framework/Applications/app_test/test_hash/image-3.png new file mode 100644 index 000000000..39b791457 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/image-3.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/image-4.png b/APP_Framework/Applications/app_test/test_hash/image-4.png new file mode 100644 index 000000000..569ec77b6 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/image-4.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/image-5.png b/APP_Framework/Applications/app_test/test_hash/image-5.png new file mode 100644 index 000000000..c95b5a964 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/image-5.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/image-6.png b/APP_Framework/Applications/app_test/test_hash/image-6.png new file mode 100644 index 000000000..711e57b05 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/image-6.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/image-7.png b/APP_Framework/Applications/app_test/test_hash/image-7.png new file mode 100644 index 000000000..1401e2570 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/image-7.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/image.png b/APP_Framework/Applications/app_test/test_hash/image.png new file mode 100644 index 000000000..67ac749a7 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/image.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/test_hash.c b/APP_Framework/Applications/app_test/test_hash/test_hash.c new file mode 100644 index 000000000..ff139bbe5 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_hash/test_hash.c @@ -0,0 +1,197 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file: test_hash.c +* @brief: a application of HashTable function +* @version: 1.1 +* @author: NWPUWSN +* @date: 2023/6/22 +*/ + +#include +#include +#include +#include "test_hash.h" +#include +#ifdef ADD_XIZI_FETURES + + + +// 创建哈希表 + hash_list* hash_table[HashTableSize]; +void InitHash() +{ + // 起到初始化作用并且起到清空作用 + for (int i = 0; i < HashTableSize;i ++) + hash_table[i] = NULL; +} + +// 哈希函数,返回在哈希表中的下标 +int ComputeHash(int value) +{ + return value % 10; +} + +// 根据key寻找元素在哈希表中的位置,并且使用链地址法解决哈希冲突 +void InsertHash(int key, int value) +{ + hash_list *node = NULL; + node = (hash_list*)malloc(sizeof(hash_list)); + + // 没有空间可以分配了 + if (node == NULL) + { + printf("malloc faild!\n"); + return ; + } + memset(node, 0, sizeof(hash_list)); + node->val = value; + node->next = NULL; + if (hash_table[key] == NULL) + { + printf("Good!No conflict.\n"); + hash_table[key] = node; + } + else + { + printf("In the event of a conflict, the chain-address method is used.\n"); + hash_list* temp = hash_table[key]; + while (temp->next != NULL) + temp = temp->next; + temp->next = node; + } +} + +// 根据value寻找在哈希表中的位置,返回值为在取模后的第几个 +int SearchHash(int value) +{ + int index = ComputeHash(value); + int count = 0; + hash_list* temp = hash_table[index]; + + // 对应的index里面没有值 + if (temp == NULL) + { + return -1; + } + else + { + while(temp != NULL) + { + if (temp->val == value) + return count; + temp = temp->next; + count ++ ; + } + return -1; + } +} + +//删除哈希表中某个元素 +void DeleteHash(int value) +{ + int index = ComputeHash(value); + hash_list* temp = hash_table[index]; + hash_list* prenode = NULL; + + // 对应的index里面没有值 + if (temp == NULL) + { + printf("No corresponding value was found in the hash table.\n"); + } + else + { + while(temp != NULL) + { + if (temp->val == value) + { + if (prenode == NULL) + { + hash_table[index] = temp->next; + } + else + { + prenode->next = temp->next; + } + free(temp); + temp = NULL; + printf("Delete the corresponding value in the hash table.\n"); + return ; + } + prenode = temp; + temp = temp->next; + } + } +} + +//打印哈希表 +void PrintHash() +{ + int count = 0; + for (int i = 0; i < 10;i ++) + { + hash_list* temp = hash_table[i]; + while(temp != NULL) + { + printf("hash_table[%d][%d]:%d\n",i, count, temp->val); + count ++ ; + temp = temp -> next; + } + count = 0; + } +} + +void TestHash(int argc, char *argv[]) +{ + if (argc > 1) + { + // 对哈希表的一些操作 + for (int arg = 0; arg < argc; arg ++) + { + if (strcmp(argv[arg],"--insert") == 0) + { + InsertHash(ComputeHash(atoi(argv[arg + 1])), atoi(argv[arg + 1])); + printf("insert hash table,insert element is %s.\n",argv[arg + 1]); + } + if (strcmp(argv[arg],"--delete") == 0) + { + printf("delete index is %d.\n",ComputeHash(atoi(argv[arg + 1]))); + DeleteHash(atoi(argv[arg + 1])); + + } + if (strcmp(argv[arg],"--search") == 0) + { + int col = SearchHash(atoi(argv[arg + 1])); + if (col == -1) + printf("No corresponding value was found in the hash table.\n"); + else + printf("the value in hash_table[%d][%d]",ComputeHash(atoi(argv[arg + 1])),col); + } + if (strcmp(argv[arg],"--print") == 0) + { + PrintHash(); + } + if (strcmp(argv[arg],"--clean") == 0) + { + InitHash(); + printf("The hash table is emptied.\n"); + } + } + } + else + { + printf("nothing to do.\n"); + } +} +PRIV_SHELL_CMD_FUNCTION(TestHash, a hash test sample, PRIV_SHELL_CMD_MAIN_ATTR); +#endif \ No newline at end of file diff --git a/APP_Framework/Applications/app_test/test_hash/test_hash.h b/APP_Framework/Applications/app_test/test_hash/test_hash.h new file mode 100644 index 000000000..a05ec8ecc --- /dev/null +++ b/APP_Framework/Applications/app_test/test_hash/test_hash.h @@ -0,0 +1,37 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file: test_hash.c +* @brief: a application of HashTable function +* @version: 1.1 +* @author: NWPUWSN +* @date: 2023/6/22 +*/ + +// 初始化哈希表,一共有10个元素,其值为0表示为空 +#define HashTableSize 10 + +typedef struct HashNode{ + int val; + struct HashNode *next; +}hash_list; + +void InitHash(); +int ComputeHash(int value); +void InsertHash(int key, int value); +void DeleteHash(int key); +int SearchHash(int key); +void PrintHash(); +void TestHash(int argc, char *argv[]); + +