change function and class name.

This commit is contained in:
LeviXubbbb 2023-07-24 02:36:08 +08:00
parent bcfb881a6a
commit 36584b95ce
5 changed files with 100 additions and 97 deletions

View File

@ -30,40 +30,40 @@ void TestHash(void)
HashTable table; HashTable table;
printf("Init Hash Table\n"); printf("Init Hash Table\n");
Init(&table); HashTableInit(&table);
printf("Test Hash Table\n"); printf("Test Hash Table\n");
printf("Insert 1, 10\n"); printf("Insert 1, 10\n");
Insert(&table, 1, 10); HashTableInsert(&table, 1, 10);
printf("Insert 2, 20\n"); printf("Insert 2, 20\n");
Insert(&table, 2, 20); HashTableInsert(&table, 2, 20);
printf("Insert 3, 30\n"); printf("Insert 3, 30\n");
Insert(&table, 3, 30); HashTableInsert(&table, 3, 30);
printf("Insert 101, 40\n"); printf("Insert 101, 40\n");
Insert(&table, 101, 40); HashTableInsert(&table, 101, 40);
printf("Insert 102, 50\n"); printf("Insert 102, 50\n");
Insert(&table, 102, 50); HashTableInsert(&table, 102, 50);
printf("Insert 103, 60\n"); printf("Insert 103, 60\n");
Insert(&table, 103, 60); HashTableInsert(&table, 103, 60);
printf("Insert 104, 70\n"); printf("Insert 104, 70\n");
Insert(&table, 104, 70); HashTableInsert(&table, 104, 70);
printf("Search 1, result: %d\n", Search(&table, 1)); printf("Search 1, result: %d\n", HashTableSearch(&table, 1));
printf("Search 2, result: %d\n", Search(&table, 2)); printf("Search 2, result: %d\n", HashTableSearch(&table, 2));
printf("Search 3, result: %d\n", Search(&table, 3)); printf("Search 3, result: %d\n", HashTableSearch(&table, 3));
printf("Search 4, result: %d\n", Search(&table, 4)); printf("Search 4, result: %d\n", HashTableSearch(&table, 4));
printf("Search 101, result: %d\n", Search(&table, 101)); printf("Search 101, result: %d\n", HashTableSearch(&table, 101));
printf("Search 102, result: %d\n", Search(&table, 102)); printf("Search 102, result: %d\n", HashTableSearch(&table, 102));
printf("Search 103, result: %d\n", Search(&table, 103)); printf("Search 103, result: %d\n", HashTableSearch(&table, 103));
printf("Search 104, result: %d\n", Search(&table, 104)); printf("Search 104, result: %d\n", HashTableSearch(&table, 104));
printf("Delete 1\n"); printf("Delete 1\n");
Delete(&table, 1); HashTableDelete(&table, 1);
printf("Search 1, result: %d\n", Search(&table, 1)); printf("Search 1, result: %d\n", HashTableSearch(&table, 1));
printf("Clear Table\n"); printf("Clear Table\n");
Clear(&table); HashTableClear(&table);
return; return;
} }

View File

@ -25,44 +25,46 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define TABLE_SIZE 101 #define HASH_TABLE_SIZE 101
typedef struct Node{ typedef struct HashTableNode HashTableNode;
struct HashTableNode{
int key; int key;
int value; int value;
struct Node *next; HashTableNode *next;
} Node; };
typedef struct HashTable{ typedef struct HashTable{
Node *table[TABLE_SIZE]; HashTableNode *table[HASH_TABLE_SIZE];
} HashTable; } HashTable;
unsigned int Hash(int key) static unsigned int Hash(int key)
{ {
return key % TABLE_SIZE; return key % HASH_TABLE_SIZE;
} }
Node* CreateNode(int key, int value) static HashTableNode* CreateNode(int key, int value)
{ {
Node *new_node = (Node*)malloc(sizeof(Node)); HashTableNode* new_node = (HashTableNode*)malloc(sizeof(HashTableNode));
new_node->key = key; new_node->key = key;
new_node->value = value; new_node->value = value;
new_node->next = NULL; new_node->next = NULL;
return new_node; return new_node;
} }
void Init(HashTable *hash_table) void HashTableInit(HashTable *hash_table)
{ {
int i; int i;
for(i = 0; i < TABLE_SIZE; i++) for(i = 0; i < HASH_TABLE_SIZE; i++)
hash_table->table[i] = NULL; hash_table->table[i] = NULL;
} }
void Insert(HashTable *hash_table, int key, int value) void HashTableInsert(HashTable *hash_table, int key, int value)
{ {
unsigned int hash_value = Hash(key); unsigned int hash_value = Hash(key);
Node *node = hash_table->table[hash_value]; HashTableNode* node = hash_table->table[hash_value];
while(node != NULL && node->key != key) while(node != NULL && node->key != key)
node = node->next; node = node->next;
if(node != NULL){ if(node != NULL){
@ -70,15 +72,15 @@ void Insert(HashTable *hash_table, int key, int value)
return; return;
} }
Node *new_node = CreateNode(key, value); HashTableNode* new_node = CreateNode(key, value);
new_node->next = hash_table->table[hash_value]; new_node->next = hash_table->table[hash_value];
hash_table->table[hash_value] = new_node; hash_table->table[hash_value] = new_node;
} }
int Search(HashTable *hash_table, int key) int HashTableSearch(HashTable *hash_table, int key)
{ {
unsigned int hash_value = Hash(key); unsigned int hash_value = Hash(key);
Node *node = hash_table->table[hash_value]; HashTableNode* node = hash_table->table[hash_value];
while(node != NULL && node->key != key) while(node != NULL && node->key != key)
node = node->next; node = node->next;
if(node == NULL) if(node == NULL)
@ -87,11 +89,11 @@ int Search(HashTable *hash_table, int key)
return node->value; return node->value;
} }
void Delete(HashTable *hash_table, int key) void HashTableDelete(HashTable *hash_table, int key)
{ {
unsigned int hash_value = Hash(key); unsigned int hash_value = Hash(key);
Node *node = hash_table->table[hash_value]; HashTableNode* node = hash_table->table[hash_value];
Node *prev_node = NULL; HashTableNode* prev_node = NULL;
while(node != NULL && node->key != key){ while(node != NULL && node->key != key){
prev_node = node; prev_node = node;
node = node->next; node = node->next;
@ -105,13 +107,13 @@ void Delete(HashTable *hash_table, int key)
free(node); free(node);
} }
void Clear(HashTable *hash_table) void HashTableClear(HashTable *hash_table)
{ {
int i; int i;
for(i = 0; i < TABLE_SIZE; i++){ for(i = 0; i < HASH_TABLE_SIZE; i++){
Node *node = hash_table->table[i]; HashTableNode* node = hash_table->table[i];
while(node != NULL){ while(node != NULL){
Node *temp = node; HashTableNode* temp = node;
node = node->next; node = node->next;
free(temp); free(temp);
} }
@ -119,12 +121,12 @@ void Clear(HashTable *hash_table)
} }
} }
void PrintTable(HashTable *hash_table) void HashTablePrint(HashTable *hash_table)
{ {
int i; int i;
for(i = 0; i < TABLE_SIZE; i++){ for(i = 0; i < HASH_TABLE_SIZE; i++){
printf("Print %d\n", i); printf("Print %d\n", i);
Node *node = hash_table->table[i]; HashTableNode* node = hash_table->table[i];
while(node != NULL){ while(node != NULL){
printf("key: %d, value: %d\n", node->key, node->value); printf("key: %d, value: %d\n", node->key, node->value);
node = node->next; node = node->next;

View File

@ -27,7 +27,7 @@
void TestRBTree(void) void TestRBTree(void)
{ {
struct Node *root = NULL; RBTreeNode* root = NULL;
printf("创建红黑树并且插入以下结点\n"); printf("创建红黑树并且插入以下结点\n");
printf("key: 7, value: 7\n"); printf("key: 7, value: 7\n");
printf("key: 3, value: 3\n"); printf("key: 3, value: 3\n");
@ -49,21 +49,21 @@ void TestRBTree(void)
int values[] = {7, 3, 18, 10, 22, 8, 11, 26, 2, 6, 13, 20, 27, 1, 12, 14, 25}; int values[] = {7, 3, 18, 10, 22, 8, 11, 26, 2, 6, 13, 20, 27, 1, 12, 14, 25};
for (int i = 0; i < sizeof(values) / sizeof(values[0]); i++) for (int i = 0; i < sizeof(values) / sizeof(values[0]); i++)
insertNode(&root, values[i], values[i]); RBTreeInsert(&root, values[i], values[i]);
printf("插入创建完毕后中序遍历红黑树结果:\n"); printf("插入创建完毕后中序遍历红黑树结果:\n");
inorderTraversal(root); RBTreeInorderTraversal(root);
printf("\n"); printf("\n");
printf("查找key值为 10 的结点\n"); printf("查找key值为 10 的结点\n");
struct Node *searchResult = searchNode(root, 10); RBTreeNode* searchResult = RBTreeSearch(root, 10);
if (searchResult != NULL) if (searchResult != NULL)
printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value); printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value);
else else
printf("未找到key值为 %d 的结点\n", 10); printf("未找到key值为 %d 的结点\n", 10);
printf("查找key值为 100 的结点\n"); printf("查找key值为 100 的结点\n");
searchResult = searchNode(root, 100); searchResult = RBTreeSearch(root, 100);
if (searchResult != NULL) if (searchResult != NULL)
printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value); printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value);
else else
@ -71,18 +71,18 @@ void TestRBTree(void)
printf("删除结点 10 和 22\n"); printf("删除结点 10 和 22\n");
int deleteData = 10; int deleteData = 10;
deleteNode(&root, 10); RBTreeDelete(&root, 10);
deleteNode(&root, 22); RBTreeDelete(&root, 22);
printf("查找key值为 10 的结点\n"); printf("查找key值为 10 的结点\n");
searchResult = searchNode(root, 10); searchResult = RBTreeSearch(root, 10);
if (searchResult != NULL) if (searchResult != NULL)
printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value); printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value);
else else
printf("未找到key值为 %d 的结点\n", 10); printf("未找到key值为 %d 的结点\n", 10);
printf("查找key值为 22 的结点\n"); printf("查找key值为 22 的结点\n");
searchResult = searchNode(root, 22); searchResult = RBTreeSearch(root, 22);
if (searchResult != NULL) if (searchResult != NULL)
printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value); printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value);
else else
@ -90,10 +90,10 @@ void TestRBTree(void)
printf("删除结点 %d 后的中序遍历结果:\n", deleteData); printf("删除结点 %d 后的中序遍历结果:\n", deleteData);
inorderTraversal(root); RBTreeInorderTraversal(root);
printf("\n"); printf("\n");
printf("清空红黑树并且释放内存\n"); printf("清空红黑树并且释放内存\n");
clear(root); RBTreeClear(root);
} }
PRIV_SHELL_CMD_FUNCTION(TestRBTree, a red black tree test sample, PRIV_SHELL_CMD_MAIN_ATTR); PRIV_SHELL_CMD_FUNCTION(TestRBTree, a red black tree test sample, PRIV_SHELL_CMD_MAIN_ATTR);

View File

@ -26,19 +26,20 @@
#include <string.h> #include <string.h>
// define color of red-black tree // define color of red-black tree
enum Color {RED, BLACK}; enum RBTreeColor {RED, BLACK};
typedef struct RBTreeNode RBTreeNode;
// define a node of red-black tree // define a node of red-black tree
struct Node { struct RBTreeNode{
int key; int key;
int value; int value;
enum Color color; enum RBTreeColor color;
struct Node *left, *right, *parent; RBTreeNode *left, *right, *parent;
}; };
// create a new nodes // create a new nodes
struct Node *createNode(int key, int value) { static RBTreeNode *createNode(int key, int value) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node)); RBTreeNode *newNode = (RBTreeNode*)malloc(sizeof(RBTreeNode));
newNode->key = key; newNode->key = key;
newNode->value = value; newNode->value = value;
newNode->color = RED; newNode->color = RED;
@ -47,8 +48,8 @@ struct Node *createNode(int key, int value) {
} }
// red-black tree left rotation // red-black tree left rotation
void leftRotate(struct Node **root, struct Node *x) { static void leftRotate(RBTreeNode **root, RBTreeNode *x) {
struct Node *y = x->right; RBTreeNode *y = x->right;
x->right = y->left; x->right = y->left;
if (y->left != NULL) if (y->left != NULL)
y->left->parent = x; y->left->parent = x;
@ -64,8 +65,8 @@ void leftRotate(struct Node **root, struct Node *x) {
} }
// red-black tree right rotation // red-black tree right rotation
void rightRotate(struct Node **root, struct Node *y) { static void rightRotate(RBTreeNode **root, RBTreeNode *y) {
struct Node *x = y->left; RBTreeNode *x = y->left;
y->left = x->right; y->left = x->right;
if (x->right != NULL) if (x->right != NULL)
x->right->parent = y; x->right->parent = y;
@ -81,16 +82,16 @@ void rightRotate(struct Node **root, struct Node *y) {
} }
// red-black tree fix-up // red-black tree fix-up
void fixViolation(struct Node **root, struct Node *newNode) { static void fixViolation(RBTreeNode **root, RBTreeNode *newNode) {
struct Node *parent = NULL; RBTreeNode *parent = NULL;
struct Node *grandparent = NULL; RBTreeNode *grandparent = NULL;
while ((newNode != *root) && (newNode->color != BLACK) && (newNode->parent->color == RED)) { while ((newNode != *root) && (newNode->color != BLACK) && (newNode->parent->color == RED)) {
parent = newNode->parent; parent = newNode->parent;
grandparent = newNode->parent->parent; grandparent = newNode->parent->parent;
if (parent == grandparent->left) { if (parent == grandparent->left) {
struct Node *uncle = grandparent->right; RBTreeNode *uncle = grandparent->right;
if (uncle != NULL && uncle->color == RED) { if (uncle != NULL && uncle->color == RED) {
grandparent->color = RED; grandparent->color = RED;
@ -105,13 +106,13 @@ void fixViolation(struct Node **root, struct Node *newNode) {
} }
rightRotate(root, grandparent); rightRotate(root, grandparent);
int temp = parent->color; enum RBTreeColor temp = parent->color;
parent->color = grandparent->color; parent->color = grandparent->color;
grandparent->color = temp; grandparent->color = temp;
newNode = parent; newNode = parent;
} }
} else { } else {
struct Node *uncle = grandparent->left; RBTreeNode *uncle = grandparent->left;
if ((uncle != NULL) && (uncle->color == RED)) { if ((uncle != NULL) && (uncle->color == RED)) {
grandparent->color = RED; grandparent->color = RED;
@ -126,7 +127,7 @@ void fixViolation(struct Node **root, struct Node *newNode) {
} }
leftRotate(root, grandparent); leftRotate(root, grandparent);
int temp = parent->color; enum RBTreeColor temp = parent->color;
parent->color = grandparent->color; parent->color = grandparent->color;
grandparent->color = temp; grandparent->color = temp;
newNode = parent; newNode = parent;
@ -138,10 +139,10 @@ void fixViolation(struct Node **root, struct Node *newNode) {
} }
// insert a node into red-black tree // insert a node into red-black tree
void insertNode(struct Node **root, int key, int value) { void RBTreeInsert(RBTreeNode **root, int key, int value) {
struct Node *newNode = createNode(key, value); RBTreeNode* newNode = createNode(key, value);
struct Node *current = *root; RBTreeNode* current = *root;
struct Node *parent = NULL; RBTreeNode* parent = NULL;
while (current != NULL) { while (current != NULL) {
parent = current; parent = current;
@ -163,18 +164,18 @@ void insertNode(struct Node **root, int key, int value) {
} }
// search a node in red-black tree // search a node in red-black tree
struct Node *searchNode(struct Node *root, int key) { RBTreeNode* RBTreeSearch(RBTreeNode* root, int key) {
if (root == NULL || root->key == key) if (root == NULL || root->key == key)
return root; return root;
if (root->key < key) if (root->key < key)
return searchNode(root->right, key); return RBTreeSearch(root->right, key);
return searchNode(root->left, key); return RBTreeSearch(root->left, key);
} }
// find the minimum node in red-black tree // find the minimum node in red-black tree
struct Node *minimumNode(struct Node *node) { static RBTreeNode* minimumNode(RBTreeNode* node) {
while (node->left != NULL) while (node->left != NULL)
node = node->left; node = node->left;
@ -182,7 +183,7 @@ struct Node *minimumNode(struct Node *node) {
} }
// replace a node with another node // replace a node with another node
void transplant(struct Node **root, struct Node *u, struct Node *v) { static void transplant(RBTreeNode** root, RBTreeNode* u, RBTreeNode* v) {
if (u->parent == NULL) if (u->parent == NULL)
*root = v; *root = v;
else if (u == u->parent->left) else if (u == u->parent->left)
@ -195,8 +196,8 @@ void transplant(struct Node **root, struct Node *u, struct Node *v) {
} }
// fix the delete violation in red-black tree // fix the delete violation in red-black tree
void fixDeleteViolation(struct Node **root, struct Node *node, struct Node *parent) { static void fixDeleteViolation(RBTreeNode** root, RBTreeNode* node, RBTreeNode* parent) {
struct Node *sibling = NULL; RBTreeNode* sibling = NULL;
while ((node != *root) && ((node == NULL) || (node->color == BLACK))) { while ((node != *root) && ((node == NULL) || (node->color == BLACK))) {
if (node == parent->left) { if (node == parent->left) {
@ -268,14 +269,14 @@ void fixDeleteViolation(struct Node **root, struct Node *node, struct Node *pare
// delete a node from red-black tree // delete a node from red-black tree
void deleteNode(struct Node **root, int key) { void RBTreeDelete(RBTreeNode** root, int key) {
struct Node *nodeToDelete = searchNode(*root, key); RBTreeNode* nodeToDelete = RBTreeSearch(*root, key);
if (nodeToDelete == NULL) if (nodeToDelete == NULL)
return; return;
struct Node *child = NULL; RBTreeNode* child = NULL;
struct Node *parent = NULL; RBTreeNode* parent = NULL;
enum Color originalColor = nodeToDelete->color; enum RBTreeColor originalColor = nodeToDelete->color;
if (nodeToDelete->left == NULL) { if (nodeToDelete->left == NULL) {
child = nodeToDelete->right; child = nodeToDelete->right;
@ -286,7 +287,7 @@ void deleteNode(struct Node **root, int key) {
parent = nodeToDelete->parent; parent = nodeToDelete->parent;
transplant(root, nodeToDelete, nodeToDelete->left); transplant(root, nodeToDelete, nodeToDelete->left);
} else { } else {
struct Node *successor = minimumNode(nodeToDelete->right); RBTreeNode* successor = minimumNode(nodeToDelete->right);
originalColor = successor->color; originalColor = successor->color;
child = successor->right; child = successor->right;
parent = successor->parent; parent = successor->parent;
@ -311,19 +312,19 @@ void deleteNode(struct Node **root, int key) {
} }
// inorder traversal // inorder traversal
void inorderTraversal(struct Node *root) { void RBTreeInorderTraversal(RBTreeNode* root) {
if (root != NULL) { if (root != NULL) {
inorderTraversal(root->left); RBTreeInorderTraversal(root->left);
printf("%d ", root->key); printf("%d ", root->key);
inorderTraversal(root->right); RBTreeInorderTraversal(root->right);
} }
} }
//clear the tree //clear the tree
void clear(struct Node *root) { void RBTreeClear(RBTreeNode* root) {
if (root != NULL) { if (root != NULL) {
clear(root->left); RBTreeClear(root->left);
clear(root->right); RBTreeClear(root->right);
free(root); free(root);
} }
} }

View File

@ -7,7 +7,7 @@ export APPLFLAGS := -mcmodel=medany -march=rv64imac -mabi=lp64 -nostartfiles -Wl
export CXXFLAGS := -mcmodel=medany -march=rv64imac -mabi=lp64 -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -O0 -Wa,-g -ggdb -Werror export CXXFLAGS := -mcmodel=medany -march=rv64imac -mabi=lp64 -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -O0 -Wa,-g -ggdb -Werror
export CROSS_COMPILE ?=/opt/xpack-riscv-none-embed-gcc-8.3.0-1.2/bin/riscv-none-embed- export CROSS_COMPILE ?=/opt/gnu-mcu-eclipse/riscv-none-gcc/8.2.0-2.1-20190425-1021/bin/riscv-none-embed-
export DEFINES := -DHAVE_CCONFIG_H -DHAVE_SIGINFO export DEFINES := -DHAVE_CCONFIG_H -DHAVE_SIGINFO