Exercise 9
Exercise 9
Programme:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
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;
}
}
// Main function
int main() {
struct HashTable *hashTable = createHashTable();
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)
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];
};
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 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
return 0;
}
out put:
Data corresponding to key 1: 10
Data corresponding to key 2: 20
Data corresponding to key 11: 110