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 index 6207db80e..ff139bbe5 100644 --- a/APP_Framework/Applications/app_test/test_hash/test_hash.c +++ b/APP_Framework/Applications/app_test/test_hash/test_hash.c @@ -21,15 +21,10 @@ #include #include #include +#include "test_hash.h" #include #ifdef ADD_XIZI_FETURES -#define HashTableSize 10 -// 初始化哈希表,一共有10个元素,其值为0表示为空 -typedef struct HashNode{ - int val; - struct HashNode *next; -}hash_list; // 创建哈希表 @@ -129,6 +124,7 @@ void DeleteHash(int value) prenode->next = temp->next; } free(temp); + temp = NULL; printf("Delete the corresponding value in the hash table.\n"); return ; } 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[]); + +