Create a Hash Table to perform the following operations (i) Insertion (ii) Deletion (iii) Search

 #include<stdio.h>

int htable[15],m=11,k,pos,h,ch,flag,count;

void insert(int);

void delete(int);

int search(int);

void display();

int division(int);

int multiply(int);

int main()

{

clrscr();

printf("\nHash Functions:");

printf("\nChoose Hash Function\n1.Division\n2.Multiplication");

scanf("%d",&h);

while(1)

{

printf("\nEnter Your Choice\n1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit");

scanf("%d",&ch);

switch(ch)

{

case 1: if (count>=m)

printf("Hash Table is Full.Insertion Not Possible");

else

{

printf("\nEnter Key to Insert:");

scanf("%d",&k);

insert(k);

}

break;

case 2: if(count==0)

printf("\nHash Table is Empty");

else

{

printf("Enter Key to delete:");

scanf("%d",&k);

delete(k);

}

break;

case 3: if (count==0)

printf("\nHash Table is empty");

else

{

printf("\nEnter Key To Search");

scanf("%d",&k);

pos=search(k);

if(htable[pos]==k)

printf("\nKey is found at %d",pos);

else

printf("\nKey Not Found");

}

break;

case 4: if(count==0)

printf("Hash Table is Empty");

else

display();

break;

default: exit(0);

}

}

return 0;

}

int search(int k)

{

int tc;

if(ch==1)

pos=division(k);

else if(h==2)

pos=multiply(k);

if(ch==1)

{

flag=0;

while (flag==0)

{

if(htable[pos]==k||htable[pos]==0)

{

flag=1;

break;

}

else

{

printf("\n Collision Occured at index %d",pos);

pos=(pos+1)%m;

}

}

}

else

{

flag=0;

tc=0;

while (flag==0)

{

if(htable[pos]==k||tc==m)

{

flag=1;

break;

}

else

{

pos=(pos+1)%m;

tc=tc+1;

}

}

}

return pos;

}

int division (int k)

{

return k%m;

}

int multiply(int k)

{

float A=0.61804,t2;

int t1;

t1=k*A;

t2=k*A;

t2=t2-t1;

pos=t2*m;

return pos;

}

void insert(int k)

{

pos=search(k);

if (htable[pos]==k)

printf("\nKey Already Exists In Hash Table");

else

{

htable[pos]=k;

printf("\nKey %d is inserted at %d",k,pos);

count=count+1;

}

}

void delete(int k)

{

pos=search(k);

if(htable[pos]==k)

{

htable[pos]=0;

printf("\nKey %d is deleted from %d position",k,pos);

count=count+1;

}

else

printf("\nKey is not found in hash table");

}

void display()

{

int i;

printf("\n Index\t key");

for (i=0;i<m;i++)

printf("\n%d\t %d",i,htable[i]);

}

Comments

Popular posts from this blog

Create a Binary Search Tree of integers and perform the following operations (i)insert (ii) delete (iii). Search (iv) traversals (pre-order, in-order, post-order) BST

Creation Of DLL

Develop non-recursive functions to perform search for a Key value in a given list using (i) Binary Search Non Recursive