Hash Table
Description
A hash table (hash map) is a data structure used to implement an associative array, a structure that can map
keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from
which the correct value can be found.
Ideally, the hash function will assign each key to a unique bucket, but it is possible that two keys will generate
an identical hash causing both keys to point to the same bucket. Instead, most hash table designs assume
that hash collisions—different keys that are assigned by the hash function to the same bucket—will occur and
must be accommodated in some way. Most hash table implementations have some collision resolution
strategy to handle such events. One such method is the separate chaining method which is implemented
using linked lists. Chained hash tables with linked lists are popular because they require only basic data
structures with simple algorithms, and can use simple hash functions that are unsuitable for other methods.
In the method known as separate chaining, each bucket is independent, and has some sort of list of entries
with the same index. The time for hash table operations is the time to find the bucket (which is constant) plus
the time for the list operation.
In a good hash table, each bucket has zero or one entries, and sometimes two or three, but rarely more than
that. Therefore, structures that are efficient in time and space for these cases are preferred. For separatechaining, the worst-case scenario is when all entries are inserted into the same bucket, in which case the
hash table is ineffective and the cost is that of searching the bucket data structure. If the latter is a linear list,
the lookup procedure may have to scan all its entries, so the worst-case cost is proportional to the number n
of entries in the table.
Illustrations :
Insertion :
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Insert 315 :
Deletion :
Delete 100 :
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Search Element :
Search for key 407 :
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Code
struct node
{
int key,Mark;
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
char name[100];
struct node *next;
};
struct hash
{
struct node *head;
int count;
};
struct hash *hashTable = NULL;
int ElementCount = 0;
struct node * CreateNode(int key, char *name, int mark);
void InsertToTable(int key, char *name, int mark);
void DeleteFromTable(int key);
void SearchElement(int key);
void DisplayTable();
void main()
{
int NumOfElements, choice, key, mark;
char name[100];
printf("Enter the number of elements:");
scanf("%d", &NumOfElements);
ElementCount = NumOfElements;
hashTable = (struct hash *)calloc(NumOfElements, sizeof (struct hash));
while (1)
{
printf("1.Insert \
2.Delete \
3.Search \
4.Display \
5.Exit ");
printf("Enter your choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the key value: ");
scanf("%d", &key);
printf("Name: ");
scanf("%s",name);
printf("Mark: ");
scanf("%d", &mark);
InsertToTable(key, name, mark);
break;
case 2:
printf("Enter the key to perform deletion:");
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
scanf("%d", &key);
DeleteFromTable(key);
break;
case 3:
printf("Enter the key to search:");
scanf("%d", &key);
SearchElement(key);
break;
case 4:
DisplayTable();
break;
case 5:
exit(0);
default:
printf("Enter a valid option\n");
break;
}
}
}
struct node * CreateNode(int key, char *name, int mark)
{
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->key = key;
newnode->Mark = mark;
strcpy(newnode->name, name);
newnode->next = NULL;
return newnode;
}
void InsertToTable(int key, char *name, int mark)
{
int hashIndex = key % ElementCount;
struct node *newnode = CreateNode(key, name, mark);
if (!hashTable[hashIndex].head) {
hashTable[hashIndex].head = newnode;
hashTable[hashIndex].count = 1;
return;
}
newnode->next = (hashTable[hashIndex].head);
hashTable[hashIndex].head = newnode;
hashTable[hashIndex].count++;
return;
}
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
void DeleteFromTable(int key)
{
int hashIndex = key % ElementCount;
int flag = 0;
struct node *temp, *myNode;
myNode = hashTable[hashIndex].head;
if (!myNode)
{
printf("Given input is not present in hash Table\n");
return;
}
temp = myNode;
while (myNode != NULL)
{
if (myNode->key == key)
{
flag = 1;
if (myNode == hashTable[hashIndex].head)
hashTable[hashIndex].head = myNode->next;
else
temp->next = myNode->next;
hashTable[hashIndex].count--;
free(myNode);
break;
}
temp = myNode;
myNode = myNode->next;
}
if (flag)
printf("Data is deleted from Hash Table\n");
else
printf("Given data is not present in Hash table\n");
return;
}
void SearchElement(int key)
{
int hashIndex = key % ElementCount;
int flag = 0;
struct node *myNode;
myNode = hashTable[hashIndex].head;
if (!myNode) {
printf("Element is not found in the table \n");
return;
}
while (myNode != NULL)
{
if (myNode->key == key) {
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
printf("StudentID : %d\n", myNode->key);
printf("Name
: %s\n", myNode->name);
printf("Mark
: %d\n", myNode->Mark);
flag = 1;
break;
}
myNode = myNode->next;
}
if (!flag)
printf("Element unavailable in hash table\n");
return;
}
void DisplayTable()
{
struct node *myNode;
int i;
for (i = 0; i < ElementCount; i++) {
if (hashTable[i].count == 0)
continue;
myNode = hashTable[i].head;
if (!myNode)
continue;
printf("\nData at index %d :\n", i);
printf("StudentID
Name
Mark
while (myNode != NULL) {
printf("%-12d", myNode->key);
printf("%-15s", myNode->name);
printf("%d\n", myNode->Mark);
myNode = myNode->next;
}
}
return;
}
\n");
Code Walk
struct node
{
int key,Mark;
char name[100];
struct node *next;
};
A structure 'node' is defined to store the
details of a single node in the list. It
contains the details of a single student i.e
Student ID, Mark and the student name.
Also, is contains a pointer which stores the
address of the next node which will be
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
added to the list.
struct hash
{
struct node *head;
int count;
};
struct hash *hashTable = NULL;
int ElementCount = 0;
struct node * CreateNode(int key, char *name, int mark);
void InsertToTable(int key, char *name, int mark);
void DeleteFromTable(int key);
A structure 'hash' is defined to store entires
of the linked list. The entries are hashed to
the table using a hash function which
maintains a count of the number of nodes
added to each entry in the hash table.
A pointer to the hash table, hashTable, is
initially as NULL and is a global variable
which implies that it can be accessed by all
the functions in the program.
The global integer variable ElementCount is
used to denote the number of entries in
the hash table.
This indicates the declaration for the
function CreateNode() which is used to
create a new node to be inserted into a list
based on the value generated from the
hash function. It takes the key value, name
and mark of a single student as the input
arguments.
This indicates the declaration for the
function InsertToTable() which is used to
insert a new node into a list based on the
value generated from the hash function. It
takes the key value, name and mark of a
single student as the input arguments.
This indicates the declaration for the
function DeleteFromTable() which is used
to delete a node from the given list by
searching for it using the key value. It takes
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
the key value as the input argument.
void SearchElement(int key);
void DisplayTable();
This indicates the declaration for the
function SearchElement() which is used to
search for a node from the given list by
using the key value. It takes the key value
as the input argument.
This indicates the declaration for the
function Display() which is used to display
the contents of the hash table on the
screen.
Function : main
The program begins execution from main().
The main program contains the following
local variables.
int NumOfElements, choice, key, mark;
char name[100];
NumOfElements
: Used to denote the
number of entries created in the hash
table.
choice : Used to obtain the choice for the
operation to be performed from the user.
Key,mark,name
: Used to obtain the
details for the particular student which are
the StudentID, mark and the name of the
student respectively.
printf("Enter the number of elements:");
scanf("%d", &NumOfElements);
ElementCount = NumOfElements;
The variable NumOfElements to denote the
number of entries created in the hash table
is obtained as input from the user and
assigned
to
the
global
variable
ElementCount.
The memory for the pointer to the hash
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
hashTable = (struct hash *)calloc(NumOfElements,
sizeof (struct hash));
while (1)
{
printf("1.Insert \
2.Delete \
3.Search \
4.Display \
5.Exit ");
printf("Enter your choice:");
scanf("%d", &choice);
switch(choice)
table 'hashTable' is allocated using the
calloc function using the NumOfElements
and the size of the structure 'hash'.
The above printf() statements are used to
provide the choices to the user and the
choice given as input is stored in the
variable 'choice'.The while() loop is used to
executed multiple times based on the input
given by the user.
Based on the choice given as input to the
program, the corresponding switch case
statement is executed.
case 1:
printf("Enter the key value: ");
scanf("%d", &key);
printf("Name: ");
scanf("%s",name);
printf("Mark: ");
scanf("%d", &mark);
If the user gives '1' as the input, the
statements corresponding to case 1 are
executed. Here, the values which are to be
inserted into the node in the list are
obtained as input in the variables key,
name and mark from the user.
InsertToTable(key, name, mark);
break;
The InsertToTable() is used to insert a new
node into a list based on the value
generated from the hash function. It takes
the key value, name and mark of a single
student as the input arguments.
case 2:
printf("Enter the key to perform deletion:");
scanf("%d", &key);
If the user gives '2' as the input, the
statements corresponding to case 2 are
executed.
This
contains
the
DeleteFromTable() function which deletes a
particular node in the list based on the key
value.
DeleteFromTable(key);
The DeleteFromTable() is used to delete a
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
break;
case 3:
printf("Enter the key to search:");
scanf("%d", &key);
node from the given list by searching for it
using the key value. It takes the key value
as the input argument.
If the user gives '3' as the input, the
statements corresponding to case 3 are
executed. This contains the function
SearchElement() which searches for a
particular node in the list based on the key
value.
SearchElement(key);
break;
The SearchElement() is used to search for a
node from the given list by using the key
value. It takes the key value as the input
argument.
case 4:
DisplayTable();
break;
If the user gives '4' as the input, the
statements corresponding to case 4 are
executed. This contains the function
DisplayTable() which is used to display all
the entries present in the hash table.
case 5:
exit(0);
If the user gives '5' as the input, the
statements corresponding to case 5 are
executed. This contains the function exit(0)
which exits the running program.
default:
printf("Enter a valid option\n");
break;
If the user gives an input which cannot be
matched with any of the given cases, then
the default statement is printed.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Function : CreateNode
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
newnode->key = key;
newnode->Mark = mark;
strcpy(newnode->name, name);
newnode->next = NULL;
return newnode;
A pointer to the structure node is created
as newnode which stores the details for a
single student.
The memory for the stack pointer newnode
is allocated using the malloc function by
using the size of the structure to calculate
the bytes required for one node.
Once the memory for the single node is
allocated, the details of the student such
as the StudentID, mark and name are
stored in it.
Finally, the newly created node is returned
back to the calling function.
Function : InsertToTable
int hashIndex = key % ElementCount;
struct node *newnode = CreateNode(key, name, mark);
if (!hashTable[hashIndex].head)
The variable hashIndex contains the value
which determines where the new node to
be inserted is stored in the hash table. It is
calculated using the key and the
ElementCount.
Once the new node is created using the
CreateNode() function, its address is
returned and stored in a new pointer to the
structure node i.e. newnode.
The initial condition checked it whether the
given hash table is empty or it contains
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
some elements in it.
hashTable[hashIndex].head = newnode;
hashTable[hashIndex].count = 1;
return;
newnode->next = (hashTable[hashIndex].head);
hashTable[hashIndex].head = newnode;
hashTable[hashIndex].count++;
If the given hash table has no elements in
it, then we add the new node to the head
of the list and after the insertion,
increment the value of the count by 1.
If the hash table already contains entries,
then we update the table with the newest
entry by inserting the node into the table
using the hashIndex and updating the head
of the list to point to the new node.
We also update the count which indicates
the number of nodes present in a particular
list in the hash table.
Function : DeleteFromTable
The variable hashIndex contains the value
which determines the location of the list
where the node to be deleted is present.
int hashIndex = key % ElementCount;
int flag = 0;
struct node *temp, *myNode;
myNode = hashTable[hashIndex].head;
The flag variable is to indicate whether
given value to be delete is present in the
list.
The structure pointer temp is used to store
the value which is to be deleted from the
list.
The address of the list head which contains
the element to be deleted is stored in the
structure pointer myNode.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
if (!myNode)
If the value stored in myNode is NULL, then
the given value is not present in the list.
printf("Given input is not present in hash Table\n");
return;
temp = myNode;
while (myNode != NULL)
if (myNode->key == key)
If it is not NULL, we store the address of the
head in a temporary variable.
Using the while loop, we traverse through
the list of elements till the end of the list is
reached or until the element is found. If the
key value matches the value present in the
list, then the element is present in the list.
flag = 1;
if (myNode == hashTable[hashIndex].head)
Once the value is found, we set the flag
value as 1 to indicate it and then check
whether the given element found is
present in the head of the list.
hashTable[hashIndex].head = myNode->next;
If the element to be deleted is present in
the head of the list, we adjust the links to
make the next element in the list as the
new head.
else
temp->next = myNode->next;
If the element is not present as the head of
the list, then we adjust the links to make
the previous element point to the next
element so that we can delete that node.
hashTable[hashIndex].count--;
free(myNode);
break;
Once the links have been adjusted, we
decrement the count to indicate the
element's removal and then deallocate the
memory resources for that node using the
free() function.
temp = myNode;
myNode = myNode->next;
If the given value is not present, then we
traverse to the next node in the list as
myNode->next.
if (flag)
If the value of the flag is set to 1, we can
identify that the node to be deleted has
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
printf("Data is deleted from Hash
Table\n");
else
been found in the list, else it is not present
in the table.
printf("Given data is not present in Hash
table\n");
return;
Function : SearchElement
int hashIndex = key % ElementCount;
int flag = 0;
struct node *myNode;
The variable hashIndex contains the value
which determines the location of the list
where the node to be deleted is present.
The flag variable is to indicate whether
given value to be delete is present in the
list.
The structure pointer myNode is used to
store the value which is to be searched
from the list.
myNode = hashTable[hashIndex].head;
if (!myNode)
The address of the list head which contains
the element to be searched is stored in the
structure pointer myNode.
If the value stored in myNode is NULL, then
the given value is not present in the list.
printf("Element is not found in the table \n");
return;
while (myNode != NULL)
{
if (myNode->key == key)
printf("StudentID : %d\n", myNode->key);
printf("Name : %s\n", myNode->name);
Using the while loop, we traverse through
the list of elements till the end of the list is
reached or until the element is found. If the
key value matches the value present in the
list, then the element is present in the list.
If a match for the key value is found, then
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
printf("Mark
flag = 1;
break;
: %d\n", myNode->Mark);
myNode = myNode->next;
if (!flag)
printf("Element unavailable in hash
we print all the values associated with that
key as the output and set the flag value to
1.
If the value is not found, then we traverse
to the next node in the list to compare the
values as myNode->next.
If the value of the flag is not set to 1, we
can identify that the node to be searched is
not present in the table.
table\n");
Function : DisplayTable
struct node *myNode;
int i;
for (i = 0; i < ElementCount; i++)
if (hashTable[i].count == 0)
continue;
myNode = hashTable[i].head;
if (!myNode)
continue;
printf("\nData at index %d :\n", i);
printf("StudentID Name
Mark \n");
The structure pointer myNode, is a
temporary pointer which is used to store
the value obtained from the table and
displayed as the output.
A 'for' loop is used to traverse through the
contents of the hash table and the list of
nodes corresponding to each hash Index is
printed. If that particular index has a count
of zero, then it contains no elements.
The address of the each list head which
contains the list of nodes is stored in the
variable myNode and if it points to NULL,
then the list does not contain any
elements.
If the list head does not point to NULL, then
all the elements in that list are printed as
the output, till the end of the list.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
while (myNode != NULL)
printf("%-12d", myNode->key);
printf("%-15s", myNode->name);
printf("%d\n", myNode->Mark);
myNode = myNode->next;
Once the values present in a single node is
printed, then the next node is traversed as
myNode->next.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
© Copyright 2026 Paperzz