Wednesday, 29 October 2014

Hashing prog........:---------



#include<stdio.h>
#include<stdbool.h>
#include<conio.h>
int hash_pos;
int array[40];
//declaring user defined functions
void insert();
void search();
int Hash(int );
int reHash(int );
void Delete();

void insert(){
    int data;
    int count = 0;
    printf("Enter the data to insert: ");
    scanf("%d", &data);
    hash_pos = Hash(data);
    if(hash_pos >= 40){
        hash_pos = 0;
    }
    while(array[hash_pos] != '*'){
        hash_pos = reHash(hash_pos);
        count++;
        if(count>=40){
            printf("Memory Full!!No space is avaible for storage");
            break;
        }
    }
    if(array[hash_pos] == '*'){
        array[hash_pos] = data;
    }
    printf("Data is stored at index %d\n", hash_pos);
}
int Hash(int key){
    return key%100;
}
int reHash(int key){
    return (key+1)%100;
}
void search(){
    int key,i;
    bool isFound = false;
    printf("Enter the key to search: ");
    scanf("%d", &key);
    for(i = 0; i < 40; i++){
        if(array[i] == key){
            isFound = true;
            break;
        }
    }
    if(isFound){
        printf("The key is found at index %d\n", i);
    }else{
        printf("No record found!!\n");
    }
}
void Delete(){
    int key,i;
    bool isFound = false;
    printf("Enter the key to delete: ");
    scanf("%d", &key);
    for(i = 0; i < 40; i++){
        if(array[i] == key){
            isFound = true;
            break;
        }
    }
    if(isFound){
        array[i] = '*';
        printf("The key is deleted\n");
    }else{
        printf("No key is Found!!");
    }
}
int main(){
    int choice;
    int i;
    for(i = 0; i < 40; i++){
        array[i] = '*';
    }
    while(1){
        printf("\n1. Insert\n2. Search\n3. Delete\n4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch(choice){
            case 1:
                insert();
                break;
            case 2:
                search();
                break;
            case 3:
                Delete();
                break;
           // case 4:
             //   exit();
            default:
                printf("\nEnter correct option\n");
                break;
        }
    }
    return 0;
}


OUT PUT:----------





No comments:

Post a Comment

Note: only a member of this blog may post a comment.