Experiment 8
Experiment 8
Program Statement:
8. Consider a super market scenario where sales manager wants to search for the
customer details using a customer-id. Customer information like (custid, custname,
&custphno) are stored as a structure, and custid will be used as hash key. Develop
and execute a program in C using suitable data structures to implement the
following operations:
a. Insertion of a new data entry.
b. Search for customer information using custid
c. Display the records. (Demonstrate collision and its handling using linear probing
method).
Theory:
Hash in C:
In C programming language, hashing is a technique that involves converting a large amount of data
into a fixed-size value or a smaller value known as a hash. The hash is generated through a hash
function, which maps the input data to an output hash. The resulting hash value can then be used to
efficiently search, retrieve, and compare data within large data sets.
Hashing is commonly used in data structures such as hash tables, which are arrays that store data in
a way that allows for quick insertion, deletion, and retrieval of data. The hash function used to
generate the hash value maps the key (or the data to be stored) to an index within the hash table.
This index is then used to store the data in the corresponding location within the array.
Hashing is useful for several reasons. Firstly, it can reduce the amount of memory required to store
large data sets by converting the data into a smaller value. Secondly, it can improve the performance
of algorithms by allowing for faster searching and retrieval of data. Finally, it can help to ensure data
integrity by detecting duplicate data and preventing collisions (when two different keys map to the
same index).
The process of hashing involves three main steps: creating the hash function, generating the hash
value, and storing the data in the hash table.
Creating the hash function involves designing an algorithm that maps the input data to a fixed-size
value. This algorithm should be designed to distribute the data evenly across the hash table to
reduce the likelihood of collisions. A good hash function should also be fast, simple, and
deterministic (i.e. it should always produce the same output for the same input).
Once the hash function is created, the next step is to generate the hash value for the data. This
involves passing the data through the hash function, which returns a fixed-size hash value. This value
is then used as an index within the hash table to store the data.
Storing the data in the hash table involves placing the data in the corresponding location within the
array. If a collision occurs (i.e. if two different keys map to the same index), the hash table may use a
technique called chaining to store both keys in the same index. In chaining, a linked list is created for
each index, and the keys are added to the linked list.
Hashing in C can be implemented using several different methods, including the division method,
multiplication method, and the folding method. The division method involves taking the remainder
of the key divided by the size of the hash table to determine the index. The multiplication method
involves multiplying the key by a constant value and then taking the fractional part of the result to
determine the index. The folding method involves breaking the key into several parts, adding them
together, and then using the result to determine the index.
get (key)
struct Customer {
int custid;
char custname[50];
char custphno[15];
};
Defines a structure named Customer to store customer information, including custid (customer ID),
custname (customer name), and custphno (customer phone number).
Global Variables:
Hash Function:
Implements a simple hash function that takes a customer ID as input and returns the hash value
based on modulo operation with the table size.
Insertion Function:
// ...
Defines a function insertCustomer to insert a new customer record into the hash table using linear
probing to handle collisions.
Search Function:
// ...
Implements a function searchCustomer to find and display customer information based on the
provided customer ID, using linear probing to handle collisions.
Display Function:
void displayRecords() {
// ...
Defines a function displayRecords to print all non-empty records in the hash table.
Main Function:
int main() {
// ...
Prompts the user to enter a customer ID for searching and then calls the searchCustomer function.
Output: