This commit is contained in:
zyj879580 2023-06-23 14:01:07 +08:00
parent 8c8d762cdc
commit a09723fcef
4 changed files with 154 additions and 2 deletions

View File

@ -1,7 +1,7 @@
menu "test app"
menuconfig USER_TEST
bool "Enable application test function "
default n
default y
if USER_TEST
menuconfig USER_TEST_ADC
@ -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 y
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,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<stdio.h>
#include<stdlib.h>
#include<transform.h>
#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

View File

@ -20,7 +20,7 @@ extern int FrameworkInit();
extern void ApplicationOtaTaskInit(void);
int main(void)
{
printf("Hello, world!zyj233 \n");
printf("Hello, world! \n");
FrameworkInit();
#ifdef APPLICATION_OTA
ApplicationOtaTaskInit();