Docsity

Hashing Implementation in C: Linear, Quadratic, and Double Probing, Study Guides, Projects, Research of Data Structures and Algorithms

Toggle sidebar
bg1
Practical No. 3 C Code Data Structures AIDS CSE(AI) SE 2024-2025 Pattern
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_NAME_LENGTH 50
typedef struct {
long long key;
char *name;
} Client;
Client *linearTable, *quadraticTable, *doubleHashTable;
int table_size;
int hash2(long long key) {
return 1 + (key % (table_size - 1));
}
int double_hash(long long key, int i) {
return (key % table_size + i * hash2(key)) % table_size;
}
void init_tables() {
linearTable = malloc(table_size * sizeof(Client));
quadraticTable = malloc(table_size * sizeof(Client));
doubleHashTable = malloc(table_size * sizeof(Client));
for (int i = 0; i < table_size; i++) {
linearTable[i].key = quadraticTable[i].key = doubleHashTable[i].key = -1;
linearTable[i].name = quadraticTable[i].name = doubleHashTable[i].name =
NULL;
}
}
// Check for duplicate key in all tables
int is_duplicate(long long key) {
int pos;
// Linear probing check
pos = key % table_size;
for (int i = 0; i < table_size; i++) {
int index = (pos + i) % table_size;
if (linearTable[index].key == key) return 1;
if (linearTable[index].key == -1) break;
}
// Quadratic probing check
pos = key % table_size;
for (int i = 0; i < table_size; i++) {
int index = (pos + i * i) % table_size;
if (quadraticTable[index].key == key) return 1;
if (quadraticTable[index].key == -1) break;
}
// Double hashing check
for (int i = 0; i < table_size; i++) {
int index = double_hash(key, i);
if (doubleHashTable[index].key == key) return 1;
if (doubleHashTable[index].key == -1) break;
}
return 0;
}
void insert_linear(long long key, char *name) {
if (is_duplicate(key)) {
pf3
pf4
pf5

Partial preview of the text

Download Hashing Implementation in C: Linear, Quadratic, and Double Probing and more Study Guides, Projects, Research Data Structures and Algorithms in PDF only on Docsity!

Practical No. 3 C Code Data Structures AIDS CSE(AI) SE 2024-2025 Pattern #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_NAME_LENGTH 50 typedef struct { long long key; char *name; } Client; Client *linearTable, *quadraticTable, *doubleHashTable; int table_size; int hash2(long long key) { return 1 + (key % (table_size - 1)); } int double_hash(long long key, int i) { return (key % table_size + i * hash2(key)) % table_size; } void init_tables() { linearTable = malloc(table_size * sizeof(Client)); quadraticTable = malloc(table_size * sizeof(Client)); doubleHashTable = malloc(table_size * sizeof(Client)); for (int i = 0; i < table_size; i++) { linearTable[i].key = quadraticTable[i].key = doubleHashTable[i].key = -1; linearTable[i].name = quadraticTable[i].name = doubleHashTable[i].name = NULL; } } // Check for duplicate key in all tables int is_duplicate(long long key) { int pos; // Linear probing check pos = key % table_size; for (int i = 0; i < table_size; i++) { int index = (pos + i) % table_size; if (linearTable[index].key == key) return 1; if (linearTable[index].key == -1) break; } // Quadratic probing check pos = key % table_size; for (int i = 0; i < table_size; i++) { int index = (pos + i * i) % table_size; if (quadraticTable[index].key == key) return 1; if (quadraticTable[index].key == -1) break; } // Double hashing check for (int i = 0; i < table_size; i++) { int index = double_hash(key, i); if (doubleHashTable[index].key == key) return 1; if (doubleHashTable[index].key == -1) break; } return 0; } void insert_linear(long long key, char *name) { if (is_duplicate(key)) {

printf("Duplicate key %lld! Not inserting into Linear Probing.\n", key); return; } int index = key % table_size; for (int i = 0; i < table_size; i++) { int pos = (index + i) % table_size; if (linearTable[pos].key == -1) { linearTable[pos].key = key; linearTable[pos].name = strdup(name); return; } } printf("Linear Probing Table Full!! Cannot insert key %lld\n", key); } void insert_quadratic(long long key, char *name) { if (is_duplicate(key)) { printf("Duplicate key %lld! Not inserting into Quadratic Probing.\n", key); return; } int index = key % table_size; for (int i = 0; i < table_size; i++) { int pos = (index + i * i) % table_size; if (quadraticTable[pos].key == -1) { quadraticTable[pos].key = key; quadraticTable[pos].name = strdup(name); return; } } printf("Quadratic Probing Table Full!! Cannot insert key %lld\n", key); } void insert_double_hash(long long key, char *name) { if (is_duplicate(key)) { printf("Duplicate key %lld! Not inserting into Double Hashing.\n", key); return; } for (int i = 0; i < table_size; i++) { int pos = double_hash(key, i); if (doubleHashTable[pos].key == -1) { doubleHashTable[pos].key = key; doubleHashTable[pos].name = strdup(name); return; } } printf("Double Hashing Table Full!! Cannot insert key %lld\n", key); } // Search and display functions int search_linear(long long key) { int index = key % table_size; for (int i = 0; i < table_size; i++) { int pos = (index + i) % table_size; if (linearTable[pos].key == key) return pos; if (linearTable[pos].key == -1) return -1; } return -1; }

for (int i = 0; i < table_size; i++) { free(linearTable[i].name); free(quadraticTable[i].name); free(doubleHashTable[i].name); } free(linearTable); free(quadraticTable); free(doubleHashTable); } int main() { printf("Enter the size of the Telephone Book Database: "); if (scanf("%d", &table_size) != 1 || table_size <= 1) { printf("Invalid table size.\n"); return 1; } getchar(); // consume newline init_tables(); int mainContinue = 1; while (mainContinue) { printf("\nMenu:\n1. Insert Client\n2. Display Database\n3. Search Client\n4. Exit\nEnter your choice: "); int choice; if (scanf("%d", &choice) != 1) { printf("Invalid input!\n"); while (getchar() != '\n'); continue; } getchar(); // consume newline if (choice == 1) { char again = 'y'; while (again == 'y' || again == 'Y') { long long key; char name[MAX_NAME_LENGTH]; while (1) { printf("Enter Telephone Number: "); if (scanf("%lld", &key) == 1 && key > 0) { getchar(); break; } printf("Invalid number! Try again.\n"); while (getchar() != '\n'); } while (1) { printf("Enter Client Name: "); if (!fgets(name, MAX_NAME_LENGTH, stdin)) continue; size_t len = strlen(name); if (len > 0 && name[len - 1] == '\n') name[len - 1] = '\0'; if (is_valid_name(name)) break; printf("Invalid name! Try again.\n"); } insert_linear(key, name); insert_quadratic(key, name); insert_double_hash(key, name); printf("Client Added!!\n"); printf("Do You Want to Insert More Key (y/n): "); again = getchar();

while (getchar() != '\n'); } char menuAgain; while (1) { printf("Do You Want to Continue in Main Menu (y/n): "); menuAgain = getchar(); while (getchar() != '\n'); if (menuAgain == 'y' || menuAgain == 'Y') break; if (menuAgain == 'n' || menuAgain == 'N') { mainContinue = 0; break; } printf("Please enter y or n.\n"); } } else if (choice == 2) { display_linear(); display_quadratic(); display_double_hash(); } else if (choice == 3) { long long key; printf("Enter Telephone Number to Search: "); if (scanf("%lld", &key) != 1) { printf("Invalid input!\n"); while (getchar() != '\n'); continue; } getchar(); // consume newline int index = search_linear(key); if (index != -1) printf("Linear Probing: Found '%s' at index %d\n", linearTable[index].name, index); else printf("Linear Probing: Not found.\n"); index = search_quadratic(key); if (index != -1) printf("Quadratic Probing: Found '%s' at index %d\n", quadraticTable[index].name, index); else printf("Quadratic Probing: Not found.\n"); index = search_double_hash(key); if (index != -1) printf("Double Hashing: Found '%s' at index %d\n", doubleHashTable[index].name, index); else printf("Double Hashing: Not found.\n"); } else if (choice == 4) { printf("Exiting...\n"); break; } else { printf("Invalid choice!\n"); } } cleanup(); return 0; }