Menu

Data Structures [ Lab Programs ]


C program to implement the following Hash Functions:
i) Division Method
ii) Multiplication Method
iii) Mid-square Method
iv) Folding Method

Hash Functions
#include <stdio.h>
#include <math.h>

// Division Method
int divisionMethod(int key, int tableSize) {
    return key % tableSize;
}

// Multiplication Method
int multiplicationMethod(int key, int tableSize) {
    float A = 0.618033;
    float temp = key * A;
    float fractional = temp - (int)temp;
    return (int)(tableSize * fractional);
}

// Mid-square Method
int midSquareMethod(int key, int tableSize) {
    long long square = (long long)key * key;

    int digits = 0;
    long long temp = square;
    while (temp > 0) {
        digits++;
        temp /= 10;
    }

    int extract = (int)(square / pow(10, digits/4)) % (int)pow(10, digits/2);

    return extract % tableSize;
}

// Folding Method
int foldingMethod(int key, int tableSize) {
    int sum = 0;

    while (key > 0) {
        sum += key % 100;  // split into 2-digit parts
        key /= 100;
    }
    return sum % tableSize;
}

// Driver code
int main() {
    int keys[5], tableSize, choice;
    static int table[15];

    printf("Enter 5 numbers:\n");
    for (int i = 0; i < 5; i++) {
        scanf("%d", &keys[i]);
    }

    printf("Enter hash table size (<15): ");
    scanf("%d", &tableSize);

    printf("\nChoose Hash Method:\n");
    printf("1. Division Method\n");
    printf("2. Multiplication Method\n");
    printf("3. Mid-square Method\n");
    printf("4. Folding Method\n");
    printf("Enter choice: ");
    scanf("%d", &choice);

    printf("\nHash values:\n");

    for (int i = 0; i < 5; i++) {
        int hash;

        switch (choice) {
            case 1:
                hash = divisionMethod(keys[i], tableSize);
                break;
            case 2:
                hash = multiplicationMethod(keys[i], tableSize);
                break;
            case 3:
                hash = midSquareMethod(keys[i], tableSize);
                break;
            case 4:
                hash = foldingMethod(keys[i], tableSize);
                break;
            default:
                printf("Invalid choice!\n");
                return 0;
        }
        table[hash]=keys[i];
    }
    
    for (int i=0;i<tableSize;i++)
     printf("%d\t",table[i]);

    return 0;
}

OUTPUT 1
Enter 5 numbers:
29 56 14 23 10
Enter hash table size (<15): 10

Choose Hash Method:
1. Division Method
2. Multiplication Method
3. Mid-square Method
4. Folding Method
Enter choice: 1

Hash values:
10	0	0	23	14	0	56	0	0	29 
OUTPUT 2
Enter 5 numbers:
5 6 4 9 8
Enter hash table size (<15): 10

Choose Hash Method:
1. Division Method
2. Multiplication Method
3. Mid-square Method
4. Folding Method
Enter choice: 2

Hash values:
5	0	0	0	4	9	0	6	0	8
OUTPUT 3
Enter 5 numbers:
12 15 23 19 26
Enter hash table size (<15): 10

Choose Hash Method:
1. Division Method
2. Multiplication Method
3. Mid-square Method
4. Folding Method
Enter choice: 3

Hash values:
0	19	0	0	12	15	26	0	0	23
OUTPUT 4
Enter 5 numbers:
1234 6543 1498 2413 7124
Enter hash table size (<15): 10

Choose Hash Method:
1. Division Method
2. Multiplication Method
3. Mid-square Method
4. Folding Method
Enter choice: 4

Hash values:
0	0	1498	0	0	7124	1234	2413	6543	0

Related Content :

1. Write a program that uses functions to perform the following operations on singly linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

2. Write a program that uses functions to perform the following operations on doubly linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

3. Write a program that uses functions to perform the following operations on circular linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

4. Write a program that implement Stack (its operations) using Array    View Solution

5. Write a program that implement Stack (its operations) using Linked List (Pointer)    View Solution

6. Write a program that implement Queue(its operations) using Array    View Solution

7. Write a program that implement Queue (its operations) using Linked List (Pointer)    View Solution

8. Write a program that implements Radix sorting methods to sort a given list of integers in ascending order    View Solution

9. Write a program that implements Heap sorting methods to sort a given list of integers in ascending order    View Solution

10. Write a program that implements Shell sorting methods to sort a given list of integers in ascending order    View Solution

11. Write a program that implements Tree sorting methods to sort a given list of integers in ascending order    View Solution

12. Write a program to implement the tree traversal methods using Recursive    View Solution

13. Write a program to implement the tree traversal methods using Non Recursive    View Solution

14. Write a program to implement Binary Search Tree (its operations)    View Solution

15. Write a program to implement AVL Tree (its operations)    View Solution

16. Write a program to implement Red - Black Tree (its operations)    View Solution

17. Write a program to implement the graph traversal methods (Breadth First Search)    View Solution

18.Write a program to implement the graph traversal methods (Depth First Search)    View Solution

19. Write a program to implement the following Hash Functions: i) Division Method, ii) Multiplication Method, iii) Mid-square Method, iv) Folding Method    View Solution