demo
This commit is contained in:
parent
8c8d762cdc
commit
a09723fcef
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue