#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10
// Define the structure of each element in the hash table
typedef struct {
int key;
int value;
int is_occupied; // 0 means empty, 1 means occupied
} HashItem;
// Initialize the hash table as an array
HashItem hashTable[TABLE_SIZE];
// Hash function to map keys to indices
int hashFunction(int key) {
return key % TABLE_SIZE;
}
// Function to insert a key-value pair into the hash table
void insert(int key, int value) {
int index = hashFunction(key);
// Use linear probing in case of collision
while (hashTable[index].is_occupied) {
if (hashTable[index].key == key) {
// If key already exists, update the value
hashTable[index].value = value;
printf("Updated key %d with value %d at index %d\n", key, value, index);
return;
}
index = (index + 1) % TABLE_SIZE; // Move to the next index
}
// Insert the new key-value pair
hashTable[index].key = key;
hashTable[index].value = value;
hashTable[index].is_occupied = 1; // Mark as occupied
printf("Inserted key %d with value %d at index %d\n", key, value, index);
}
// Function to search for a value by its key in the hash table
int search(int key) {
int index = hashFunction(key);
// Use linear probing to search for the key
while (hashTable[index].is_occupied) {
if (hashTable[index].key == key) {
// If key is found, return the value
return hashTable[index].value;
}
index = (index + 1) % TABLE_SIZE; // Move to the next index
}
// If key is not found, return -1
return -1;
}
// Function to delete a key-value pair from the hash table
void delete(int key) {
int index = hashFunction(key);
// Use linear probing to find the key
while (hashTable[index].is_occupied) {
if (hashTable[index].key == key) {
// Mark the slot as empty
hashTable[index].is_occupied = 0;
printf("Deleted key %d from index %d\n", key, index);
return;
}
index = (index + 1) % TABLE_SIZE; // Move to the next index
}
printf("Key %d not found for deletion\n", key);
}
// Function to display the hash table
void display() {
printf("Hash Table:\n");
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashTable[i].is_occupied) {
printf("Index %d: Key %d, Value %d\n", i, hashTable[i].key, hashTable[i].value);
} else {
printf("Index %d: Empty\n", i);
}
}
}
// Main function to test the hash table
int main() {
// Initialize the hash table slots as empty
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable[i].is_occupied = 0;
}
// Insert some key-value pairs
insert(1, 10);
insert(11, 20);
insert(21, 30);
insert(31, 40);
// Display the hash table
display();
// Search for a key
int key = 11;
int result = search(key);
if (result != -1) {
printf("Value for key %d: %d\n", key, result);
} else {
printf("Key %d not found in hash table\n", key);
}
// Delete a key
delete(11);
display();
return 0;
}
Explanation of the Code
HashItem Structure:
key: The key for the hash table.
value: The associated value.
is_occupied: A flag to indicate whether the slot is occupied (1) or empty (0).
hashFunction: It computes the index using the formula key % TABLE_SIZE to ensure the index falls within the array bounds.
Insertion (insert function):
Compute the index using the hash function.
Use linear probing if the computed index is occupied (i.e., continue checking the next slot until an empty slot is found).
Insert the key-value pair at the first empty slot or update the value if the key already exists.
Search (search function):
Compute the index using the hash function.
Use linear probing to find the key. If found, return its value; otherwise, return -1.
Deletion (delete function):
Compute the index using the hash function.
Use linear probing to find the key and mark the slot as empty when found.
Display (display function): This function iterates over the hash table and prints the content of each slot.
Main Function:
Initializes the hash table.
Performs insertions, displays the table, searches for a key, deletes a key, and displays the table again to show changes.
Output of the Program
Inserted key 1 with value 10 at index 1
Inserted key 11 with value 20 at index 2
Inserted key 21 with value 30 at index 3
Inserted key 31 with value 40 at index 4
Hash Table:
Index 0: Empty
Index 1: Key 1, Value 10
Index 2: Key 11, Value 20
Index 3: Key 21, Value 30
Index 4: Key 31, Value 40
Index 5: Empty
Index 6: Empty
Index 7: Empty
Index 8: Empty
Index 9: Empty
Value for key 11: 20
Deleted key 11 from index 2
Hash Table:
Index 0: Empty
Index 1: Key 1, Value 10
Index 2: Empty
Index 3: Key 21, Value 30
Index 4: Key 31, Value 40
Index 5: Empty
Index 6: Empty
Index 7: Empty
Index 8: Empty
Index 9: Empty