diff --git a/APP_Framework/Applications/app_test/test_hash.c b/APP_Framework/Applications/app_test/test_hash.c index 3979bb4b6..45f4d35b5 100644 --- a/APP_Framework/Applications/app_test/test_hash.c +++ b/APP_Framework/Applications/app_test/test_hash.c @@ -23,8 +23,99 @@ #include #include #ifdef ADD_XIZI_FETURES -int testa[4]; -int i = 0; +// 初始化哈希表,一共有10个元素,其值为0表示为空 +struct hash_node{ + int val; + struct hash_node *next; +}; +typedef struct hash_node hash_list; + +// 创建哈希表 +hash_list* hash_table[10]; +void InitHash() +{ + // 起到初始化作用并且起到清空作用 + for (int i = 0; i < 10;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) +{ + printf("delete.\n"); +} + +//打印哈希表 +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[]) { @@ -36,10 +127,7 @@ void TestHash(int argc, char *argv[]) // printf("the commend is %s.\n",argv[arg]); if (strcmp(argv[arg],"--insert") == 0) { - testa[i] = atoi(argv[arg + 1]); - i++; - for (int j = 0; j < 4; j++) - printf("testa[%d]:%d\n",j,testa[j]); + 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) @@ -49,13 +137,20 @@ void TestHash(int argc, char *argv[]) } if (strcmp(argv[arg],"--search") == 0) { - // TODO:实现hash的查找操作 - printf("search hash table."); + 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],"--update") == 0) + if (strcmp(argv[arg],"--print") == 0) { - // TODO:实现hash的修改操作 - printf("update index is %s.\n",argv[arg + 1]); + PrintHash(); + } + if (strcmp(argv[arg],"--clean") == 0) + { + InitHash(); + printf("The hash table is emptied.\n"); } } }