Implementation of Double Hashing Aim:: EX NO:7.3 Date
Implementation of Double Hashing Aim:: EX NO:7.3 Date
EX NO:7.3
AIM:
To write a C program for implementation of double hashing.
PSEUDOCODE:
define TABLE_SIZE = 10
define PRIME = 7
declare hashTable[TABLE_SIZE]
function initializeTable()
for i = 0 to TABLE_SIZE - 1
hashTable[i] = -1
function hashFunction1(key)
return key % TABLE_SIZE
function hashFunction2(key)
return PRIME - (key % PRIME)
function insert(key)
index = hashFunction1(key)
stepSize = hashFunction2(key)
i=0
while hashTable[(index + i * stepSize) % TABLE_SIZE] != -1
i++
if i == TABLE_SIZE
print "Hash table is full, cannot insert", key
return
newIndex = (index + i * stepSize) % TABLE_SIZE
hashTable[newIndex] = key
print "Inserted", key, "at index", newIndex
function search(key)
index = hashFunction1(key)
stepSize = hashFunction2(key)
i=0
while hashTable[(index + i * stepSize) % TABLE_SIZE] != -1
newIndex = (index + i * stepSize) % TABLE_SIZE
if hashTable[newIndex] == key
return newIndex // Key found
i++
if i == TABLE_SIZE
break
return -1 // Key not found
function displayTable()
print "Hash Table:"
for i = 0 to TABLE_SIZE - 1
if hashTable[i] == -1
print "Index", i, ": Empty"
else
print "Index", i, ":", hashTable[i]
SOURCE CODE:
#include <stdio.h>
#define TABLE_SIZE 10
#define PRIME 7
int hashTable[TABLE_SIZE];
ROLL NO : 717823P152
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES
void initializeTable() {
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable[i] = -1;
}
}
if (hashTable[newIndex] == key) {
ROLL NO: 717823P152
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES
i++;
if (i == TABLE_SIZE) {
break;
void displayTable() {
printf("Hash Table:\n");
if (hashTable[i] == -1) {
printf("Index %d: Empty\n", i);
} else {
}
}
int main() {
initializeTable();
insert(23);
insert(43);
insert(13);
insert(27);
insert(33);
displayTable();
return 0;
OUTPUT:
RESULT:
Thus the program for implementation of double hashing is executed
successfully and the output is verified.
EX NO:7.4
AIM:
To write a C program for Implementation of Seperate Chaining.
PSEUDOCODE:
define TABLE_SIZE = 10
declare hashTable[TABLE_SIZE]
structure Node
key: integer
next: Node pointer
function createNode(key)
newNode = allocate memory for Node
newNode.key = key
newNode.next = NULL
return newNode
function hashFunction(key)
return key % TABLE_SIZE
function insert(key)
index = hashFunction(key)
newNode = createNode(key)
if hashTable[index] == NULL
hashTable[index] = newNode
else
newNode.next = hashTable[index]
hashTable[index] = newNode
print "Inserted", key, "at index", index
function search(key)
index = hashFunction(key)
temp = hashTable[index]
while temp != NULL
if temp.key == key
return index // Key found
temp = temp.next
return -1 // Key not found
function displayTable()
print "Hash Table:"
for i = 0 to TABLE_SIZE - 1
print "Index", i, ":"
temp = hashTable[i]
while temp != NULL
print " ->", temp.key
temp = temp.next
print " -> NULL"
function freeTable()
for i = 0 to TABLE_SIZE - 1
temp = hashTable[i]
while temp != NULL
toFree = temp
temp = temp.next
free memory for toFree
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10
struct Node {
int key;
struct Node* next;
};
newNode->next = NULL;
return newNode;
}
if (hashTable[index] == NULL) {
hashTable[index] = newNode;
} else {
newNode->next = hashTable[index];
hashTable[index] = newNode;
}
if (temp->key == key) {
return index; // Key found
}
temp = temp->next;
void displayTable() {
printf("Hash Table:\n");
temp = temp->next;
}
void freeTable() {
}
int main() {
insert(23);
insert(43);
insert(13);
insert(27);
insert(33);
displayTable();
if (searchIndex != -1) {
printf("Key %d found at index %d\n", key, searchIndex);
} else {
freeTable();
return 0;
}
OUTPUT:
ROLL NO : 717823P152
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES
RESULT:
Thus the program for Implementation of Separatee Chaining is executed
successfully and the output is verified.
ROLL NO :717823P152