0% found this document useful (0 votes)
1 views3 pages

5

This document contains a C program that implements a simple hash table with basic operations such as insertion, searching, and deletion using linear probing for collision resolution. It defines a hash function and initializes the hash table, allowing for the insertion of keys and the management of collisions. The program also includes functionality to display the current state of the hash table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

5

This document contains a C program that implements a simple hash table with basic operations such as insertion, searching, and deletion using linear probing for collision resolution. It defines a hash function and initializes the hash table, allowing for the insertion of keys and the management of collisions. The program also includes functionality to display the current state of the hash table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

5.

code:

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

#define TABLE_SIZE 10 // Size of the hash table

// Hash Table structure


typedef struct HashTable {
int *table;
} HashTable;

// Hash function: simple modulo-based hash function


int hash(int key) {
return key % TABLE_SIZE;
}

// Initialize the hash table


void initHashTable(HashTable *ht) {
ht->table = (int *)malloc(TABLE_SIZE * sizeof(int));

// Initialize all slots as empty (using -1 to represent an empty slot)


for (int i = 0; i < TABLE_SIZE; i++) {
ht->table[i] = -1;
}
}

// Insert a key into the hash table using linear probing


void insert(HashTable *ht, int key) {
int index = hash(key);

// Linear probing: check the next available slot


while (ht->table[index] != -1) {
// If the key already exists, we update it (optional depending on use case)
if (ht->table[index] == key) {
printf("Key %d already exists at index %d\n", key, index);
return;
}
// Probe the next index
index = (index + 1) % TABLE_SIZE;
}

// Insert the key at the found empty slot


ht->table[index] = key;
printf("Inserted key %d at index %d\n", key, index);
}

// Search for a key in the hash table


int search(HashTable *ht, int key) {
int index = hash(key);

// Linear probing: search for the key


while (ht->table[index] != -1) {
if (ht->table[index] == key) {
return index; // Key found
}
index = (index + 1) % TABLE_SIZE;
}

return -1; // Key not found


}

// Delete a key from the hash table


void delete(HashTable *ht, int key) {
int index = search(ht, key);

if (index == -1) {
printf("Key %d not found in the table.\n", key);
return;
}

// Delete the key by marking it as deleted (using -1 or another special value)


ht->table[index] = -1;
printf("Key %d deleted from index %d\n", key, index);
}

// Display the hash table


void display(HashTable *ht) {
printf("Hash Table: ");
for (int i = 0; i < TABLE_SIZE; i++) {
if (ht->table[i] != -1) {
printf("[%d] ", ht->table[i]);
} else {
printf("[ ] ");
}
}
printf("\n");
}

int main() {
HashTable ht;
initHashTable(&ht);

// Inserting keys into the hash table


insert(&ht, 15);
insert(&ht, 25);
insert(&ht, 35);
insert(&ht, 45);
insert(&ht, 55);
insert(&ht, 5); // This will cause a collision

display(&ht);

// Searching for keys


int key = 25;
int index = search(&ht, key);
if (index != -1) {
printf("Key %d found at index %d\n", key, index);
} else {
printf("Key %d not found\n", key);
}

// Deleting a key
delete(&ht, 25);
display(&ht);
return 0;
}

You might also like