Merge pull request '2023_open_source_contest_warmup_1st_issue1' (#1) from Develop into 2023_open_source_contest

This commit is contained in:
mashenglong 2023-06-30 18:24:41 +08:00
commit c62383c351
14 changed files with 294 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -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 <stdio.h>
#include <string.h>
#include <string.h>
#include "test_hash.h"
#include <transform.h>
#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

View File

@ -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[]);