C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Hash Table Basics

What is a Hash Table?

A hash table (or hash map) is a data structure that stores key–value pairs and provides fast insertion, deletion, and lookup — typically in O(1) time on average.

It uses a hash function to map a given key to an index in an array (called the hash table).

 Basic Idea

  1. You have a set of keys — e.g., {10, 25, 35, 20}
  2. The hash function computes an index for each key:
  1. index = key % TABLE_SIZE
  1. Keys are stored in that index of an array.

Example:

Let TABLE_SIZE = 10 and keys = [10, 25, 35, 20]

Key

Hash (key % 10)

Index

10

0

0

25

5

5

35

5

5 (collision)

20

0

0 (collision)

 Collision

When two keys map to the same index, a collision occurs.

Collision Resolution Techniques:

  1. Chaining → Each index holds a linked list of values.
  2. Open Addressing → Find the next free slot using:
    • Linear Probing
    • Quadratic Probing
    • Double Hashing

 

C Program: Hash Table Basics

Hash Table using Chaining (C Program)

C

#include <stdio.h>

#include <stdlib.h>

 

#define TABLE_SIZE 10

 

// Node structure for chaining

struct Node {

    int data;

    struct Node* next;

};

 

struct Node* hashTable[TABLE_SIZE];

 

// Hash function

int hashFunction(int key) {

    return key % TABLE_SIZE;

}

 

// Insert key into hash table

void insert(int key) {

    int index = hashFunction(key);

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = key;

    newNode->next = NULL;

 

    if (hashTable[index] == NULL) {

        hashTable[index] = newNode;

    } else {

        // Insert at beginning of linked list

        newNode->next = hashTable[index];

        hashTable[index] = newNode;

    }

}

 

// Search key in hash table

int search(int key) {

    int index = hashFunction(key);

    struct Node* temp = hashTable[index];

    while (temp != NULL) {

        if (temp->data == key)

            return 1;

        temp = temp->next;

    }

    return 0;

}

 

// Display hash table

void display() {

    for (int i = 0; i < TABLE_SIZE; i++) {

        struct Node* temp = hashTable[i];

        printf("[%d] -> ", i);

        while (temp != NULL) {

            printf("%d -> ", temp->data);

            temp = temp->next;

        }

        printf("NULL\n");

    }

}

 

int main() {

    int choice, key;

 

    while (1) {

        printf("\n1. Insert\n2. Search\n3. Display\n4. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1:

                printf("Enter key to insert: ");

                scanf("%d", &key);

                insert(key);

                break;

            case 2:

                printf("Enter key to search: ");

                scanf("%d", &key);

                if (search(key))

                    printf("Key %d found in hash table.\n", key);

                else

                    printf("Key %d not found.\n", key);

                break;

            case 3:

                display();

                break;

            case 4:

                exit(0);

            default:

                printf("Invalid choice!\n");

        }

    }

}

Output

 
OUTPUT :
1. Insert
2. Search
3. Display
4. Exit
Enter your choice: 1
Enter key to insert: 15
Enter your choice: 1
Enter key to insert: 25
Enter your choice: 1
Enter key to insert: 35
Enter your choice: 3

[0] -> NULL
[1] -> NULL
[2] -> NULL
[3] -> NULL
[4] -> NULL
[5] -> 35 -> 25 -> 15 -> NULL
[6] -> NULL
[7] -> NULL
[8] -> NULL
[9] -> NULL

Time Complexity

Operation

Average Case

Worst Case

Insertion

O(1)

O(n)

Deletion

O(1)

O(n)

Search

O(1)

O(n)

Worst case occurs when all keys hash to the same index (poor hash function).

Key Points

  • Fast lookups using hash function.
  • Handles collisions using chaining or open addressing.
  • Used in symbol tables, password storage, dictionaries, etc.
  • Choosing a good hash function and load factor is critical for performance.