Skip to content

Instantly share code, notes, and snippets.

@jo0707
Created November 7, 2024 17:25
Show Gist options
  • Save jo0707/d6a92817e0e2af2fe807c5e46d2607da to your computer and use it in GitHub Desktop.
Save jo0707/d6a92817e0e2af2fe807c5e46d2607da to your computer and use it in GitHub Desktop.
Berikut adalah contoh metode Hashing Modulus dan Collision Handling berupa Pengalamatan Rantai.
#include <iostream>
#include <cstdlib>
using namespace std;
#define Nil NULL
#define MaxEl 10
typedef int infotype;
typedef struct tNode *addrNode;
// Struktur node dari linked list
typedef struct tNode {
infotype info;
addrNode next;
} Node;
// Struktur hash table
typedef struct tHash {
addrNode first;
} Hash;
#define Info(P) (P)->info
#define Next(P) (P)->next
#define First(H, i) (H)[i].first
// Membuat tabel hash kosong dengan inisialisasi elemen first bernilai Nil
void CreateEmptyHash(Hash hash_table[MaxEl]) {
for (int i = 0; i < MaxEl; i++) {
First(hash_table, i) = Nil;
}
}
// Mengalokasikan memori untuk node baru
addrNode NodeAllocation(infotype X) {
addrNode NewNode = (Node*) malloc(sizeof(Node));
if (NewNode != Nil) {
Info(NewNode) = X;
Next(NewNode) = Nil;
}
return NewNode;
}
// Menghapus node dari memori
void NodeDeallocation(addrNode hapus) {
free(hapus);
}
// Mengecek apakah node pertama kosong
bool IsEmptyFirst(addrNode First) {
return (First == Nil);
}
// Menyisipkan elemen di awal linked list
void InsertFirst(addrNode *First, infotype X) {
addrNode NewNode = NodeAllocation(X);
if (NewNode != Nil) {
Next(NewNode) = *First;
*First = NewNode;
}
}
// Menyisipkan elemen di akhir linked list
void InsertLast(addrNode *First, infotype X) {
addrNode NewNode = NodeAllocation(X);
if (NewNode != Nil) {
if (IsEmptyFirst(*First)) {
*First = NewNode;
} else {
addrNode temp = *First;
while (Next(temp) != Nil) {
temp = Next(temp);
}
Next(temp) = NewNode;
}
}
}
// Menghapus elemen pertama dari linked list
void DeleteFirst(addrNode *First) {
if (!IsEmptyFirst(*First)) {
addrNode temp = *First;
*First = Next(*First);
NodeDeallocation(temp);
}
}
// Menghapus elemen setelah node tertentu dalam linked list
void DeleteAfter(addrNode pred) {
if (pred != Nil && Next(pred) != Nil) {
addrNode temp = Next(pred);
Next(pred) = Next(temp);
NodeDeallocation(temp);
}
}
// Menyisipkan nilai ke dalam hash table menggunakan metode chaining
void InsertValue(Hash hash_table[MaxEl], infotype X) {
int index = X % MaxEl;
addrNode *FirstNode = &(First(hash_table, index));
if (IsEmptyFirst(*FirstNode)) {
InsertFirst(FirstNode, X);
} else {
InsertLast(FirstNode, X);
}
}
// Menghapus nilai dari hash table
void DeleteValue(Hash hash_table[MaxEl], infotype X) {
int index = X % MaxEl;
addrNode *FirstNode = &(First(hash_table, index));
if (IsEmptyFirst(*FirstNode)) {
cout << "Alamat menunjuk Nil.";
} else {
if (Info(*FirstNode) == X) {
DeleteFirst(FirstNode);
} else {
addrNode temp = *FirstNode;
while (Next(temp) != Nil && Info(Next(temp)) != X) {
temp = Next(temp);
}
if (Next(temp) == Nil) {
cout << "Pada index ke-" << index << " tidak ditemukan data " << X << ".";
} else {
DeleteAfter(temp);
}
}
}
}
// Menampilkan seluruh isi dari hash table
void DisplayHashTable(Hash hash_table[MaxEl]) {
for (int i = 0; i < MaxEl; i++) {
cout << "Index " << i << ": ";
addrNode temp = First(hash_table, i);
if (IsEmptyFirst(temp)) {
cout << "Kosong";
} else {
while (temp != Nil) {
cout << Info(temp) << " -> ";
temp = Next(temp);
}
cout << "Null";
}
cout << endl;
}
}
// Fungsi utama untuk menguji operasi pada tabel hash
int main() {
Hash hash_table[MaxEl];
CreateEmptyHash(hash_table);
// Insert beberapa nilai
InsertValue(hash_table, 15);
InsertValue(hash_table, 22);
InsertValue(hash_table, 30);
InsertValue(hash_table, 33);
InsertValue(hash_table, 41);
InsertValue(hash_table, 4);
InsertValue(hash_table, 25);
InsertValue(hash_table, 35);
InsertValue(hash_table, 99);
// Tampilkan hash table
cout << "Isi Hash Table:" << endl;
DisplayHashTable(hash_table);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment