2023_open_source_contest_warmup_1st_issue1
This commit is contained in:
parent
52c8d940f8
commit
6e9ffb0b39
|
@ -1,9 +1,14 @@
|
||||||
menu "test app"
|
menu "test app"
|
||||||
menuconfig USER_TEST
|
menuconfig USER_TEST
|
||||||
bool "Enable application test function "
|
bool "Enable application test function "
|
||||||
default n
|
default y
|
||||||
|
|
||||||
if USER_TEST
|
if USER_TEST
|
||||||
|
menuconfig USER_HASH_TABLE
|
||||||
|
bool "Config test hash table"
|
||||||
|
default y
|
||||||
|
|
||||||
|
|
||||||
menuconfig USER_TEST_ADC
|
menuconfig USER_TEST_ADC
|
||||||
bool "Config test adc"
|
bool "Config test adc"
|
||||||
default n
|
default n
|
||||||
|
@ -240,5 +245,6 @@ menu "test app"
|
||||||
bool "Config test soft timer"
|
bool "Config test soft timer"
|
||||||
default n
|
default n
|
||||||
|
|
||||||
|
|
||||||
endif
|
endif
|
||||||
endmenu
|
endmenu
|
||||||
|
|
|
@ -101,5 +101,9 @@ ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
|
||||||
SRC_FILES += test_timer.c
|
SRC_FILES += test_timer.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_USER_HASH_TABLE),y)
|
||||||
|
SRC_FILES += test_hash/test_hash.c
|
||||||
|
endif
|
||||||
|
|
||||||
include $(KERNEL_ROOT)/compiler.mk
|
include $(KERNEL_ROOT)/compiler.mk
|
||||||
endif
|
endif
|
||||||
|
|
|
@ -18,11 +18,12 @@ v2.
|
||||||
* @author: Jager
|
* @author: Jager
|
||||||
* @date: 2023/6/17
|
* @date: 2023/6/17
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include<transform.h>
|
#include<transform.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include<string.h>
|
#include<string.h>
|
||||||
#include"test_hash.h"
|
|
||||||
|
|
||||||
#ifdef ADD_XIZI_FETURES
|
#ifdef ADD_XIZI_FETURES
|
||||||
|
|
||||||
|
@ -40,6 +41,7 @@ typedef struct HashTable {
|
||||||
Node** buckets;
|
Node** buckets;
|
||||||
} HashTable;
|
} HashTable;
|
||||||
|
|
||||||
|
|
||||||
// 创建节点
|
// 创建节点
|
||||||
Node* createNode(int key, int value) {
|
Node* createNode(int key, int value) {
|
||||||
Node* newNode = (Node*)malloc(sizeof(Node));
|
Node* newNode = (Node*)malloc(sizeof(Node));
|
||||||
|
@ -49,9 +51,11 @@ Node* createNode(int key, int value) {
|
||||||
return newNode;
|
return newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建哈希表
|
HashTable* hashTable =NULL;
|
||||||
|
|
||||||
|
// initial
|
||||||
HashTable* createHashTable() {
|
HashTable* createHashTable() {
|
||||||
HashTable* hashTable = (HashTable*)malloc(sizeof(HashTable));
|
hashTable = (HashTable*)malloc(sizeof(HashTable));
|
||||||
hashTable->buckets = (Node**)malloc(sizeof(Node*) * TABLE_SIZE);
|
hashTable->buckets = (Node**)malloc(sizeof(Node*) * TABLE_SIZE);
|
||||||
for (int i = 0; i < TABLE_SIZE; i++) {
|
for (int i = 0; i < TABLE_SIZE; i++) {
|
||||||
hashTable->buckets[i] = NULL;
|
hashTable->buckets[i] = NULL;
|
||||||
|
@ -64,24 +68,6 @@ int hashFunction(int key) {
|
||||||
return key % TABLE_SIZE;
|
return key % TABLE_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 插入操作
|
|
||||||
void insert(HashTable* hashTable, int key, int value) {
|
|
||||||
int index = hashFunction(key);
|
|
||||||
Node* newNode = createNode(key, value);
|
|
||||||
|
|
||||||
// 如果桶为空,则直接插入
|
|
||||||
if (hashTable->buckets[index] == NULL) {
|
|
||||||
hashTable->buckets[index] = newNode;
|
|
||||||
}
|
|
||||||
else { // 否则在链表尾部插入
|
|
||||||
Node* current = hashTable->buckets[index];
|
|
||||||
while (current->next != NULL) {
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
current->next = newNode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查找操作
|
// 查找操作
|
||||||
int search(HashTable* hashTable, int key) {
|
int search(HashTable* hashTable, int key) {
|
||||||
int index = hashFunction(key);
|
int index = hashFunction(key);
|
||||||
|
@ -97,8 +83,42 @@ int search(HashTable* hashTable, int key) {
|
||||||
return -1; // 未找到
|
return -1; // 未找到
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 插入操作
|
||||||
|
void insert(HashTable* hashTable, int key, int value) {
|
||||||
|
//查找表中是否存在key
|
||||||
|
int val = search(hashTable, key);
|
||||||
|
if(val==value)return;//存在(key,value)直接返回
|
||||||
|
else if(val==-1){//不存在key直接插入
|
||||||
|
int index = hashFunction(key);
|
||||||
|
Node* newNode = createNode(key, value);
|
||||||
|
// 如果桶为空,则直接插入
|
||||||
|
if (hashTable->buckets[index] == NULL) {
|
||||||
|
hashTable->buckets[index] = newNode;
|
||||||
|
}
|
||||||
|
else { // 否则在链表尾部插入
|
||||||
|
Node* current = hashTable->buckets[index];
|
||||||
|
while (current->next != NULL) {
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
current->next = newNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{//存在key但value不同,替换value
|
||||||
|
int index = hashFunction(key);
|
||||||
|
Node* current = hashTable->buckets[index];
|
||||||
|
while (current != NULL) {
|
||||||
|
if (current->key == key) {
|
||||||
|
current->value=value;
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 删除操作
|
// 删除操作
|
||||||
void removeEntry(HashTable* hashTable, int key) {
|
int removeEntry(HashTable* hashTable, int key) {
|
||||||
|
int value = search(hashTable, key);
|
||||||
|
if(value==-1)return 0;
|
||||||
int index = hashFunction(key);
|
int index = hashFunction(key);
|
||||||
Node* current = hashTable->buckets[index];
|
Node* current = hashTable->buckets[index];
|
||||||
Node* prev = NULL;
|
Node* prev = NULL;
|
||||||
|
@ -112,12 +132,12 @@ void removeEntry(HashTable* hashTable, int key) {
|
||||||
prev->next = current->next;
|
prev->next = current->next;
|
||||||
}
|
}
|
||||||
free(current);
|
free(current);
|
||||||
return;
|
return 1;
|
||||||
}
|
}
|
||||||
prev = current;
|
prev = current;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打印哈希表
|
// 打印哈希表
|
||||||
|
@ -134,30 +154,67 @@ void printHashTable(HashTable* hashTable) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试
|
// 测试
|
||||||
void TestHash() {
|
void TestHash(int argc, char *argv[]) {
|
||||||
HashTable* hashTable = createHashTable();
|
|
||||||
|
|
||||||
// 插入操作
|
//显示传入参数
|
||||||
insert(hashTable, 5, 10);
|
printf("%dparameter(s): ", argc);
|
||||||
insert(hashTable, 2, 20);
|
for (char i = 1; i < argc; i++) {
|
||||||
insert(hashTable, 7, 30);
|
printf("%s ", argv[i]);
|
||||||
insert(hashTable, 12, 40);
|
}
|
||||||
insert(hashTable, 15, 50);
|
printf("\n");
|
||||||
|
if(argc==1){
|
||||||
|
printf("Enter TestHash h for help\n");
|
||||||
|
}
|
||||||
|
|
||||||
// 查找操作
|
|
||||||
printf("Value at key 7: %d\n", search(hashTable, 7));
|
// 1.insert
|
||||||
printf("Value at key 12: %d\n", search(hashTable, 12));
|
if(argc==4 && argv[1][0]=='1'){
|
||||||
printf("Value at key 9: %d\n", search(hashTable, 9));
|
int key=atoi(argv[2]);int value=atoi(argv[3]);
|
||||||
|
insert(hashTable, key, value);
|
||||||
// 删除操作
|
printf("insert key(%d),value(%d) success\n",key, value);
|
||||||
removeEntry(hashTable, 7);
|
}
|
||||||
removeEntry(hashTable, 12);
|
// 2.delete
|
||||||
|
else if(argc==3 && argv[1][0]=='2'){
|
||||||
// 打印哈希表
|
int key=atoi(argv[2]);
|
||||||
printHashTable(hashTable);
|
// 删除操作
|
||||||
|
int flag=removeEntry(hashTable, key);
|
||||||
|
if(flag){
|
||||||
|
printf("delete key(%d) success\n",key);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
printf("delete key(%d) failed\n",key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 3.search
|
||||||
|
else if(argc==3 && argv[1][0]=='3'){
|
||||||
|
int key=atoi(argv[2]);
|
||||||
|
// 查找操作
|
||||||
|
int value=search(hashTable, key);
|
||||||
|
if(value==-1){
|
||||||
|
printf("query key(%d),not exist\n",key);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
printf("query key(%d),the answer is : %d\n",key,value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 4.display
|
||||||
|
else if(argc==2 && argv[1][0]=='4'){
|
||||||
|
// 打印哈希表
|
||||||
|
printHashTable(hashTable);
|
||||||
|
}
|
||||||
|
// 5.help
|
||||||
|
else if(argc==2 && argv[1][0]=='h'){
|
||||||
|
printf("1:insert(key,value)\n2:delete(key)\n3:search(key)\n4:dispaly the table\nh : help\n");
|
||||||
|
}
|
||||||
|
// error
|
||||||
|
else if(argc>1){
|
||||||
|
printf("error\n");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PRIV_SHELL_CMD_FUNCTION(TestHash, a hash test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
PRIV_SHELL_CMD_FUNCTION(TestHash, a hash test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||||
#endif
|
PRIV_SHELL_CMD_FUNCTION(createHashTable, initial, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue