add delete func
This commit is contained in:
parent
361054aa90
commit
c46a1bd412
|
@ -102,7 +102,7 @@ ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
|
|||
endif
|
||||
|
||||
ifeq ($(CONFIG_USER_TEST_HASH),y)
|
||||
SRC_FILES += test_hash.c
|
||||
SRC_FILES += test_hash/test_hash.c
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
|
@ -12,42 +12,47 @@
|
|||
|
||||
/**
|
||||
* @file: test_hash.c
|
||||
* @brief: a application of adc function
|
||||
* @brief: a application of HashTable function
|
||||
* @version: 1.1
|
||||
* @author: AIIT XUOS Lab
|
||||
* @author: NWPUWSN
|
||||
* @date: 2023/6/22
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <transform.h>
|
||||
#include <string.h>
|
||||
#include <transform.h>
|
||||
#ifdef ADD_XIZI_FETURES
|
||||
#define HashTableSize 10
|
||||
|
||||
// 初始化哈希表,一共有10个元素,其值为0表示为空
|
||||
struct hash_node{
|
||||
typedef struct HashNode{
|
||||
int val;
|
||||
struct hash_node *next;
|
||||
};
|
||||
typedef struct hash_node hash_list;
|
||||
struct HashNode *next;
|
||||
}hash_list;
|
||||
|
||||
|
||||
// 创建哈希表
|
||||
hash_list* hash_table[10];
|
||||
hash_list* hash_table[HashTableSize];
|
||||
void InitHash()
|
||||
{
|
||||
// 起到初始化作用并且起到清空作用
|
||||
for (int i = 0; i < 10;i ++)
|
||||
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)
|
||||
{
|
||||
|
@ -71,12 +76,14 @@ void InsertHash(int key, int value)
|
|||
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)
|
||||
{
|
||||
|
@ -94,10 +101,41 @@ int SearchHash(int value)
|
|||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//删除哈希表中某个元素
|
||||
void DeleteHash(int value)
|
||||
{
|
||||
printf("delete.\n");
|
||||
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);
|
||||
printf("Delete the corresponding value in the hash table.\n");
|
||||
return ;
|
||||
}
|
||||
prenode = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//打印哈希表
|
||||
|
@ -124,7 +162,6 @@ void TestHash(int argc, char *argv[])
|
|||
// 对哈希表的一些操作
|
||||
for (int arg = 0; arg < argc; arg ++)
|
||||
{
|
||||
// printf("the commend is %s.\n",argv[arg]);
|
||||
if (strcmp(argv[arg],"--insert") == 0)
|
||||
{
|
||||
InsertHash(ComputeHash(atoi(argv[arg + 1])), atoi(argv[arg + 1]));
|
||||
|
@ -132,8 +169,9 @@ void TestHash(int argc, char *argv[])
|
|||
}
|
||||
if (strcmp(argv[arg],"--delete") == 0)
|
||||
{
|
||||
// TODO:实现hash的删除操作
|
||||
printf("delete index is %s.\n",argv[arg + 1]);
|
||||
printf("delete index is %d.\n",ComputeHash(atoi(argv[arg + 1])));
|
||||
DeleteHash(atoi(argv[arg + 1]));
|
||||
|
||||
}
|
||||
if (strcmp(argv[arg],"--search") == 0)
|
||||
{
|
Loading…
Reference in New Issue