-->

Friday, 7 April 2017

IMPLEMENTING HASHING PROGRAM

IMPLEMENTATION OF HASHING



/***********************************************************
* You can use all the programs on  www.kuttyselvapro.blogspot.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact kuttyselva57@gmail.com
* To find more C programs, do visit www.kuttyselvapro.blogspot.com
* and browse!
*
*                      Happy Coding
***********************************************************/
#include<stdio.h>
#include<conio.h>
void main() {
 int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 int n, value;
 int temp, hash;
 clrscr();
 printf("\nEnter the value of n(table size):");
 scanf("%d", &n);
 do {
  printf("\nEnter the hash value");
  scanf("%d", &value);
  hash = value % n;
  if (a[hash] == 0) {
   a[hash] = value;
   printf("\na[%d]the value %d is stored", hash, value);
  } else {
   for (hash++; hash < n; hash++) {
    if (a[hash] == 0) {
     printf("Space is allocated give other value");
     a[hash] = value;
     printf("\n a[%d]the value %d is stored", hash, value);
     goto menu;
    }
   }
   for (hash = 0; hash < n; hash++) {
    if (a[hash] == 0) {
     printf("Space is allocated give other value");
     a[hash] = value;
     printf("\n a[%d]the value %d is stored", hash, value);
     goto menu;
    }
   }
   printf("\n\nERROR\n");
   printf("\nEnter '0' and press 'Enter key' twice to exit");
  }
  menu:
  printf("\n Do u want enter more");
  scanf("%d", &temp);
 }
 while (temp == 1);
 getch();
}
_________________________________________________________________________
IMPLEMENTING INSERTION DISPLAY HASING
Example Program To Implement Chain Hashing(in C):



  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>

  struct hash *hashTable = NULL;
  int eleCount = 0;

  struct node {
        int key, age;
        char name[100];
        struct node *next;
  };

  struct hash {
        struct node *head;
        int count;
  };

  struct node * createNode(int key, char *name, int age) {
        struct node *newnode;
        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->key = key;
        newnode->age = age;
        strcpy(newnode->name, name);
        newnode->next = NULL;
        return newnode;
  }


  void insertToHash(int key, char *name, int age) {
        int hashIndex = key % eleCount;
        struct node *newnode =  createNode(key, name, age);
        /* head of list for the bucket with index "hashIndex" */
        if (!hashTable[hashIndex].head) {
                hashTable[hashIndex].head = newnode;
                hashTable[hashIndex].count = 1;
                return;
        }
        /* adding new node to the list */
        newnode->next = (hashTable[hashIndex].head);
        /*
         * update the head of the list and no of
         * nodes in the current bucket
         */
        hashTable[hashIndex].head = newnode;
        hashTable[hashIndex].count++;
        return;
  }


  void deleteFromHash(int key) {
        /* find the bucket using hash index */
        int hashIndex = key % eleCount, flag = 0;
        struct node *temp, *myNode;
        /* get the list head from current bucket */
        myNode = hashTable[hashIndex].head;
        if (!myNode) {
                printf("Given data is not present in hash Table!!\n");
                return;
        }
        temp = myNode;
        while (myNode != NULL) {
                /* delete the node with given key */
                if (myNode->key == key) {
                        flag = 1;
                        if (myNode == hashTable[hashIndex].head)
                                hashTable[hashIndex].head = myNode->next;
                        else
                                temp->next = myNode->next;

                        hashTable[hashIndex].count--;
                        free(myNode);
                        break;
                }
                temp = myNode;
                myNode = myNode->next;
        }
        if (flag)
                printf("Data deleted successfully from Hash Table\n");
        else
                printf("Given data is not present in hash Table!!!!\n");
        return;
  }


  void searchInHash(int key) {
        int hashIndex = key % eleCount, flag = 0;
        struct node *myNode;
        myNode = hashTable[hashIndex].head;
        if (!myNode) {
                printf("Search element unavailable in hash table\n");
                return;
        }
        while (myNode != NULL) {
                if (myNode->key == key) {
                        printf("VoterID  : %d\n", myNode->key);
                        printf("Name     : %s\n", myNode->name);
                        printf("Age      : %d\n", myNode->age);
                        flag = 1;
                        break;
                }
                myNode = myNode->next;
        }
        if (!flag)
                printf("Search element unavailable in hash table\n");
        return;
  }

  void display() {
        struct node *myNode;
        int i;
        for (i = 0; i < eleCount; i++) {
                if (hashTable[i].count == 0)
                        continue;
                myNode = hashTable[i].head;
                if (!myNode)
                        continue;
                printf("\nData at index %d in Hash Table:\n", i);
                printf("VoterID     Name          Age   \n");
                printf("--------------------------------\n");
                while (myNode != NULL) {
                        printf("%-12d", myNode->key);
                        printf("%-15s", myNode->name);
                        printf("%d\n", myNode->age);
                        myNode = myNode->next;
                }
        }
        return;
  }

  int main() {
        int n, ch, key, age;
        char name[100];
        printf("Enter the number of elements:");
        scanf("%d", &n);
        eleCount = n;
        /* create hash table with "n" no of buckets */
        hashTable = (struct hash *)calloc(n, sizeof (struct hash));
        while (1) {
                printf("\n1. Insertion\t2. Deletion\n");
                printf("3. Searching\t4. Display\n5. Exit\n");
                printf("Enter your choice:");
                scanf("%d", &ch);
                switch (ch) {
                        case 1:
                                printf("Enter the key value:");
                                scanf("%d", &key);
                                getchar();
                                printf("Name:");
                                fgets(name, 100, stdin);
                                name[strlen(name) - 1] = '\0';
                                printf("Age:");
                                scanf("%d", &age);
                                /*inserting new node to hash table */
                                insertToHash(key, name, age);
                                break;

                        case 2:
                                printf("Enter the key to perform deletion:");
                                scanf("%d", &key);
                                /* delete node with "key" from hash table */
                                deleteFromHash(key);
                                break;

                        case 3:
                                printf("Enter the key to search:");
                                scanf("%d", &key);
                                searchInHash(key);
                                break;
                        case 4:
                                display();
                                break;
                        case 5:
                                exit(0);
                        default:
                                printf("U have entered wrong option!!\n");
                                break;
                }
        }
        return 0;
  }


  Output (C Program To Perform Insertion, Deletion In Chain Hash Table):
  jp@jp-VirtualBox:$ ./a.out
  Enter the number of elements:3

  1. Insertion 2. Deletion
  3. Searching 4. Display
  5. Exit
  Enter your choice:1
  Enter the key value:3
  Name:Sally
  Age:23

  1. Insertion 2. Deletion
  3. Searching 4. Display
  5. Exit
  Enter your choice:1
  Enter the key value:33
  Name:Harry
  Age:25

  1. Insertion 2. Deletion
  3. Searching 4. Display
  5. Exit
  Enter your choice:1
  Enter the key value:7
  Name:Nick
  Age:30

  1. Insertion 2. Deletion
  3. Searching 4. Display
  5. Exit
  Enter your choice:1
  Enter the key value:35
  Name:Raj
  Age:28

  1. Insertion 2. Deletion
  3. Searching 4. Display
  5. Exit
  Enter your choice:4

  Data at index 0 in Hash Table:
  VoterID     Name          Age   
  --------------------------------
  33          Harry          25
  3           Sally          23
  
  Data at index 1 in Hash Table:
  VoterID     Name          Age   
  --------------------------------
  7           Nick           30

  Data at index 2 in Hash Table:
  VoterID     Name          Age   
  --------------------------------
  35          Raj            28

  1. Insertion 2. Deletion
  3. Searching 4. Display
  5. Exit
  Enter your choice:2
  Enter the key to perform deletion:33
  Data deleted successfully from Hash Table

  1. Insertion 2. Deletion
  3. Searching 4. Display
  5. Exit
  Enter your choice:4

  Data at index 0 in Hash Table:
  VoterID     Name          Age   
  --------------------------------
  3           Sally          23

  Data at index 1 in Hash Table:
  VoterID     Name          Age   
  --------------------------------
  7           Nick           30

  Data at index 2 in Hash Table:
  VoterID     Name          Age   
  --------------------------------
  35          Raj            28

  1. Insertion 2. Deletion
  3. Searching 4. Display
  5. Exit
  Enter your choice:3
  Enter the key to search:35
  VoterID  : 35
  Name     : Raj
  Age      : 28

  1. Insertion 2. Deletion
  3. Searching 4. Display
  5. Exit

  Enter your choice:5
ANOTHER HASHING PROGRAM

This Program performs Hashing Algorithm in C.

#include<stdio.h>
#include<conio.h>
void hash1(int,int,int *);
void hash(int,int *);
int count=1,t=0;
void main()
{

int table[11],key,i=0,j=0,c;
clrscr();
for(;i<=10;i++)
table[i]=0;
printf("------------------------------ HASHING ---------------------------------\n\n");
printf("Enter the no of elements you want to enter : ");
scanf("%d",&c);
for(j=0;j<c;j++)
{
printf("\n\nEnter element : ");
scanf("%d",&key);
t=0;
hash(key,table);
printf("\n\n----------------TABLE IS--------------\n\n");
for(i=0;i<=10;i++)
printf("\n\t\t%d\t%d\n",i,table[i]);
}
getch();
}

void hash(int key,int *table)
{
int h;
h=key%11;
if(*(table+h)==0)
{
*(table+h)=key;
}
else
{
t=h;
hash1(key,h,table);
}
}

void hash1(int key,int h,int *table)
{
if(count<=3)
{
if((key%7)!=0)
h=h+(key%7);
else
h=h+((key+1)%7);
h=h%11;
if(*(table+h)==0)
*(table+h)=key;
else
hash1(key,h,table);
count++;
}
else
{
h=t;
h++;
while(h!=t)
{
h=(h)%11;
if(*(table+h)==0)
{
*(table+h)=key;
printf("\n\n-----NUMBER IS INSERTED IN THE TABLE-----\n\n");
break;
}                          
h++;
}
if(h==t)
printf("\n\n----SPACE IS NOT AVAILABLE IN THE TABLE-----\n\n");
}
}

keep calm and say bujuku bujuku.

0 comments:

Post a Comment

Start Work With Me

Contact Us
KUTTY SELVA
+91 7708139984
Madurai,Tamilnadu