0% found this document useful (0 votes)
22 views4 pages

Practical 10

This document defines functions for creating and manipulating a hash table data structure in C. It includes functions for creating a hash table, inserting key-value pairs using mid-square and division hashing, retrieving values, and displaying the table. The main function demonstrates a menu-driven program that allows users to insert, retrieve, and display contents of the hash table before freeing allocated memory.

Uploaded by

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

Practical 10

This document defines functions for creating and manipulating a hash table data structure in C. It includes functions for creating a hash table, inserting key-value pairs using mid-square and division hashing, retrieving values, and displaying the table. The main function demonstrates a menu-driven program that allows users to insert, retrieve, and display contents of the hash table before freeing allocated memory.

Uploaded by

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

PRACTICAL 10

#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10
// Structure for a hash table entry
struct HashEntry {
int key;
int value;
};
// Structure for a hash table
struct HashTable {
struct HashEntry* table[TABLE_SIZE];
};
// Function to create a new hash table
struct HashTable* createHashTable() {
struct HashTable* hashTable = (struct HashTable*)malloc(sizeof(struct HashTable));
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable->table[i] = NULL;
}
return hashTable;
}
// Function to calculate the mid-square hash of a given key
int midSquareHash(int key) {
int square = key * key;
int hash = (square / 100) % TABLE_SIZE; // Considering 2-digit square values
return hash;
}
// Function to calculate the division hash of a given key
int divisionHash(int key) {
int hash = key % TABLE_SIZE;
return hash;
}
// Function to insert a key-value pair into the hash table using the mid-square hash function
void insertMidSquare(struct HashTable* hashTable, int key, int value) {
int hash = midSquareHash(key);
struct HashEntry* entry = (struct HashEntry*)malloc(sizeof(struct HashEntry));
entry->key = key;
entry->value = value;
hashTable->table[hash] = entry;
}
// Function to insert a key-value pair into the hash table using the division hash function
void insertDivision(struct HashTable* hashTable, int key, int value) {
int hash = divisionHash(key);
struct HashEntry* entry = (struct HashEntry*)malloc(sizeof(struct HashEntry));
entry->key = key;
entry->value = value;
// Linear probing to handle collisions
while (hashTable->table[hash] != NULL) {
hash = (hash + 1) % TABLE_SIZE;
}
hashTable->table[hash] = entry;
}
// Function to retrieve the value associated with a given key from the hash table
int get(struct HashTable* hashTable, int key) {
int hash = midSquareHash(key);
struct HashEntry* entry = hashTable->table[hash];
// Linear probing to find the correct entry
while (entry != NULL && entry->key != key) {
hash = (hash + 1) % TABLE_SIZE;
entry = hashTable->table[hash];
}
if (entry == NULL) {
return -1; // Key not found
} else {
return entry->value;
}
}
// Function to display the contents of the hash table
void displayHashTable(struct HashTable* hashTable) {
printf("Hash Table:\n");
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashTable->table[i] != NULL) {
printf("Index %d: Key = %d, Value = %d\n", i, hashTable->table[i]->key, hashTable->table[i]-
>value);
}
}
}
int main() {
struct HashTable* hashTable = createHashTable();
int choice, key, value;
do {
printf("1. Insert using Mid-Square Hash Function\n");
printf("2. Insert using Division Hash Function\n");
printf("3. Get value for a key\n");
printf("4. Display Hash Table\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the key: ");
scanf("%d", &key);
printf("Enter the value: ");
scanf("%d", &value);
insertMidSquare(hashTable, key, value);
printf("Key-value pair inserted using Mid-Square Hash Function.\n");
break;
case 2:
printf("Enter the key: ");
scanf("%d", &key);
printf("Enter the value: ");
scanf("%d", &value);
insertDivision(hashTable, key, value);
printf("Key-value pair inserted using Division Hash Function.\n");
break;
case 3:
printf("Enter the key to get the value: ");
scanf("%d", &key);
value = get(hashTable, key);
if (value == -1) {
printf("Value not found for the given key.\n");
} else {
printf("Value for key %d: %d\n", key, value);
}
break;
case 4:
displayHashTable(hashTable);
break;
case 5:
printf("Exiting the program...\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
printf("\n");
} while (choice != 5); // Free the memory allocated for the hash table entries
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashTable->table[i] != NULL) {
free(hashTable->table[i]);
}
}
// Free the memory allocated for the hash table
free(hashTable);
return 0;
}

You might also like