diff --git a/APP_Framework/Applications/app_test/test_hash/1.png b/APP_Framework/Applications/app_test/test_hash/1.png new file mode 100644 index 000000000..7af2404eb Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/1.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/2.png b/APP_Framework/Applications/app_test/test_hash/2.png new file mode 100644 index 000000000..2ea3c1a7f Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/2.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/test1.png b/APP_Framework/Applications/app_test/test_hash/test1.png new file mode 100644 index 000000000..77adde014 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/test1.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/test2.png b/APP_Framework/Applications/app_test/test_hash/test2.png new file mode 100644 index 000000000..2f0625746 Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/test2.png differ diff --git a/APP_Framework/Applications/app_test/test_hash/test3.png b/APP_Framework/Applications/app_test/test_hash/test3.png new file mode 100644 index 000000000..d96bf798b Binary files /dev/null and b/APP_Framework/Applications/app_test/test_hash/test3.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 c3909f49d..bb5b538d3 100644 --- a/APP_Framework/Applications/app_test/test_hash/test_hash.c +++ b/APP_Framework/Applications/app_test/test_hash/test_hash.c @@ -1,144 +1,144 @@ -/* -* Copyright (c) 2020 AIIT Ubiquitous Team -* 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 基于矽璓模拟器cortex-m3-emulator,实现哈希表,支持链地址法解决哈希冲突 -* @description 输入:首先输入关键字数量,然后输入关键字,最后输入需要查询的关键字进行查询。采用除留余数法构造哈希函数,冲突解决采用链地址法。 -* @version 1.0 -* @author 查无此队 -* @date 2023-06-21 -*************************************************/ - - -#include -#include -#include -#ifdef ADD_XIZI_FETURES -/*定义哈希表所能存储结点个数*/ -#define MAX_SIZE 10000L - -/* 哈希表链节点实现数据结构 */ -/* key 关键字 */ -/* next 该节点指向的下一个节点 */ -struct HashMap { - int key; - struct HashMap *next; -}; - -/* 哈希表节点数组,存储所有节点 */ -struct HashMap *hash_map[MAX_SIZE]; - -/** -* @Description 负责构造哈希链表 -* @param head - 当前哈希链表节点 -* @param key - 需要插入的关键字 -* @return 成功:插入完毕的当前哈希链表节点,失败:插入完毕的当前哈希链表节点 -*/ -struct HashMap *InsertKey(struct HashMap *head, int key) -{ - /* 将关键字组装为待插入的链节点 */ - struct HashMap *insert_node = (struct HashMap *) malloc(sizeof(struct HashMap)); - insert_node->key = key; - - /* 如果节点为空,则直接插入到head后,反之则根据除留余数法移动到合适位置 */ - if (head == NULL) { - head = insert_node; - insert_node->next = NULL; - } else { - /* 用于while查询位置使用的指针 */ - struct HashMap * p1 = head; - struct HashMap * p2 = NULL; - - while ((insert_node->key > p1->key) && (p1->next != NULL)){ - p2 = p1; - p1 = p1->next; - } - - if (insert_node->key <= p1->key) { - /* 插入到第一个节点之前,否则插入到p2指向的节点之后 */ - if (head == p1) - head = insert_node; - else - p2->next = insert_node; - - insert_node->next = p1; - } else{ - /* 插入到结尾处 */ - p1->next = insert_node; - insert_node->next = NULL; - } - } - - return head; -} - -/** -* @Description 接收输入参数,通过循环调用InsertKey函数插入关键字构造哈希表 -* @param size - 要构造的哈希表大小 -* @return 成功:插入完毕的当前哈希链表节点,失败:插入完毕的当前哈希链表节点 -*/ -void CreateHashMap(int size) { - for (int i = 0; i < size; i++) - hash_map[i] = NULL; - - for (int i = 0; i < size; i++) { - int key; - scanf("%d", &key); - /* 吸收回车符 */ - getchar(); - /* 采用除留余数法确定位置 */ - int location = key % (size + 1); - hash_map[location] = InsertKey(hash_map[location], key); - } -} - -/** -* @Description 打印一个哈希表节点 -* @param head - 要打印的哈希表节点 -*/ -void PrintHashMap(struct HashMap *head) -{ - struct HashMap *p = head; - if(head != NULL){ - do{ - printf(" -> %d ",p->key); - p = p->next; - } while(p != NULL); - } else{ - printf("null"); - } -} - -/** -* @Description 测试哈希表 -* @param size - 要构造的哈希表大小 -*/ -void TestHash(void) -{ - int size; - scanf("%d",&size); - getchar(); - CreateHashMap(size); - - printf("采用链地址法得到的哈希表为:\n"); - for (int i = 0; i < size + 1; i++) { - printf("第%d行:", i); - PrintHashMap(hash_map[i]); - printf("\n"); - } - - return ; -} -PRIV_SHELL_CMD_FUNCTION(TestHash, a hash_map test sample, PRIV_SHELL_CMD_MAIN_ATTR); +/* +* Copyright (c) 2020 AIIT Ubiquitous Team +* 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 基于矽璓模拟器cortex-m3-emulator,实现哈希表,支持链地址法解决哈希冲突 +* @description 输入:首先输入关键字数量,然后输入关键字,最后输入需要查询的关键字进行查询。采用除留余数法构造哈希函数,冲突解决采用链地址法。 +* @version 1.0 +* @author 查无此队 +* @date 2023-06-21 +*************************************************/ + + +#include +#include +#include +#ifdef ADD_XIZI_FETURES +/*定义哈希表所能存储结点个数*/ +#define MAX_SIZE 10000L + +/* 哈希表链节点实现数据结构 */ +/* key 关键字 */ +/* next 该节点指向的下一个节点 */ +struct HashMap { + int key; + struct HashMap *next; +}; + +/* 哈希表节点数组,存储所有节点 */ +struct HashMap *hash_map[MAX_SIZE]; + +/** +* @Description 负责构造哈希链表 +* @param head - 当前哈希链表节点 +* @param key - 需要插入的关键字 +* @return 成功:插入完毕的当前哈希链表节点,失败:插入完毕的当前哈希链表节点 +*/ +struct HashMap *InsertKey(struct HashMap *head, int key) +{ + /* 将关键字组装为待插入的链节点 */ + struct HashMap *insert_node = (struct HashMap *) malloc(sizeof(struct HashMap)); + insert_node->key = key; + + /* 如果节点为空,则直接插入到head后,反之则根据除留余数法移动到合适位置 */ + if (head == NULL) { + head = insert_node; + insert_node->next = NULL; + } else { + /* 用于while查询位置使用的指针 */ + struct HashMap * p1 = head; + struct HashMap * p2 = NULL; + + while ((insert_node->key > p1->key) && (p1->next != NULL)){ + p2 = p1; + p1 = p1->next; + } + + if (insert_node->key <= p1->key) { + /* 插入到第一个节点之前,否则插入到p2指向的节点之后 */ + if (head == p1) + head = insert_node; + else + p2->next = insert_node; + + insert_node->next = p1; + } else{ + /* 插入到结尾处 */ + p1->next = insert_node; + insert_node->next = NULL; + } + } + + return head; +} + +/** +* @Description 接收输入参数,通过循环调用InsertKey函数插入关键字构造哈希表 +* @param size - 要构造的哈希表大小 +* @return 成功:插入完毕的当前哈希链表节点,失败:插入完毕的当前哈希链表节点 +*/ +void CreateHashMap(int size) { + for (int i = 0; i < size; i++) + hash_map[i] = NULL; + + for (int i = 0; i < size; i++) { + int key; + scanf("%d", &key); + /* 吸收回车符 */ + getchar(); + /* 采用除留余数法确定位置 */ + int location = key % (size + 1); + hash_map[location] = InsertKey(hash_map[location], key); + } +} + +/** +* @Description 打印一个哈希表节点 +* @param head - 要打印的哈希表节点 +*/ +void PrintHashMap(struct HashMap *head) +{ + struct HashMap *p = head; + if(head != NULL){ + do{ + printf(" -> %d ",p->key); + p = p->next; + } while(p != NULL); + } else{ + printf("null"); + } +} + +/** +* @Description 测试哈希表 +* @param size - 要构造的哈希表大小 +*/ +void TestHash(void) +{ + int size; + scanf("%d",&size); + getchar(); + CreateHashMap(size); + + printf("采用链地址法得到的哈希表为:\n"); + for (int i = 0; i < size + 1; i++) { + printf("第%d行:", i); + PrintHashMap(hash_map[i]); + printf("\n"); + } + + return ; +} +PRIV_SHELL_CMD_FUNCTION(TestHash, a hash_map test sample, PRIV_SHELL_CMD_MAIN_ATTR); #endif \ No newline at end of file