0% found this document useful (0 votes)
11 views9 pages

Implementation of Double Hashing Aim:: EX NO:7.3 Date

Uploaded by

rparthipan2686
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)
11 views9 pages

Implementation of Double Hashing Aim:: EX NO:7.3 Date

Uploaded by

rparthipan2686
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/ 9

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

EX NO:7.3

DATE: IMPLEMENTATION OF DOUBLE HASHING

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;

}
}

int hashFunction1(int key) {

return key % TABLE_SIZE;


}

int hashFunction2(int key) {


return PRIME - (key % PRIME);

void insert(int key) {

int index = hashFunction1(key);


int stepSize = hashFunction2(key);
int i = 0;

while (hashTable[(index + i * stepSize) % TABLE_SIZE] != -1) {


i++;
if (i == TABLE_SIZE) {

printf("Hash table is full, cannot insert %d\n", key);


return;
}

int newIndex = (index + i * stepSize) % TABLE_SIZE;


hashTable[newIndex] = key;
printf("Inserted %d at index %d\n", key, newIndex);

int search(int key) {

int index = hashFunction1(key);


int stepSize = hashFunction2(key);
int i = 0;

while (hashTable[(index + i * stepSize) % TABLE_SIZE] != -1) {


int newIndex = (index + i * stepSize) % TABLE_SIZE;

if (hashTable[newIndex] == key) {
ROLL NO: 717823P152
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

return newIndex; // Key found

i++;
if (i == TABLE_SIZE) {

break;

return -1; // Key not found

void displayTable() {

printf("Hash Table:\n");

for (int i = 0; i < TABLE_SIZE; i++) {

if (hashTable[i] == -1) {
printf("Index %d: Empty\n", i);

} else {

printf("Index %d: %d\n", i, hashTable[i]);

}
}

int main() {

initializeTable();

insert(23);

insert(43);
insert(13);

insert(27);

insert(33);

displayTable();

int key = 27;


int searchIndex = search(key);
if (searchIndex != -1) {
printf("Key %d found at index %d\n", key, searchIndex);
} else {

ROLL NO: 717823P152


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

printf("Key %d not found in the hash table\n", key);

return 0;

OUTPUT:

RESULT:
Thus the program for implementation of double hashing is executed
successfully and the output is verified.

ROLL NO: 717823P152


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

EX NO:7.4

DATE: IMPLEMENTATION OF SEPERATE CHAINING

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

ROLL NO: 717823P152


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>

#define TABLE_SIZE 10

struct Node {
int key;
struct Node* next;

};

struct Node* hashTable[TABLE_SIZE];


struct Node* createNode(int key) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->key = key;

newNode->next = NULL;
return newNode;
}

int hashFunction(int key) {


return key % TABLE_SIZE;
}

void insert(int key) {

int index = hashFunction(key);


struct Node* newNode = createNode(key);

if (hashTable[index] == NULL) {

hashTable[index] = newNode;
} else {

newNode->next = hashTable[index];

hashTable[index] = newNode;
}

printf("Inserted %d at index %d\n", key, index);


}
int search(int key) {

int index = hashFunction(key);

struct Node* temp = hashTable[index];

while (temp != NULL) {

ROLL NO: 717823P152


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

if (temp->key == key) {
return index; // Key found
}

temp = temp->next;

return -1; // Key not found

void displayTable() {
printf("Hash Table:\n");

for (int i = 0; i < TABLE_SIZE; i++) {


printf("Index %d:", i);

struct Node* temp = hashTable[i];


while (temp != NULL) {

printf(" -> %d", temp->key);

temp = temp->next;
}

printf(" -> NULL\n");

void freeTable() {

for (int i = 0; i < TABLE_SIZE; i++) {


struct Node* temp = hashTable[i];
while (temp != NULL) {
struct Node* toFree = temp;
temp = temp->next;
free(toFree);

}
int main() {

for (int i = 0; i < TABLE_SIZE; i++) { hashTable[i] = NULL;

ROLL NO: 717823P152


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 23CSR201 – DATA STRUCTURES

insert(23);
insert(43);

insert(13);

insert(27);

insert(33);
displayTable();

int key = 27;

int searchIndex = search(key);

if (searchIndex != -1) {
printf("Key %d found at index %d\n", key, searchIndex);
} else {

printf("Key %d not found in the hash table\n", key);

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy