2023_open_source_contest_warmup_1st_issue1
This commit is contained in:
parent
7754a149a9
commit
52c8d940f8
|
@ -0,0 +1,163 @@
|
|||
/*
|
||||
* 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: a application of hashtable function
|
||||
* @version: 1.1
|
||||
* @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
|
||||
|
||||
#define TABLE_SIZE 10
|
||||
|
||||
// 哈希节点
|
||||
typedef struct Node {
|
||||
int key;
|
||||
int value;
|
||||
struct Node* next;
|
||||
} Node;
|
||||
|
||||
// 哈希表
|
||||
typedef struct HashTable {
|
||||
Node** buckets;
|
||||
} HashTable;
|
||||
|
||||
// 创建节点
|
||||
Node* createNode(int key, int value) {
|
||||
Node* newNode = (Node*)malloc(sizeof(Node));
|
||||
newNode->key = key;
|
||||
newNode->value = value;
|
||||
newNode->next = NULL;
|
||||
return newNode;
|
||||
}
|
||||
|
||||
// 创建哈希表
|
||||
HashTable* createHashTable() {
|
||||
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;
|
||||
}
|
||||
return hashTable;
|
||||
}
|
||||
|
||||
// 哈希函数
|
||||
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);
|
||||
Node* current = hashTable->buckets[index];
|
||||
|
||||
while (current != NULL) {
|
||||
if (current->key == key) {
|
||||
return current->value;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
return -1; // 未找到
|
||||
}
|
||||
|
||||
// 删除操作
|
||||
void removeEntry(HashTable* hashTable, int key) {
|
||||
int index = hashFunction(key);
|
||||
Node* current = hashTable->buckets[index];
|
||||
Node* prev = NULL;
|
||||
|
||||
while (current != NULL) {
|
||||
if (current->key == key) {
|
||||
if (prev == NULL) {
|
||||
hashTable->buckets[index] = current->next;
|
||||
}
|
||||
else {
|
||||
prev->next = current->next;
|
||||
}
|
||||
free(current);
|
||||
return;
|
||||
}
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 打印哈希表
|
||||
void printHashTable(HashTable* hashTable) {
|
||||
for (int i = 0; i < TABLE_SIZE; i++) {
|
||||
printf("[%d]: ", i);
|
||||
Node* current = hashTable->buckets[i];
|
||||
while (current != NULL) {
|
||||
printf("(%d, %d) ", current->key, current->value);
|
||||
current = current->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// 测试
|
||||
void TestHash() {
|
||||
HashTable* hashTable = createHashTable();
|
||||
|
||||
// 插入操作
|
||||
insert(hashTable, 5, 10);
|
||||
insert(hashTable, 2, 20);
|
||||
insert(hashTable, 7, 30);
|
||||
insert(hashTable, 12, 40);
|
||||
insert(hashTable, 15, 50);
|
||||
|
||||
// 查找操作
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
PRIV_SHELL_CMD_FUNCTION(TestHash, a hash test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue