0% found this document useful (0 votes)
13 views15 pages

Assig 7

The document contains two main sections demonstrating hash table implementations in C, including methods for collision resolution such as division, mid-square, digit folding, linear probing, and quadratic probing. Each section provides functions for inserting, searching, deleting, and displaying keys in the hash table, along with a menu-driven interface for user interaction. The expected output illustrates how keys are processed and the handling of collisions.

Uploaded by

yashodapawar10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views15 pages

Assig 7

The document contains two main sections demonstrating hash table implementations in C, including methods for collision resolution such as division, mid-square, digit folding, linear probing, and quadratic probing. Each section provides functions for inserting, searching, deleting, and displaying keys in the hash table, along with a menu-driven interface for user interaction. The expected output illustrates how keys are processed and the handling of collisions.

Uploaded by

yashodapawar10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SETA

#include <stdio.h>

#include <math.h>

#define TABLE_SIZE 10 // Hash table size for demonstration

int hash_table_div[TABLE_SIZE] = {0};

int hash_table_mid[TABLE_SIZE] = {0};

int hash_table_fold[TABLE_SIZE] = {0};

// Division Method

int hash_division(int key) {

return key % TABLE_SIZE;

// Mid-Square Method

int hash_mid_square(int key) {

int squared = key * key;

int mid_digits = (squared / 10) % 100; // Extract middle two digits

return mid_digits % TABLE_SIZE;

// Digit Folding Method

int hash_digit_folding(int key) {


int sum = 0;

while (key > 0) {

sum += key % 100; // Taking two digits at a time

key /= 100;

return sum % TABLE_SIZE;

void insert_and_check_collision(int key, int (*hash_func)(int), int *hash_table, const char *method) {

int index = hash_func(key);

if (hash_table[index] != 0) {

printf("Collision detected for key %d at index %d using %s method.\n", key, index, method);

} else {

hash_table[index] = key;

int main() {

int n, key;

printf("Enter the number of keys: ");

scanf("%d", &n);

int keys[n];

printf("Enter %d keys: ", n);

for (int i = 0; i < n; i++) {


scanf("%d", &keys[i]);

printf("\nKey\tDivision Hash\tMid-Square Hash\tDigit Folding Hash\n");

for (int i = 0; i < n; i++) {

key = keys[i];

int div_hash = hash_division(key);

int mid_hash = hash_mid_square(key);

int fold_hash = hash_digit_folding(key);

printf("%d\t%d\t\t%d\t\t%d\n", key, div_hash, mid_hash, fold_hash);

insert_and_check_collision(key, hash_division, hash_table_div, "Division");

insert_and_check_collision(key, hash_mid_square, hash_table_mid, "Mid-Square");

insert_and_check_collision(key, hash_digit_folding, hash_table_fold, "Digit Folding");

return 0;

/* Expected Output Example:

Enter the number of keys: 5

Enter 5 keys: 1234 5678 91011 1213 1415

Key Division Hash Mid-Square Hash Digit Folding Hash

1234 4 2 7
5678 8 9 9

91011 1 1 2

1213 3 7 4

1415 5 2 6

Collision detected for key 1415 at index 2 using Mid-Square method.

*/
SETB

1.Menu drive

#include <stdio.h>

#include <stdlib.h>

#define TABLE_SIZE 10 // Define the size of the hash table

// Hash table array

int hashTable[TABLE_SIZE];

// Initialize the hash table with -1 (indicating empty slots)

void initializeTable() {

for (int i = 0; i < TABLE_SIZE; i++) {

hashTable[i] = -1;

// Hash function

int hashFunction(int key) {

return key % TABLE_SIZE;

// Insert a key into the hash table using linear probing

void insert(int key) {


int index = hashFunction(key);

int originalIndex = index;

while (hashTable[index] != -1) { // Find an empty slot

index = (index + 1) % TABLE_SIZE;

if (index == originalIndex) { // Table is full

printf("Hash table is full! Cannot insert %d.\n", key);

return;

hashTable[index] = key;

printf("Inserted %d at index %d\n", key, index);

// Search for a key in the hash table

void search(int key) {

int index = hashFunction(key);

int originalIndex = index;

while (hashTable[index] != -1) {

if (hashTable[index] == key) {

printf("Key %d found at index %d\n", key, index);

return;

index = (index + 1) % TABLE_SIZE;


if (index == originalIndex) {

break;

printf("Key %d not found in the hash table.\n", key);

// Delete a key from the hash table

void delete(int key) {

int index = hashFunction(key);

int originalIndex = index;

while (hashTable[index] != -1) {

if (hashTable[index] == key) {

hashTable[index] = -1;

printf("Key %d deleted from index %d\n", key, index);

return;

index = (index + 1) % TABLE_SIZE;

if (index == originalIndex) {

break;

printf("Key %d not found in the hash table.\n", key);

}
// Display the hash table

void display() {

printf("Hash Table:\n");

for (int i = 0; i < TABLE_SIZE; i++) {

if (hashTable[i] == -1)

printf("[%d] : Empty\n", i);

else

printf("[%d] : %d\n", i, hashTable[i]);

int main() {

int choice, key;

initializeTable();

while (1) {

printf("\nMenu:\n");

printf("1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter key to insert: ");


scanf("%d", &key);

insert(key);

break;

case 2:

printf("Enter key to delete: ");

scanf("%d", &key);

delete(key);

break;

case 3:

printf("Enter key to search: ");

scanf("%d", &key);

search(key);

break;

case 4:

display();

break;

case 5:

exit(0);

default:

printf("Invalid choice! Please try again.\n");

return 0;

}
2.Menu drive

#include <stdio.h>

#include <stdlib.h>

#define TABLE_SIZE 10 // Define the size of the hash table

// Hash table array

int hashTable[TABLE_SIZE];

// Initialize the hash table with -1 (indicating empty slots)

void initializeTable() {

for (int i = 0; i < TABLE_SIZE; i++) {

hashTable[i] = -1;

// Hash function

int hashFunction(int key) {

return key % TABLE_SIZE;

// Insert a key into the hash table using quadratic probing

void insert(int key) {


int index = hashFunction(key);

int i = 1;

while (hashTable[index] != -1) { // Find an empty slot

index = (hashFunction(key) + i * i) % TABLE_SIZE;

i++;

if (i == TABLE_SIZE) { // Table is full

printf("Hash table is full! Cannot insert %d.\n", key);

return;

hashTable[index] = key;

printf("Inserted %d at index %d\n", key, index);

// Search for a key in the hash table

void search(int key) {

int index = hashFunction(key);

int i = 1;

while (hashTable[index] != -1) {

if (hashTable[index] == key) {

printf("Key %d found at index %d\n", key, index);

return;

}
index = (hashFunction(key) + i * i) % TABLE_SIZE;

i++;

if (i == TABLE_SIZE) {

break;

printf("Key %d not found in the hash table.\n", key);

// Delete a key from the hash table

void delete(int key) {

int index = hashFunction(key);

int i = 1;

while (hashTable[index] != -1) {

if (hashTable[index] == key) {

hashTable[index] = -1;

printf("Key %d deleted from index %d\n", key, index);

return;

index = (hashFunction(key) + i * i) % TABLE_SIZE;

i++;

if (i == TABLE_SIZE) {

break;

}
}

printf("Key %d not found in the hash table.\n", key);

// Display the hash table

void display() {

printf("Hash Table:\n");

for (int i = 0; i < TABLE_SIZE; i++) {

if (hashTable[i] == -1)

printf("[%d] : Empty\n", i);

else

printf("[%d] : %d\n", i, hashTable[i]);

int main() {

int choice, key;

initializeTable();

while (1) {

printf("\nMenu:\n");

printf("1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:

printf("Enter key to insert: ");

scanf("%d", &key);

insert(key);

break;

case 2:

printf("Enter key to delete: ");

scanf("%d", &key);

delete(key);

break;

case 3:

printf("Enter key to search: ");

scanf("%d", &key);

search(key);

break;

case 4:

display();

break;

case 5:

exit(0);

default:

printf("Invalid choice! Please try again.\n");

}
return 0;

You might also like