0% found this document useful (0 votes)
10 views6 pages

Exercise 9

The document provides two implementations: a hash table with collision resolution using chaining and a simple cache using hashing. The hash table supports insertion, searching, and displaying key-value pairs, while the cache allows adding and retrieving data based on keys. Both implementations include memory management and handle collisions effectively.

Uploaded by

kodurusailalitya
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)
10 views6 pages

Exercise 9

The document provides two implementations: a hash table with collision resolution using chaining and a simple cache using hashing. The hash table supports insertion, searching, and displaying key-value pairs, while the cache allows adding and retrieving data based on keys. Both implementations include memory management and handle collisions effectively.

Uploaded by

kodurusailalitya
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/ 6

Exercise 9: Hashing

i) Implement a hash table with collision resolution techniques.

Programme:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 10

// Node for storing key-value pairs


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

// Hash table structure


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

// Function to create a new node


struct Node *createNode(char *key, int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->key = strdup(key);
newNode->value = value;
newNode->next = NULL;
return newNode;
}

// Function to initialize hash table


struct HashTable *createHashTable() {
struct HashTable *hashTable = (struct HashTable *)malloc(sizeof(struct HashTable));
if (hashTable == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
for (int i = 0; i < SIZE; i++) {
hashTable->table[i] = NULL;
}
return hashTable;
}

// Function to calculate hash value


int hash(char *key) {
int hashValue = 0;
for (int i = 0; key[i] != '\0'; i++) {
hashValue += key[i];
}
return hashValue % SIZE;
}

// Function to insert key-value pair into hash table


void insert(struct HashTable *hashTable, char *key, int value) {
int index = hash(key);
struct Node *newNode = createNode(key, value);

if (hashTable->table[index] == NULL) {
hashTable->table[index] = newNode;
} else {
// Collision detected, chaining
struct Node *current = hashTable->table[index];
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}

// Function to search for a key in the hash table


int search(struct HashTable *hashTable, char *key) {
int index = hash(key);
struct Node *current = hashTable->table[index];
while (current != NULL) {
if (strcmp(current->key, key) == 0) {
return current->value;
}
current = current->next;
}
return -1; // Key not found
}

// Function to display the hash table


void display(struct HashTable *hashTable) {
for (int i = 0; i < SIZE; i++) {
struct Node *current = hashTable->table[i];
printf("Bucket %d: ", i);
while (current != NULL) {
printf("(%s, %d) ", current->key, current->value);
current = current->next;
}
printf("\n");
}
}

// Main function
int main() {
struct HashTable *hashTable = createHashTable();

// Inserting some key-value pairs


insert(hashTable, "apple", 10);
insert(hashTable, "banana", 20);
insert(hashTable, "orange", 30);
insert(hashTable, "grape", 40);
insert(hashTable, "melon", 50);

// Display the hash table


display(hashTable);

// Searching for a key


printf("\nValue for key 'orange': %d\n", search(hashTable, "orange"));
printf("Value for key 'pear': %d\n", search(hashTable, "pear"));
return 0;
}

out put:
ucket 0: (apple, 10)
Bucket 1:
Bucket 2:
Bucket 3:
Bucket 4:
Bucket 5:
Bucket 6: (orange, 30)
Bucket 7: (grape, 40)
Bucket 8:
Bucket 9: (banana, 20) (melon, 50)

Value for key 'orange': 30


Value for key 'pear': -1

ii) Write a program to implement a simple cache using hashing.

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

#define CACHE_SIZE 10

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

struct Cache {
struct Node* entries[CACHE_SIZE];
};

struct Node* createNode(int key, int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->key = key;
newNode->data = data;
newNode->next = NULL;
return newNode;
}

int hash(int key) {


return key % CACHE_SIZE;
}

void addToCache(struct Cache* cache, int key, int data) {


int index = hash(key);
struct Node* newNode = createNode(key, data);

if (cache->entries[index] == NULL) {
cache->entries[index] = newNode;
} else {
struct Node* temp = cache->entries[index];
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

int getFromCache(struct Cache* cache, int key) {


int index = hash(key);
struct Node* temp = cache->entries[index];

while (temp != NULL) {


if (temp->key == key) {
return temp->data;
}
temp = temp->next;
}

return -1; // Key not found in cache


}

int main() {
struct Cache cache;
for (int i = 0; i < CACHE_SIZE; i++) {
cache.entries[i] = NULL;
}

addToCache(&cache, 1, 10);
addToCache(&cache, 2, 20);
addToCache(&cache, 11, 110); // Collides with key 1

printf("Data corresponding to key 1: %d\n", getFromCache(&cache, 1));


printf("Data corresponding to key 2: %d\n", getFromCache(&cache, 2));
printf("Data corresponding to key 11: %d\n", getFromCache(&cache, 11));

return 0;
}

out put:
Data corresponding to key 1: 10
Data corresponding to key 2: 20
Data corresponding to key 11: 110

You might also like