# เทคนิคการจัดการข้อมูลแบบไดนามิคด้วย Separate Chaining Hashing ในภาษา C
การจัดการข้อมูลเป็นสิ่งสำคัญในการพัฒนาโปรแกรม หนึ่งในเทคนิคที่ช่วยในการจัดการข้อมูลอย่างมีประสิทธิภาพคือการใช้ Hash Tables และวิธีการที่เรียกว่า Separate Chaining สำหรับการจัดการการชนของข้อมูลที่มี Hash Key เดียวกัน ในบทความนี้ เราจะมาพิจารณาเทคนิคดังกล่าวผ่านภาษา C พร้อมกับการยกตัวอย่างโค้ดเพื่อให้เข้าใจได้ง่ายขึ้น
Separate Chaining Hashing เป็นวิธีหนึ่งในการแก้ปัญหาการชน (Collision) ของข้อมูลใน Hash Table ซึ่งเป็นโครงสร้างข้อมูลที่ใช้ฟังก์ชันแฮชในการคำนวณดัชนีที่ข้อมูลจะถูกเก็บ โดย Separate Chaining จะจัดการการชนด้วยการให้แต่ละดัชนีสามารถเก็บข้อมูลได้มากกว่าหนึ่งค่า โดยปกติจะใช้ Linked List ในการเก็บข้อมูลที่ชนกันหรือถูกแฮชไปยังดัชนีเดียวกัน
#include
#include
// โครงสร้างข้อมูลสำหรับ Linked List
typedef struct node {
int key;
int data;
struct node* next;
} Node;
// โครงสร้างข้อมูลสำหรับ Hash Table
typedef struct hashtable {
int size;
struct node **list;
} HashTable;
// สร้าง Node ใหม่
Node *createNode(int key, int data) {
Node *newNode = (Node *) malloc(sizeof(Node));
newNode->key = key;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// ฟังก์ชันแฮช
int hashCode(HashTable *ht, int key) {
return key % ht->size;
}
// สร้าง Hash Table
HashTable *createTable(int size) {
HashTable *table = (HashTable *) malloc(sizeof(HashTable));
table->size = size;
table->list = (Node **) malloc(size * sizeof(Node *));
for (int i = 0; i < size; i++)
table->list[i] = NULL;
return table;
}
// เพิ่มข้อมูลลงใน Hash Table (Insert)
void insert(HashTable *ht, int key, int data) {
Node *newNode = createNode(key, data);
int hashIndex = hashCode(ht, key);
if (ht->list[hashIndex] == NULL) {
// หากไม่มีการชน ให้ใส่ข้อมูลได้เลย
ht->list[hashIndex] = newNode;
} else {
// มีการชน ให้นำข้อมูลไปไว้ที่หน้า Linked List
newNode->next = ht->list[hashIndex];
ht->list[hashIndex] = newNode;
}
}
// ค้นหาข้อมูล (Find)
Node* find(HashTable *ht, int key) {
int hashIndex = hashCode(ht, key);
Node *current = ht->list[hashIndex];
while (current != NULL) {
if (current->key == key)
return current;
current = current->next;
}
return NULL;
}
// ลบข้อมูล (Delete)
void delete(HashTable *ht, int key) {
int hashIndex = hashCode(ht, key);
Node *current = ht->list[hashIndex];
Node *prev = NULL;
if (current == NULL || current->key != key) {
printf("Key not found.\n");
return;
}
// ลบ Node ที่ hashIndex
while (current->key != key) {
prev = current;
current = current->next;
}
if (prev == NULL) {
// ลบ Node แรก
ht->list[hashIndex] = current->next;
} else {
// ลบ Node ในลำดับถัดไป
prev->next = current->next;
}
free(current);
}
// ฟังก์ชันสำหรับแสดงผล
void display(HashTable *ht) {
for (int i = 0; i < ht->size; i++) {
Node *temp = ht->list[i];
printf("Table[%d]: ", i);
while (temp) {
printf("(%d,%d) ", temp->key, temp->data);
temp = temp->next;
}
printf("\n");
}
}
// ตัวอย่างการใช้งาน
int main() {
HashTable *ht = createTable(10);
insert(ht, 10, 100);
insert(ht, 20, 200);
insert(ht, 30, 300);
insert(ht, 40, 400);
insert(ht, 20, 500); // จะเกิดการชน และข้อมูลจะถูกใส่ไว้ด้านหน้า
display(ht);
Node *found = find(ht, 20); // ควรจะหาเจอ
if (found) {
printf("Element found: %d\n", found->data);
} else {
printf("Element not found\n");
}
delete(ht, 20); // ลบข้อมูลที่มี key 20
display(ht);
return 0;
}
เมื่อวิเคราะห์โค้ดที่ยกมาข้างต้น เราจะเห็นภาพการทำงานของ Separate Chaining ผ่านโครงสร้างข้อมูล Hash Table ที่แสดงออกมาในภาษา C และสามารถเห็นได้ว่าการจัดการข้อมูลที่ยืดหยุ่นเป็นอย่างไร
การได้เรียนรู้และทำความเข้าใจเทคนิคการเขียนโค้ดอย่างนี้ไม่เพียงแต่ช่วยเพิ่มความรู้ในการเขียนโปรแกรม แต่ยังเปิดโอกาสสู่การพัฒนาซอฟต์แวร์ที่มีคุณภาพ และที่ EPT เรามีหลักสูตรที่จะนำท่านไปพบกับการเรียนรู้ที่ลึกซึ้งและมีประสิทธิภาพในการจัดการข้อมูล หากสนใจที่จะเพิ่มพูนทักษะการเขียนโค้ดระดับมืออาชีพ แวะเข้ามาร่วมเรียนรู้ที่ EPT กันได้ แล้วคุณจะพบกับโลกแห่งการเขียนโค้ดที่ไม่รู้จบ!
หมายเหตุ: ข้อมูลในบทความนี้อาจจะผิด โปรดตรวจสอบความถูกต้องของบทความอีกครั้งหนึ่ง บทความนี้ไม่สามารถนำไปใช้อ้างอิงใด ๆ ได้ ทาง EPT ไม่ขอยืนยันความถูกต้อง และไม่ขอรับผิดชอบต่อความเสียหายใดที่เกิดจากบทความชุดนี้ทั้งทางทรัพย์สิน ร่างกาย หรือจิตใจของผู้อ่านและผู้เกี่ยวข้อง
หากมีข้อผิดพลาด/ต้องการพูดคุยเพิ่มเติมเกี่ยวกับบทความนี้ กรุณาแจ้งที่ http://m.me/Expert.Programming.Tutor
085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM