Skip to content

Commit b28d37d

Browse files
committed
Add basic hash table implementation for person structs
Introduces a hash table in C for storing and managing person_t structs, including creation, insertion, lookup, deletion, and printing functions. Adds cperson.c/h for person struct management and hash_table.c/h for hash table logic using open addressing.
1 parent 1ada915 commit b28d37d

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed

HashTable/cperson.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @file cperson.c
3+
* @author Xuhua Huang
4+
* @brief Implementation of functions for the CPerson struct.
5+
* @version 0.1
6+
* @date 2025-06-25
7+
*
8+
* @copyright Copyright (c) 2025
9+
*
10+
*/
11+
12+
#include "cperson.h"
13+
#include <stdlib.h>
14+
#include <string.h>
15+
16+
person_t* DELETED_NODE = (person_t*)(0xFFFFFFFF); // Unsafe, but illustrative
17+
18+
person_t* create_person(const char* name, int age) {
19+
person_t* new_person = (person_t*)malloc(sizeof(person_t));
20+
if (new_person) {
21+
strncpy(new_person->name, name, sizeof(new_person->name) - 1);
22+
new_person->name[sizeof(new_person->name) - 1] = '\0';
23+
new_person->age = age;
24+
}
25+
return new_person;
26+
}
27+
28+
void delete_person(person_t* person) {
29+
if (person) {
30+
free(person);
31+
}
32+
}

HashTable/cperson.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @file cperson.h
3+
* @author Xuhua Huang
4+
* @brief Define a C-style struct for a person.
5+
* @version 0.1
6+
* @date 2025-06-25
7+
*
8+
* @copyright Copyright (c) 2025
9+
*
10+
*/
11+
12+
#ifndef CPERSON_H
13+
#define CPERSON_H
14+
15+
typedef struct CPerson {
16+
char name[100];
17+
int age;
18+
} person_t;
19+
20+
extern person_t* DELETED_NODE; // Pointer to a deleted node, used in hash table operations
21+
22+
#endif // !CPERSON_H

HashTable/hash_table.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @file hash_table.c
3+
* @author Xuhua Huang
4+
* @brief
5+
* @version 0.1
6+
* @date 2025-06-25
7+
*
8+
* @copyright Copyright (c) 2025
9+
*
10+
*/
11+
12+
#include "hash_table.h"
13+
14+
#include <stdio.h>
15+
#include <string.h>
16+
17+
static person_t* hash_table[HASH_TABLE_SIZE];
18+
19+
unsigned int hash(const char* const name) {
20+
int length = strnlen(name, MAX_NAME_LENGTH);
21+
unsigned int hash_value = 0;
22+
for (int i = 0; i < length; ++i) {
23+
hash_value += name[i];
24+
hash_value = (hash_value * name[i]) % HASH_TABLE_SIZE;
25+
}
26+
return hash_value;
27+
}
28+
29+
void init_hash_table() {
30+
for (int i = 0; i < HASH_TABLE_SIZE; ++i) {
31+
hash_table[i] = NULL;
32+
}
33+
}
34+
35+
bool insert_to_table(const person_t* const ptr) {
36+
if (!ptr) return false;
37+
int index = hash(ptr->name);
38+
for (int i = 0; i < HASH_TABLE_SIZE; ++i) {
39+
int try_index = (i + index) % HASH_TABLE_SIZE;
40+
if (hash_table[try_index] == NULL) {
41+
hash_table[try_index] = (person_t*)ptr;
42+
return true;
43+
}
44+
}
45+
return false;
46+
}
47+
48+
person_t* hash_table_lookup(const char* const name) {
49+
int index = hash(name);
50+
for (int i = 0; i < HASH_TABLE_SIZE; ++i) {
51+
int try_index = (i + index) % HASH_TABLE_SIZE;
52+
if (hash_table[try_index] == NULL) return NULL;
53+
if (hash_table[try_index] == DELETED_NODE) continue;
54+
if (strncmp(hash_table[try_index]->name, name, MAX_NAME_LENGTH) == 0) {
55+
return hash_table[try_index];
56+
}
57+
}
58+
return NULL;
59+
}
60+
61+
person_t* del_from_table(const char* const name) {
62+
int index = hash(name);
63+
for (int i = 0; i < HASH_TABLE_SIZE; ++i) {
64+
int try_index = (i + index) % HASH_TABLE_SIZE;
65+
if (hash_table[try_index] == NULL) return NULL;
66+
if (hash_table[try_index] == DELETED_NODE) continue;
67+
if (strncmp(hash_table[try_index]->name, name, MAX_NAME_LENGTH) == 0) {
68+
person_t* temp = hash_table[try_index];
69+
hash_table[try_index] = DELETED_NODE;
70+
return temp;
71+
}
72+
}
73+
return NULL;
74+
}
75+
76+
void print_table() {
77+
printf("\nStart %s\n", __FUNCTION__);
78+
for (int i = 0; i < HASH_TABLE_SIZE; ++i) {
79+
if (hash_table[i] == NULL) {
80+
printf("\t%i\t---\n", i);
81+
} else if (hash_table[i] == DELETED_NODE) {
82+
printf("\t%i\t---<deleted>\n", i);
83+
} else {
84+
printf("\t%i\t%s\n", i, hash_table[i]->name);
85+
}
86+
}
87+
printf("End %s\n", __FUNCTION__);
88+
}

HashTable/hash_table.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @file hash_table.h
3+
* @author Xuhua Huang
4+
* @brief Practice of hash table in C++23 after lecture of Data Structures and Algorithms.
5+
* Constant operation time of O(1) with Open Addressing and External Chamber.
6+
*
7+
* @version 0.1
8+
* @date 2025-06-25
9+
*
10+
* @copyright Copyright (c) 2025
11+
*
12+
*/
13+
14+
#ifndef HASH_TABLE_H
15+
#define HASH_TABLE_H
16+
17+
#include "cperson.h"
18+
#include <stdbool.h>
19+
20+
#define HASH_TABLE_SIZE 10
21+
#define MAX_NAME_LENGTH 100
22+
23+
typedef struct HashTableEntry {
24+
person_t person;
25+
bool isOccupied; // Flag to check if the entry is occupied
26+
} hash_table_entry_t;
27+
28+
unsigned int hash(const char* const name);
29+
void init_hash_table();
30+
bool insert_to_table(const person_t* const ptr);
31+
person_t* hash_table_lookup(const char* const name);
32+
person_t* del_from_table(const char* const name);
33+
void print_table();
34+
35+
#endif // !HASH_TABLE_H

0 commit comments

Comments
 (0)