Docsity

It's data structure lab practical no 3, Study Guides, Projects, Research of Data Structures and Algorithms

Toggle sidebar
bg1
Program
#include <stdio.h>
#include <string.h>
#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() {
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download It's data structure lab practical no 3 and more Study Guides, Projects, Research Data Structures and Algorithms in PDF only on Docsity!

Program

#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;

Output

===== TELEPHONE BOOK =====

  1. Insert using Linear Probing
  2. Insert using Quadratic Probing
  3. Insert using Double Hashing
  4. Search Client
  5. Display Hash Table
  6. Exit Enter your choice: 1 Enter client name: Amit Enter telephone number: 9876543210 Client inserted at index 0 ===== TELEPHONE BOOK =====
  7. Insert using Linear Probing
  8. Insert using Quadratic Probing
  9. Insert using Double Hashing
  10. Search Client
  11. Display Hash Table
  12. Exit Enter your choice: 2 Enter client name: Rahul Enter telephone number: 9123456780 Client inserted at index 4 ===== TELEPHONE BOOK =====
  13. Insert using Linear Probing
  14. Insert using Quadratic Probing
  15. Insert using Double Hashing
  16. Search Client
  17. Display Hash Table
  1. Exit Enter your choice: 3 Enter client name: Neha Enter telephone number: 9988776655 Client inserted at index 3 ===== TELEPHONE BOOK =====
  2. Insert using Linear Probing
  3. Insert using Quadratic Probing
  4. Insert using Double Hashing
  5. Search Client
  6. Display Hash Table
  7. Exit Enter your choice: 5

Index Name Phone

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 =====

  1. Insert using Linear Probing