Experiment 8 DS Student
Experiment 8 DS Student
AIM: Implementation of Hash Table with Collision Handling, File Operations, and Applications
of Hashing Technique.
8.1 Write a program to create hash table and handle the collision using linear probing.
8.2 Write a program to demonstrate the file primitives such as fopen, fclose, fprintf.
8.3 Identify widely used applications which use Hashing technique for implementation
of its important feature.
Date:
Theory:
Hashing
Hashing is a method used to map a large number of data items to a smaller table by utilizing a
hashing function. This technique transforms a range of key values into a range of indexes of an
array.There are two different forms of hashing.
1. Open hashing or external hashing: Open or external hashing, allows records to be stored
in unlimited space (could be a hard disk). It places no limitation on the size of the tables.
2. Close hashing or internal hashing: Closed or internal hashing, uses a fixed space for
storage and thus limits the size of hash table.
Hashing Functions
Characteristics of a Good Hash Function
• A good hash function avoids collisions.
• A good hash function tends to spread keys evenly in the array.
• A good hash function is easy to compute.
Data Structure (3130702)
File
In computing, a file is a group of records, where each record comprises one or more fields that
have the same sequence. Typically, each field has a predetermined length.
8.1 Write a program to create hash table and handle the collision using linear probing.
Program:
#include <stdio.h>
Page No
Data Structure (3130702)
int hashFunction(int key) {
return key % TABLE_SIZE;
}
hashTable[index] = key;
}
int main() {
int hashTable[TABLE_SIZE];
return 0;
}
Page No
Data Structure (3130702)
Output:
Hash table:
Index 0: ~
Index 1: ~
Index 2: ~
Index 3: 23
Index 4: 43
Index 5: 13
Index 6: 73
Index 7: 27
Index 8: ~
Index 9: ~
8.2 Write a program to demonstrate the file primitives such as fopen, fclose, fprintf.
Program:
#include <stdio.h>
int main() {
// Declare a file pointer
FILE *file;
// Open a file in write mode (if the file doesn't exist, it will be
created)
file = fopen("example.txt", "w");
return 0;
}
Output:
Page No
Data Structure (3130702)
example.txt
This is an example of using file primitives in C.
Writing numbers: 10, 20, and 30
Writing floating point numbers: 3.14, 9.81
8.3 Identify widely used applications which use Hashing technique for implementation of its
important feature.
Ans: Here are a few widely used applications that implement hashing techniques for their key features:
1. Database Indexing:
o Hashing is used in database systems to quickly retrieve data records. The keys (such as
student IDs, employee IDs, etc.) are hashed to produce an index that points to the location
of the actual data, enabling fast lookups.
2. Password Storage:
o Many systems store hashed versions of passwords instead of plain text. When a user logs
in, the system hashes the entered password and compares it to the stored hash. This
technique enhances security, as hashes are difficult to reverse-engineer.
3. Caching:
o Web browsers, proxy servers, and databases use hashing in caching mechanisms. URLs or
database queries are hashed to check if the result is stored in the cache, improving response
time and reducing load on servers.
4. Load Balancing:
o Hashing algorithms are used in load balancing to evenly distribute client requests across a
group of servers. The hash of an IP address or session ID determines which server will
handle the request, ensuring that no single server is overwhelmed.
5. Data Deduplication:
o Hashing is widely used in data deduplication systems to detect and eliminate duplicate
data. Each chunk of data is hashed, and the system compares hashes to identify duplicates,
reducing storage space.
6. Blockchain and Cryptocurrencies:
o Hashing plays a critical role in securing blockchain transactions. For example, Bitcoin uses
the SHA-256 hashing algorithm to ensure the integrity and security of blocks within the
blockchain.
7. Error Checking (Checksums):
o Hashing is used in algorithms such as MD5 and SHA-1 to verify data integrity during file
transfers or data storage. A file’s hash value is compared before and after transmission to
detect errors or corruption.
8. Student Roll Numbers:
o Schools use hashing to store student roll numbers and records. When they need to find a
student’s details, the roll number is hashed to quickly retrieve the information.
9. Spell Checkers:
o Hashing is used to store a list of correct words in a spell checker. When you type a word, it
hashes it to quickly check if it’s correct or a typo.
10. Finding Books in a Library:
o Imagine a library system where every book has a unique code. Instead of searching
through every book, the code is hashed to quickly find the book’s exact location.
Page No
Data Structure (3130702)
Conclusion:
Quiz:
(1) How does linear probing resolve collisions in a hash table? Explain with an example.
Page No
Data Structure (3130702)
(2) What are the key differences between fopen, fclose, and fprintf in file handling, and how are
they used in C programming?
(3) Mention two real-life applications where hashing is commonly used and explain why hashing
is important in those scenarios.?
Page No
Data Structure (3130702)
Suggested Reference:
Marks
Page No