0% found this document useful (0 votes)
15 views1 page

Exp 12 DS

Uploaded by

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

Exp 12 DS

Uploaded by

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

#include <stdio.

h>

#define TABLE_SIZE 7

int hashFunction(int key) {


return key % TABLE_SIZE;
}

int linearProbing(int hashTable[], int key) {


int index = hashFunction(key);
int i = 0;
while (hashTable[(index + i) % TABLE_SIZE] != -1 && i < TABLE_SIZE) {
if (hashTable[(index + i) % TABLE_SIZE] == key)
return (index + i) % TABLE_SIZE;
i++;
}
return -1;
}

int main() {
int hashTable[TABLE_SIZE], i, element, searchElement, index;

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


hashTable[i] = -1;
}

printf("Enter 7 elements to insert into the hash table:\n");


for (i = 0; i < TABLE_SIZE; i++) {
scanf("%d", &element);
int index = hashFunction(element);
int j = 0;
while (hashTable[(index + j) % TABLE_SIZE] != -1) {
j++;
}
hashTable[(index + j) % TABLE_SIZE] = element;
}

printf("Enter an element to search: ");


scanf("%d", &searchElement);

index = linearProbing(hashTable, searchElement);


if (index != -1)
printf("Element %d found at index %d\n", searchElement, index);
else
printf("Element %d not found in the hash table\n", searchElement);

return 0;
}

You might also like