0% found this document useful (0 votes)
21 views8 pages

Experiment 8 DS Student

Uploaded by

my11cricle628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

Experiment 8 DS Student

Uploaded by

my11cricle628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment No – 8

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:

Competency and Practical Skills: Logic building and programming

Relevant CO: CO4, CO5

Objectives: (a) To understand the concepts of Hashing techniques


(b) To implement various file operations

Equipment/Instruments: Computer System Dev - C++ compiler

Safety and necessary Precautions:

✓ Operate computer system carefully and responsibly.


✓ Use required lab resources cautiously

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)

Different hashing functions


1. Division-Method
2. Folding Method
3. Algebraic Coding
4. Multiplicative Hashing
5. Digit Analysis
6. Mid-square Methods
7. Length Dependent Method

Collision Resolution Strategies


• Collision resolution is the main problem in hashing.
• If the element to be inserted is mapped to the same location, where an element is already
inserted then we have a collision and it must be resolved.
• There are several strategies for collision resolution. The most commonly used are :
1. Separate chaining - used with open hashing
2. Open addressing - used with closed hashing

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.

Different file organizations


1. Sequential files
2. Direct files
3. Index files
4. Indexed Sequential files
5. Relative files

Primitive Operations on a File


1. Creation
2. Insertion
3. Deletion
4. Updation
5. Reading
6. Searching

8.1 Write a program to create hash table and handle the collision using linear probing.

Program:

#include <stdio.h>

#define TABLE_SIZE 10 // Define the size of the hash table

// Function to calculate hash using modulo operation

Page No
Data Structure (3130702)
int hashFunction(int key) {
return key % TABLE_SIZE;
}

// Function to insert an element into the hash table


void insert(int hashTable[], int key) {
int index = hashFunction(key);

// Linear probing in case of collision


while (hashTable[index] != -1) {
index = (index + 1) % TABLE_SIZE; // Move to the next slot
}

hashTable[index] = key;
}

// Function to display the hash table


void display(int hashTable[]) {
int i;
for (i = 0; i < TABLE_SIZE; i++) {
if (hashTable[i] != -1)
printf("Index %d: %d\n", i, hashTable[i]);
else
printf("Index %d: ~\n", i); // Display empty slots as '~'
}
}

int main() {
int hashTable[TABLE_SIZE];

// Initialize hash table with -1 (indicating empty slots)


int i;
for (i = 0; i < TABLE_SIZE; i++) {
hashTable[i] = -1;
}

// Insert elements into the hash table


insert(hashTable, 23);
insert(hashTable, 43);
insert(hashTable, 13);
insert(hashTable, 27);
insert(hashTable, 73);

// Display the hash table


printf("Hash table:\n");
display(hashTable);

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");

// Check if the file opened successfully


if (file == NULL) {
printf("Error: Could not open file.\n");
return 1;
}

// Write formatted data to the file using fprintf


fprintf(file, "This is an example of using file primitives in C.\n");
fprintf(file, "Writing numbers: %d, %d, and %d\n", 10, 20, 30);
fprintf(file, "Writing floating point numbers: %.2f, %.2f\n", 3.14, 9.81);

// Close the file


fclose(file);

printf("Data successfully written to example.txt\n");

return 0;
}

Output:

Data successfully written to example.txt

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:

1. An Introduction to Data Structures with Applications. by Jean-Paul Tremblay & Paul G.


Sorenson Publisher-Tata McGraw Hill.
2. Data Structures using C & C++ -By Ten Baum Publisher – Prenctice-Hall International
3. Fundamentals of Computer Algorithms by Horowitz, Sahni,Galgotia Pub. 2001 ed.
4. http://www.geeksforgeeks.org/data-structures/
5. http://www.coursera.org/specializations/data-structures-algorithms

References used by the students:

Rubric-wise marks obtained:

Problem Coding Completeness


Logic
Understanding Standards and accuracy Q&A
Rubrics Building (2) Total
(2) (2) (2)
Avg. Good Avg. Good Avg. Good Avg. Good Avg. Good
(1) (2) (1) (2) (1) (2) (1) (2) (1) (2)

Marks

Page No

You might also like