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

15A. Hash Table and Function

The document contains multiple C code implementations of hash tables, including methods for handling collisions such as separate chaining and linear probing. It demonstrates basic operations like insertion, deletion, and searching for values in the hash table. The examples illustrate both simple integer hashing and string key-value pairs with linked list structures for collision resolution.

Uploaded by

Gayu Ks
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)
14 views18 pages

15A. Hash Table and Function

The document contains multiple C code implementations of hash tables, including methods for handling collisions such as separate chaining and linear probing. It demonstrates basic operations like insertion, deletion, and searching for values in the hash table. The examples illustrate both simple integer hashing and string key-value pairs with linked list structures for collision resolution.

Uploaded by

Gayu Ks
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/ 18

Code-1 (ChatGPT):

Code-2

#include<stdio.h>
#define size 7
int arr[size];

void init()
{
int i;
for(i = 0; i < size; i++)
arr[i] = -1;
}

void insert(int value)


{
int key = value % size;
if(arr[key] == -1)
{
arr[key] = value;
printf("%d inserted at arr[%d]\n", value,key);
}
else
{
printf("Collision : arr[%d] has element %d already!\n",key,arr[key]);
printf("Unable to insert %d\n",value);
}
}

void del(int value)


{
int key = value % size;
if(arr[key] == value)
arr[key] = -1;
else
printf("%d not present in the hash table\n",value);
}

void search(int value)


{
int key = value % size;
if(arr[key] == value)
printf("Search Found\n");
else
printf("Search Not Found\n");
}

void print()
{
int i;
for(i = 0; i < size; i++)
printf("arr[%d] = %d\n",i,arr[i]);
}

int main()
{
init();
insert(10); //key = 10 % 7 ==> 3
insert(4); //key = 4 % 7 ==> 4
insert(2); //key = 2 % 7 ==> 2
insert(3); //key = 3 % 7 ==> 3 (collision)

printf("Hash table\n");
print();
printf("\n");

printf("Deleting value 10..\n");


del(10);
printf("After the deletion hash table\n");
print();
printf("\n");

printf("Deleting value 5..\n");


del(5);
printf("After the deletion hash table\n");
print();
printf("\n");

printf("Searching value 4..\n");


search(4);
printf("Searching value 10..\n");
search(10);

return 0;
}

Another code:
CODING-3 for avoiding collision

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 10

typedef struct Node {


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

typedef struct HashTable {


Node* table[TABLE_SIZE];
} HashTable;

// Hash function
unsigned int hash(const char* key)
{
unsigned int hash_value = 0;
int i;
const unsigned int prime = 31;

for (i = 0; key[i] != '\0'; i++) {


hash_value = hash_value * prime + key[i];
}
return hash_value % TABLE_SIZE;
}

// Create a new node


Node* new_node(const char* key, int value) {
Node* node = (Node*) malloc(sizeof(Node));
node->key = strdup(key);
node->value = value;
node->next = NULL;
return node;
}

// Insert a key-value pair into the hash table


void insert(HashTable* hash_table, const char* key, int value) {
unsigned int index = hash(key);
Node* node = hash_table->table[index];
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
node->value = value; // Key already exists, update value
return;
}
node = node->next;
}
// Key not found, insert new node at beginning of list
node = new_node(key, value);
node->next = hash_table->table[index];
hash_table->table[index] = node;
}

// Search for a key in the hash table and return its value
int search(HashTable* hash_table, const char* key) {
unsigned int index = hash(key);
Node* node = hash_table->table[index];
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
return node->value;
}
node = node->next;
}
return -1; // Key not found
}

// Delete a key-value pair from the hash table


void delete(HashTable* hash_table, const char* key) {
unsigned int index = hash(key);
Node* node = hash_table->table[index];
Node* prev_node = NULL;
while (node != NULL) {
if (strcmp(node->key, key) == 0) {
if (prev_node == NULL) {
hash_table->table[index] = node->next; // Node is first in list
} else {
prev_node->next = node->next; // Node is not first in list
}
free(node->key);
free(node);
return;
}
prev_node = node;
node = node->next;
}
}

// Print the contents of the hash table


void print_hash_table(HashTable* hash_table) {
int i;
for (i = 0; i < TABLE_SIZE; i++) {
Node* node = hash_table->table[i];
printf("%d:", i);
while (node != NULL) {
printf(" -> %s:%d", node->key, node->value);
node = node->next;
}
printf("\n");
}
}
// Test the hash table
int main() {
HashTable hash_table = {0};
insert(&hash_table, "apple", 1);
insert(&hash_table, "banana", 2);
print_hash_table(&hash_table);
}

Another Coding to avoid collision


● Collision Avoidance using Linear Probing
● Collision Avoidance using open hashing Separate Chaining

Linear Probing
Calculate the hash key. key = data % size;

If hashTable [key] is empty, store the value directly. hashTable [key] = data.

If the hash index already has some value, check for next index.

key = (key+1) % size;

If the next index is available hashTable[key], store the value. Otherwise try for next index.

Do the above process till we find the space.


Separate chaining implementation in c (code-4)
#include<stdio.h>
#include<stdlib.h>

#define size 7

struct node
{
int data;
struct node *next;
};

struct node *chain[size];


void init()
{
int i;
for(i = 0; i < size; i++)
chain[i] = NULL;
}

void insert(int value)


{
//create a newnode with value
struct node *newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->next = NULL;

//calculate hash key


int key = value % size;

//check if chain[key] is empty


if(chain[key] == NULL)
chain[key] = newNode;
//collision
else
{
//add the node at the end of chain[key].
struct node *temp = chain[key];
while(temp->next)
{
temp = temp->next;
}

temp->next = newNode;
}
}

void print()
{
int i;

for(i = 0; i < size; i++)


{
struct node *temp = chain[i];
printf("chain[%d]-->",i);
while(temp)
{
printf("%d -->",temp->data);
temp = temp->next;
}
printf("NULL\n");
}
}

int main()
{
//init array of list to NULL
init();

insert(7);
insert(0);
insert(3);
insert(10);
insert(4);
insert(5);

print();

return 0;
}

You might also like