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