Hash Table using Separate Chaining C Program

By | October 5, 2016

Learn how to create Hash Table using Separate Chaining in C Programming Language. The separate chaining hash table implementation makes use of Linked List in C Programming. There are different hashing algorithms such as Bucket Hashing, Linear Probing, Separate Chaining, etc.

Hash tables offers finding the element in less key comparisons, making the search operation to execute in a Constant Time. Therefore, the search time for the element is independent of the number of records.

Separate Chaining Concept

In separate chaining implementation of hash tables, linked lists are used for elements that have the same hash address. The hash tables in this scenario does not include the actual keys and records. It contains only an array of pointers where pointer points to a linked list.

All the elements having the same hash address will be stored in a separate linked list and the starting address of that particular linked list will be stored in the index of the hash table. In the chaining method, the comparisons are done only with the keys that have the same hash values.

The disadvantage of Separate Chaining is that it needs an extra space for storing pointers which is dependent on the table size and the records.

C Program For Hash Table using Separate Chaining and Linked List

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

#define MAX 20

struct Employee
{
      int employee_id;
      char employee_name[45];
      int employee_age;
};

struct Record 
{
      struct Employee data;
      struct Record *link;
};

void insert(struct Employee employee_record, struct Record *hash_table[]);
int search_element(int key, struct Record *hash_table[]);
void remove_record(int key, struct Record *hash_table[]);
void show(struct Record *hash_table[]);
int hash_function(int key);

int main()
{
      struct Record *hash_table[MAX];	
      struct Employee employee_record;
      int count, key, option;
      for(count = 0; count <= MAX - 1; count++)
      {
            hash_table[count] = NULL;
      }
      while(1)
      {
            printf("1. Insert a Record in Hash Table\n");
            printf("2. Search for a Record\n");
            printf("3. Delete a Record\n");
            printf("4. Show Hash Table\n");
            printf("5. Quit\n");
            printf("Enter your option\n");
            scanf("%d",&option);
            switch(option)
            {
                  case 1: 
                          printf("Enter the Employee Details\n");
                          printf("Employee ID:\t");
                          scanf("%d", &employee_record.employee_id);
                          printf("Employee Name:\t");
                          scanf("%s", employee_record.employee_name);
                          printf("Employee Age:\t");
                          scanf("%d", &employee_record.employee_age);
                          insert(employee_record, hash_table);
                          break;
                  case 2: 
                          printf("Enter the element to search:\t");
                          scanf("%d", &key);
                          count = search_element(key, hash_table);
                          if(count == -1)
                          {
                                printf("Element Not Found\n");
                          }
                          else
                          {
                                printf("Element Found in Chain:\t%d\n", count);
                          }
                          break;
                  case 3: 
                          printf("Enter the element to delete:\t");
                          scanf("%d", &key);
                          remove_record(key, hash_table);
                          break;
                  case 4:
                          show(hash_table);
                          break;
                  case 5:
                          exit(1);
            }
      }	
      return 0;
}

void insert(struct Employee employee_record, struct Record *hash_table[])
{
      int key, h;
      struct Record *temp;
      key = employee_record.employee_id;
      if(search_element(key, hash_table) != -1)
      {
            printf("Duplicate Key\n");
            return;
      }
      h = hash_function(key);				
      temp = malloc(sizeof(struct Record));
      temp->data = employee_record;
      temp->link = hash_table[h];
      hash_table[h] = temp;
}

void show(struct Record *hash_table[])
{
      int count;
      struct Record *ptr;
      for(count = 0; count < MAX; count++)
      {
            printf("\n[%3d]", count);
            if(hash_table[count] != NULL)
            {
                  ptr = hash_table[count];
                  while(ptr != NULL)
                  {
                        printf("%d %s %d\t", ptr->data.employee_id, ptr->data.employee_name, ptr->data.employee_age);
                        ptr=ptr->link;
                  }
            }
      }
      printf("\n");
}

int search_element(int key, struct Record *hash_table[])
{
      int h;
      struct Record *ptr;
      h = hash_function(key);				
      ptr = hash_table[h];
      while(ptr != NULL)
      {
            if(ptr->data.employee_id == key)
            {
                  return h;
            }
            ptr = ptr->link;
      }
      return -1;
}

void remove_record(int key, struct Record *hash_table[])
{
      int h;
      struct Record *temp, *ptr;
      h = hash_function(key);				
      if(hash_table[h]==NULL)
      {
            printf("Key %d Not Found\n", key);
            return;
      }
      if(hash_table[h]->data.employee_id == key)
      {
            temp = hash_table[h];
            hash_table[h] = hash_table[h]->link;  
            free(temp);
            return;
      }
      ptr = hash_table[h];
      while(ptr->link != NULL)
      {
            if(ptr->link->data.employee_id == key)   
            {
                  temp = ptr->link;
                  ptr->link = temp->link;
                  free(temp);
                  return;
            }
            ptr = ptr->link;
      }
      printf("Key %d Not Found\n", key);
}

int hash_function(int key)
{
      return (key % MAX);
}

Output

Hash Table Implementation using Separate Chaining in C Programming using Linked List

If you have any compilation errors or doubts about C Program For Separate Chaining Hash Table, let us know about it in the comment section below.

Hash Tables
C Program For Hash Table using Linear Probing
C Program For Travelling Salesman Problem
C Program For Linear Search Algorithm
C Program To Convert Postfix Expression into Infix Expression
C Program To Implement Caesar Cipher Algorithm
C Program For Binary Search Algorithm
C Program To Display Digital Clock
C Program For Binary Search Algorithm using Recursion
C Program To Convert Decimal into Binary, Hexadecimal and Octal Values
C Program For Booth’s Algorithm

4 thoughts on “Hash Table using Separate Chaining C Program

  1. Mahesh Asalkar

    This separate chaining algorithm for creating hash table uses Singly Linked List. Can we implement it using Doubly or Circular Linked Lists too?

    Reply
  2. Rajesh Mishra

    I was desperately searching how to implement hash table using linked list. Did not get much good answers. Thanks a lot codingalpha.

    Reply
  3. Gaurav Shinde

    It is really a simple separate chaining hashing algorithm in c programming. Thanks.

    Reply
  4. Sathish Arumugam

    Thanks for the explanation. The Separate Chaining Hash Program in C Language is too simple to understand.

    Reply

Let's Discuss