0% found this document useful (0 votes)
13 views15 pages

Updated PDAssignment6

This document outlines the assignment details for a Program Design Laboratory course at the National Institute of Technology Calicut, focusing on hash table implementations using various collision resolution techniques. It includes submission guidelines, naming conventions, and specific problems related to open addressing, quadratic probing, double hashing, and separate chaining. The assignment emphasizes academic integrity and requires students to implement their solutions in C language.

Uploaded by

Man Killer
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)
13 views15 pages

Updated PDAssignment6

This document outlines the assignment details for a Program Design Laboratory course at the National Institute of Technology Calicut, focusing on hash table implementations using various collision resolution techniques. It includes submission guidelines, naming conventions, and specific problems related to open addressing, quadratic probing, double hashing, and separate chaining. The assignment emphasizes academic integrity and requires students to implement their solutions in C language.

Uploaded by

Man Killer
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/ 15

National Institute of Technology Calicut

Department of Computer Science and Engineering


Second Semester B.Tech (CSE)
CS1092E Program Design Laboratory
Assignment #6 [Focus on 1 to 3 problems]
Submission deadline (on or before): 06/04/2025 (Sunday), 11:59 PM
Naming Conventions for Submission
• Submit a single ZIP (.zip) file (do not submit in any other archived formats like .rar, .tar,
.gz). The name of this file must be

ASSG<Number><ROLLNO><FIRST-NAME>.zip
(Example: ASSG6_ B240123CS_NEEMA.zip). DO NOT add any other files (like temporary f
iles, input files, etc.) except your source code, into the zip archive.
• The source codes of each question must be named as
ASSG<Number><ROLLNO><PROGRAM-NUMBER>.c
(For example: ASSG6_ B240123CS_1.c).
Standard of Conduct
• Violation of academic integrity will be severely penalized. Each student is expected to
adhere to high standards of ethical conduct, especially those related to cheating and
plagiarism. Any submitted work MUST BE an individual effort. Any academic dishonesty will
result in zero marks in the corresponding exam or evaluation and will be reported to the
department council for record keeping and for permission to assign F grade in the course.
The department policy on academic integrity can be found at:
http://cse.nitc.ac.in/sites/default/files/Academic-Integrity new.pdf.
General Instructions
• Programs should be written in C language.
• Check your programs with sufficiently large values of inputs within the range as specified
in the question.
• Global and/or static variables should not be used in your program.

S. PD Assignment-6 (Open Hashing and Closed Hashing)


No Topic and Problem

1 Linear Probing using arrays in open addressing


Problem:
You are given an empty hash table of size N and a list of K keys to insert into the table. You must
insert the keys into the table using the following hash function:
H(x)=x mod N. If a collision occurs (i.e., the slot is already occupied), use linear probing by checking
the next available slot in a cyclic manner.
Once you have inserted all the elements, you will receive a set of Q search queries. For each query, check
and return its index, whether the key exists in the table. Otherwise, return -1 if it is not present.
Input Specification
●​ The first line contains two integers, N (size of the hash table) and K (number of keys to insert).
●​ The second line contains K space-separated integers representing the keys to insert.
●​ The third line contains an integer Q (number of queries).
●​ The next Q lines contain a single integer X each, representing a search query.
Output Specification
●​ Print the final state of the hash table in a single line, where empty slots are represented by -1.
●​ Print the index where the key is located for each query, or -1 if it is not.
Constraints
●​ 1 ≤ N ≤ 100
●​ 1≤K≤N
●​ 1 ≤ Q ≤ 50
●​ 1 ≤ Key values ≤ 10^5
Sample Input 1:
75
50 700 76 85 92
3
700
92
100
[Not Part of program output just for understanding]
Processing:
●​ 50 % 7 = 1 → Insert at index 1
●​ 700 % 7 = 0 → Insert at index 0
●​ 76 % 7 = 6 → Insert at index 6
●​ 85 % 7 = 1 (Collision) → Try 2 → Insert at index 2
●​ 92 % 7 = 1 (Collision) → Try 2 (Collision) → Try 3 → Insert at index 3
Final Hash Table:
700 50 85 92 -1 -1 76

Sample Output 1:
700 50 85 92 -1 -1 76
0
3
-1

Sample Input 2:
54
21 22 23 24
2
23
25
Processing:
●​ 21 % 5 = 1 → Insert at index 1
●​ 22 % 5 = 2 → Insert at index 2
●​ 23 % 5 = 3 → Insert at index 3
●​ 24 % 5 = 4 → Insert at index 4
Final Hash Table:
-1 21 22 23 24

Sample Output 2:
-1 21 22 23 24
3
-1

Sample Input 3:
10 6
12 22 32 42 52 62
3
32
52
100
Processing:
●​ 12 % 10 = 2 → Insert at index 2
●​ 22 % 10 = 2 (Collision) → Try 3 → Insert at index 3
●​ 32 % 10 = 2 (Collision) → Try 3 (Collision) → Try 4 → Insert at index 4
●​ 42 % 10 = 2 (Collision) → Try 3 (Collision) → Try 4 (Collision) → Try 5 → Insert at index 5
●​ 52 % 10 = 2 (Collision) → Try 3 (Collision) → Try 4 (Collision) → Try 5 (Collision) → Try 6 →
Insert at index 6
●​ 62 % 10 = 2 (Collision) → Try 3 (Collision) → Try 4 (Collision) → Try 5 (Collision) → Try 6
(Collision) → Try 7 → Insert at index 7
Final Hash Table:
-1 -1 12 22 32 42 52 62 -1 -1

Sample Output 3:
-1 -1 12 22 32 42 52 62 -1 -1
4
6
-1

Sample Input 4:
85
15 25 35 45 55
3
25
55
75
Processing:
●​ 15 % 8 = 7 → Insert at index 7
●​ 25 % 8 = 1 → Insert at index 1
●​ 35 % 8 = 3 → Insert at index 3
●​ 45 % 8 = 5 → Insert at index 5
●​ 55 % 8 = 7 (Collision) → Try 0 → Insert at index 0
Final Hash Table:
55 25 -1 35 -1 45 -1 15

Sample Output 4:
55 25 -1 35 -1 45 -1 15
1
0
-1

Sample Input 5:
63
10 20 30
3
10
30
40
Processing:
●​ 10 % 6 = 4 → Insert at index 4
●​ 20 % 6 = 2 → Insert at index 2
●​ 30 % 6 = 0 → Insert at index 0
Final Hash Table:
30 -1 20 -1 10 -1

Sample Output 5:
30 -1 20 -1 10 -1
4
0
-1

2. Quadratic Probing using arrays in open addressing


Problem:
Implement a hash table using open addressing with quadratic probing for collision resolution. Your
program should support the following operations:
●​ Insert: Insert a given key into the hash table.
●​ Search: Check if a given key exists in the hash table or not.
●​ Delete: Remove a given key from the hash table.
The hash function is defined as:​
hash(key)=key mod table_size
Quadratic probing resolves collisions using the formula:​
index=(hash(key)+i^2) mod table_size​
where i starts from 0 to table_size-1 and increments until an empty slot is found (for insertion) or the key
is found in case of search and delete.

Input Specification
1.​ The first line contains an integer N (number of operations).
2.​ The next N lines contain operations of the following format:
○​ "INSERT X": Insert key X into the hash table.
○​ "SEARCH X": Search for key X and print "1" or "-1".
○​ "DELETE X": Delete key X from the hash table.
3.​ The hash table size is predefined and must be a prime number.

Output Specification
●​ For "SEARCH X" operation, print"1" if X is present in the table, otherwise "-1" if X is not in
the table.
●​ For "DELETE X", if X exists in the hash table, it should be removed. Otherwise, do nothing.

Sample Input 1:
6
INSERT 10
INSERT 20
SEARCH 10
SEARCH 15
DELETE 10
SEARCH 10

Sample Output 1:

1
-1
-1

Sample Input 2:

5
INSERT 5
INSERT 15
SEARCH 5
DELETE 5
SEARCH 5

Sample Output 2:

1
-1

3. Double hashing using arrays in open addressing


Problem Statement:
You are tasked with developing a hash table implementation that uses double hashing for collision
resolution. The program should allow users to insert, search, delete, and display integer keys stored in a
fixed-size hash table using open addressing. Double hashing involves using a primary hash function
[H(k,i)=(h(k)+i h’(k)) mod N, where h(k)= k mod N and h’(k) = (M-k mod M)] to determine the initial
index for an element and a secondary hash function to determine the step size for resolving collisions.
The hash table should handle collisions efficiently by probing new positions based on the secondary hash
function. Implement the program in C, ensuring that insertions, deletions, and searches work correctly
while maintaining the integrity of the hash table. Assume a table size of N (prime number) and a
secondary hash value that takes a size (M) that is coprime with N. Design appropriate hash functions to
minimize collisions and improve performance.
Note: If gcd(N, M)=1, then we can say that they are coprimes.
Input Specification:
●​ The first line read the N M that indicates the size of the primary and secondary tables.
●​ Continue the execution if M is coprime to N by checking gcd(N, M)=1; otherwise, terminate the
program.
●​ The program reads the commands like insert, search, and delete. If the command is
○​ Insert <key>: for insert the <key> at the specified location based on Double hashing
○​ Delete <key>: To delete the key from the hash table and prints the location if found
otherwise, print “NOT FOUND”
○​ Search <key>: Search the <key> available in hash table prints its location if found;
otherwise, print -1.
○​ Exit command terminates the execution of the program.

Output Specification:
●​ Delete <key>: deletes the key from the hash table and prints the location if found otherwise, print
“NOT FOUND”
●​ Search <key>: Search the given <key> in hash table prints its location if found; otherwise, print
-1.
●​ Display: Prints the current state of the hash table with E and D, indicating that empty slots and
deleted slots where there is no key in the slots.
●​ Exit: command terminates the execution of the program.

Sample Input 1:
10 7
Insert 25
Insert 35
Insert 15
Insert 45
Insert 55
Display
Search 35
Delete 35
Search 35
Display
Exit

Sample Output 1:

Index: 0 1 2 3 4 5 6 7 8 9
Keys: E 15 35 E E 25 55 E E 45
2
2
-1
Index: 0 1 2 3 4 5 6 7 8 9
Keys: E 15 D E E 25 55 E E 45

Sample Input 2:
10 8
Sample Output 2:
gcd(10, 8) not equal to 1

4. Separate chaining using structures


Problem:
You are tasked with designing a Library Book Management System using a hash table that implements
separate chaining for collision. Each book in the library has a unique identification number (ID), along
with a title, author, and publication year. The system should be able to insert, search, and delete books
efficiently. The hash table uses a fixed number of buckets and resolves collisions using a linked list for
each bucket.
Input Specification:
●​ The first line of input contains a single integer ‘n’ representing the number of buckets in the hash
table.
●​ The following lines represent operations to be performed on the hash table. Each operation will be
one of the following:
○​ INSERT <id> "<title>" " <author>" <year of publication>: Add a book to the hash table.
○​ SEARCH <id>: Search for a book by its ID.
○​ DELETE <id>: Remove a book from the system by its ID.
Output Specification:
●​ For INSERT: No output required.
●​ For SEARCH:
○​ If the book exists, output the book's details in the format: "title, author, year". Otherwise,
print -1.
●​ For DELETE:
○​ If the book exists and is deleted, output 1. Otherwise, print ‘-1’.

Sample Input 1:
5
INSERT 123 "Data Structures in Practice" "John Doe" 2020
INSERT 125 "Algorithms Unlocked" "Jane Smith" 2018
SEARCH 123
SEARCH 999
DELETE 125
DELETE 125
SEARCH 125

Sample Output 1:
Data Structures in Practice, John Doe, 2020
-1
1
-1
-1

5. Menu driven program (Open addressing: Linear, Quadratic, and Double hashing)
Problem:
​ ​ ​ ​
A college has a Library Management System that uses a hash table to store and manage book records.
Each book record consists of a unique ISBN (13-digit number) and the book title (string). The system
should support three collision resolution techniques: Linear Probing, Quadratic Probing, and Double
Hashing. The user can select the collision resolution method at the start of the program. The system
should allow the user to add books, search for books, delete books, and display the current state of the
hash table.
Input Specification:
●​ The first line choose the method for handling collision.
●​ Thesecond line contains the size of the hash table with respect to the method chosen above.
The program accepts a command (a string) which is the operation to be performed (in capital letters),
followed by optional arguments depending on the command. The commands and their formats are as
follows:
1.​ Add Book:​

○​ ADD ​ <ISBN> “<title>”: Adds a book with the given <ISBN> (13-digit unique integer)
and “<title>” (string of 30 characters) into the hash table.​
​ ​
■​ Example: ADD 9783161484100 "The Great Gatsby"​
​ ​ ​ ​
2.​ Search Book:​

○​ SEARCH <ISBN>: Searches for the book with the given <ISBN>.​
​ ​
■​ Example: SEARCH ​ 9783161484100​
​ ​ ​
3.​ Delete Book:​

○​ DELETE <ISBN>: Deletes the book with the given <ISBN> from the hash table.​
​ ​
■​ Example: DELETE 9783161484100​
​ ​
4.​ Display Hash ​Table:​

○​ DISPLAY: Displays the current state of the hash table, showing the index, ​ISBN, and title
for each occupied slot.​
​ ​
■​ Example: DISPLAY
5.​ Exit:​

○​ EXIT: ​Terminates the program.​
​ ​
■​ Example: EXIT
Each command should be entered on a new line.

Output Specification:
The program will output results as follows:
1.​ Add Books ​
○​ Print output: "1" for successful insertion. Otherwise, output -1. ​ ​
2.​ Search Book:​
○​ Print ISBN, title if the book is found. Otherwise, print -1.
3.​ Delete Book:​
○​ Print 1​if the book is successfully deleted. Otherwise, print -1. ​ ​ ​
4.​ Display Hash ​Table:​
○​ Output: Displays the current state of the hash table, showing the index, ISBN, and title for
each occupied slot.
5.​ Exit: Terminate the Program.​
​ ​
SAMPLE INPUT 1:

Choose method (1: Linear, 2: Quadratic, 3: Double Hashing): 1


3
ADD 9783161484100 "The Great Gatsby"
ADD 9780451524935 "1984"
SEARCH 9783161484100
DISPLAY
EXIT

SAMPLE OUTPUT 1:

1
1
9783161484100, The Great Gatsby
Index 0: (None, None)
Index 1: (9783161484100, The Great Gatsby)
Index 2: (9780451524935, 1984)

SAMPLE INPUT 2:

Choose method (1: Linear, 2: Quadratic, 3: Double Hashing): 2


3
ADD 9783161484100 "The Great Gatsby"
ADD 9780451524935 "1984"
DELETE 9783161484100
SEARCH 9783161484100
DISPLAY
EXIT
SAMPLE OUTPUT 2:

1
1
1
-1
Index 0: (None, None)
Index 1: (None, None)
Index 2: (9780451524935, 1984)

SAMPLE INPUT 3:

Choose method (1: Linear, 2: Quadratic, 3: Double Hashing): 3


3
ADD 9783161484100 "The Great Gatsby"
ADD 9780451524935 "1984"
ADD 9780141439518 "Pride and Prejudice"
SEARCH 9780141439518
DISPLAY
EXIT

SAMPLE OUTPUT 3:

1
1
1
9780141439518, Pride and Prejudice
Index 0: (9780141439518, Pride and Prejudice)
Index 1: (9783161484100, The Great Gatsby)
Index 2: (9780451524935, 1984)

6. Programs on separate chaining technique


Problem:
You have been tasked with developing a Student Record Management System for your college. The
system must store each student’s record, which includes:
●​ A unique student ID (an integer, 100 <= ID <= 350)
●​ The student’s name (a string not more than 30 characters)
Since different student IDs might hash to the same bucket, you must use the separate chaining
technique to handle collisions. This means that each bucket in your hash table should be implemented as
a Doubly Linked List (DLL) where each node stores one student record.
●​ Hash Function: Use a simple modulo operation to determine the bucket for each student record.
For example, use ID % numBuckets as the hash function.
●​ Collision Handling with Chaining: If two or more student IDs hash to the same bucket, store
them in a DLL associated with that bucket (the most recent one at the beginning of the DLL). This
approach will allow multiple student records to coexist in the same bucket.
●​ Memory Management:
○​ Dynamically allocate memory for the nodes (students’ records) in each DLL.
○​ Ensure that all allocated memory is properly freed after deleting the node from the DLL.

Input Specifications:
●​ First line: Prompt the user to enter the number of buckets to create in the hash table (for example,
5, 10, etc.).
●​ Operations are listed below:
○​ Insert <ID> <NAME>: A unique student ID (e.g., 101, 202, etc.) and a string
representing the student’s name (e.g., "JohnDoe").
○​ Search <ID>: Search for the student's ID in the hash table or not.
○​ Delete <ID>: Deleting the student’s record if the ID is found.
○​ Display: Displays the current status of the records in the Hash Table
○​ Exit : Terminating the program.

Output Specifications:
●​ Insert: No need for display after insert operation.
●​ Search: Print ID, Name if the record is found in the respective DLL of the bucket. Otherwise,
print -1.
●​ Delete: Print the ID, Name of the record is it is deleted. Otherwise, print -1.
●​ Display: List all buckets in the hash table. Each bucket should display all the (ID, Name) pairs
stored in that bucket. Print E if a bucket is empty.

Sample Input 1:
Enter the number of buckets:
5
Insert 101 JohnDoe
Insert 104 Kendy
Insert 106 Bob
Insert 221 Alice
Insert 112 Eve
Display
Search 104
Delete 104
Search 104
Display
Exit

Sample Output 1:
Index 0: E
Index 1: 221 Alice 106 Bob 101 JohnDoe
Index 2: 112 Eve
Index 3: E
Index 4: 104 Kendy
104 Kendy
-1
Index 0: E
Index 1: 221 Alice 106 Bob 101 JohnDoe
Index 2: 112 Eve
Index 3: E
Index 4: E

7. Menu driven program on different techniques on separate chaining methods


Problem:
You have been hired to develop a vehicle registration tracking system for a local motor vehicle
department. They need to store each vehicle’s license plate number (used as a unique numeric key, e.g.,
1234, 9876, etc.) and the owner’s name. To manage potential collisions (since different license plates
might hash to the same bucket), you are required to implement separate chaining (Singly linked lists in
each bucket).

menu-driven C program should support the following operations:


1.​ Insert a new vehicle registration.
2.​ Search for a registration by the license plate number.
3.​ Delete a registration by the license plate number.
4.​ Display the entire hash table.
5.​ Exit the program.

Input Specification:
○​ At the start, prompt the user for the number of buckets in the hash table (e.g., 5, 10, etc.).
○​ Show a menu with five options:
■​ Insert
■​ Search
■​ Delete
■​ Display
■​ Exit
○​ For Insert, the user inputs:
■​ A unique integer representing the license plate number (e.g., 1234).
■​ A string representing the owner’s name (e.g., "Alice").
○​ For Search or Delete, the user inputs the license plate number they want to look up or
remove.
Output Specifications:
○​ Print a success message when an insert is successful, or an error message if the license
plate number already exists in the system.
○​ Print an appropriate message when searching:
■​ “Registration found: (plate_number, owner_name)” if it exists. “No registration
found for plate_number” if it does not exist.
○​ When deleting a registration:
■​ Print a success message if the record is found and removed, or “No registration
found for plate_number” if not found.
○​ The Display operation should print the contents of all buckets. Each bucket can list all
(plate_number, owner_name) pairs, reflecting the use of separate chaining.
○​ The program terminates on choosing Exit, optionally printing a closing message.

Sample Testcase 1:

Enter the number of buckets:


5
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your choices:
1
1234
Alice
1
5678
Bob
1
1110
Charlie
4
2
1110
3
5678
2
5678
4
5

Sample Output 1:

(Plate: 1234, Owner: Alice) inserted.


(Plate: 5678, Owner: Bob) inserted.
(Plate: 1110, Owner: Charlie) inserted.

Hash Table Contents:


Bucket 0:
Bucket 1: (1234, Alice)
Bucket 2: (5678, Bob)
Bucket 3:
Bucket 4: (1110, Charlie)

Registration found: (1110, Charlie)


Registration with Plate 5678 deleted.
No registration found for Plate 5678
Hash Table Contents:
Bucket 0:
Bucket 1: (1234, Alice)
Bucket 2:
Bucket 3:
Bucket 4: (1110, Charlie)

You might also like