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

Ads Hash

The document describes an experiment implementing hashing and collision handling in C. It defines a Node structure for storing key-value pairs in a hash table using separate chaining. Functions are included to initialize an empty hash table, insert key-value pairs by hashing the key and adding nodes to a linked list, and search for keys by hashing and traversing the corresponding linked list. The main function tests inserting three key-value pairs and searching for one pair.

Uploaded by

rohankhandare429
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)
14 views4 pages

Ads Hash

The document describes an experiment implementing hashing and collision handling in C. It defines a Node structure for storing key-value pairs in a hash table using separate chaining. Functions are included to initialize an empty hash table, insert key-value pairs by hashing the key and adding nodes to a linked list, and search for keys by hashing and traversing the corresponding linked list. The main function tests inserting three key-value pairs and searching for one pair.

Uploaded by

rohankhandare429
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/ 4

Experiment Number:

Problem Statement: Implement all the hashing taking and also collision

NAME: Rohan Bhanudas Khandare ROLLNO: 29


CLASS: IT B BATCH: 03
DATE OF PERFORMANCE:
___________________________________________________________________________

#include <stdio.h>
#include <stdlib.h>

#define TABLE_SIZE 10

// Node structure for separate chaining


struct Node {
int key;
int value;
struct Node* next;
};

// Hash table structure for separate chaining


struct HashTable {
struct Node* table[TABLE_SIZE];
};

// Hashing function using Division method


int division_hash(int key) {
return key % TABLE_SIZE;
}
// Hashing function using Multiplication method
int multiplication_hash(int key) {
double A = 0.6180339887; // A constant value (0 < A < 1)
double val = A * key;
val -= (int)val;
return (int)(TABLE_SIZE * val);
}
// Initialize the hash table
void init_hash_table(struct HashTable* ht) {
for (int i = 0; i < TABLE_SIZE; i++) {
ht->table[i] = NULL;
}
}
// Insert a key-value pair into the hash table using separate
chaining
void insert_separate_chaining(struct HashTable* ht, int key, int
value) {
int index = division_hash(key); // You can change the hash
function here
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));
newNode->key = key;
newNode->value = value;
newNode->next = NULL;

if (ht->table[index] == NULL) {
ht->table[index] = newNode;
} else {
struct Node* current = ht->table[index];
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}

// Search for a key in the hash table using separate chaining


int search_separate_chaining(struct HashTable* ht, int key) {
int index = division_hash(key); // You can change the hash
function here
struct Node* current = ht->table[index];
while (current != NULL) {
if (current->key == key) {
return current->value;
}
current = current->next;
}
return -1; // Key not found
}
int main() {
struct HashTable ht;
init_hash_table(&ht);

// Insert key-value pairs into the hash table


insert_separate_chaining(&ht, 12, 100);
insert_separate_chaining(&ht, 22, 200);
insert_separate_chaining(&ht, 32, 300);

// Search for values using keys


int key_to_search = 22;
int result = search_separate_chaining(&ht, key_to_search);
if (result != -1) {
printf("Value for key %d: %d\n", key_to_search, result);
} else {
printf("Key %d not found.\n", key_to_search);
}

return 0;
}

OUTPUT:

You might also like