





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






#include #include #define SIZE 10 struct Client { char name[30]; char phone[15]; int occupied; }; struct Client table[SIZE]; int hash1(char name[]) { int sum = 0; int i; for (i = 0; name[i] != '\0'; i++) sum += name[i]; return sum % SIZE; } int hash2(char name[]) { int sum = 0; int i; for (i = 0; name[i] != '\0'; i++) sum += name[i]; return 7 - (sum % 7); } void initialize() { int i; for (i = 0; i < SIZE; i++) table[i].occupied = 0; void linearProbing() {
char name[30], phone[15]; int index, i, pos; printf("Enter client name: "); scanf("%s", name); printf("Enter telephone number: "); scanf("%s", phone); index = hash1(name); for (i = 0; i < SIZE; i++) { pos = (index + i) % SIZE; if (table[pos].occupied == 0) { strcpy(table[pos].name, name); strcpy(table[pos].phone, phone); table[pos].occupied = 1; printf("Client inserted at index %d\n", pos); return; } } printf("Hash table is full!\n"); } void quadraticProbing() { char name[30], phone[15]; int index, i, pos; printf("Enter client name: "); scanf("%s", name); printf("Enter telephone number: "); scanf("%s", phone); index = hash1(name);
return; } } printf("Hash table is full!\n"); } void display() { int i; printf("\n-------------------------------------\n"); printf("Index\tName\t\tPhone\n"); printf("-------------------------------------\n"); for (i = 0; i < SIZE; i++) { if (table[i].occupied) { printf("%d\t%s\t\t%s\n", i, table[i].name, table[i].phone); } else { printf("%d\tEMPTY\n", i); } } printf("-------------------------------------\n"); } void search() { char name[30]; int index, i, pos; printf("Enter client name to search: "); scanf("%s", name); index = hash1(name); for (i = 0; i < SIZE; i++) { pos = (index + i) % SIZE;
if (table[pos].occupied == 0) break; if (strcmp(table[pos].name, name) == 0) { printf("Client found!\n"); printf("Name : %s\n", table[pos].name); printf("Phone : %s\n", table[pos].phone); printf("Index : %d\n", pos); return; } } printf("Client not found!\n"); } int main() { int choice; initialize(); do { printf("\n===== TELEPHONE BOOK =====\n"); printf("1. Insert using Linear Probing\n"); printf("2. Insert using Quadratic Probing\n"); printf("3. Insert using Double Hashing\n"); printf("4. Search Client\n"); printf("5. Display Hash Table\n"); printf("6. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: linearProbing(); break;
0 Amit 9876543210 1 EMPTY 2 EMPTY 3 Neha 9988776655 4 Rahul 9123456780 5 EMPTY 6 EMPTY 7 EMPTY 8 EMPTY 9 EMPTY
===== TELEPHONE BOOK =====