⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myhash.c

📁 it is c implementation of Hashing .. all types of hashing covered in this program
💻 C
字号:
#include<stdio.h>
#include<conio.h>
#define MAX 10

typedef struct key
{
 int a[MAX];

}K;

void with_out(K s);
void with(K s);
 void swap(int *a,int *b);

int hfunc(int data)
{
 int sum=0;
while(data)
 {
 sum=sum+data%10;
 data=data/10;

  }
 sum=sum%MAX;
 return(sum);
}
 void main()
{
  int ch;
  K s;

  clrscr();
  while(1)
 {
 printf("\n****************MENU***************");
 printf("\n  Enter the choice\n  1.With Replacement");
 printf("\n  2.Without Replacement");
 printf("\n  3.With Chaining ");
 printf("\n  4.Exit");
 printf("\n***********************************");
 scanf("%d",&ch);

  switch(ch)
  {

   case 1:
		   with(s);
		   break;
   case 2:

		  with_out(s);

		   break;
   case 3:
		   break;
   case 4:
		   exit(0);
		   break;
   default:
		   printf("\n\n Wrong choice entered ");

  }
	 getch();
   }
 }


  void with(K s)
 {
 int i,key,hval,k,flag=0,temp,temp1;
 char ans1,ans4;


 for(i=0;i<MAX;i++)
  s.a[i]=-1;
  UP1:
 do
{
 printf("\n\tEnter the Data:");
 scanf("%d",&key);
	hval=hfunc(key);
	temp=hval;

  if(s.a[hval]==-1)
  {
   s.a[hval]=key;

   }
   else
  {

	 k=hval+1;
	while(s.a[k]!=-1)
	{
	 if(hval==k)
	  {
		if(temp!=hfunc(s.a[temp]))
		{
		 if(key!=hfunc(key))
		 swap(&key,&s.a[temp]);
		}
		else
		printf("\n\t Hash Table is Full: ");
		flag=1;
		break;
	   }
	 else
	  {
	   if(k==MAX)
		k=0;
		else
	   k=(k+1)%MAX;
	  }
	}

	 if(flag==0)
	   s.a[k]=key;

	if(temp!=hfunc(s.a[temp]))
	 swap(&s.a[k],&s.a[temp]);

   }
   printf("\n Wanna more ");
   flushall();
   scanf("%c",&ans1);
   }while(ans1=='y' || ans1=='Y');



  printf("\n\n Hash Table is :\n\n");
  printf(" INDEX    DATA   ");
   for(i=0;i<MAX;i++)
  {
	printf("\n");
	printf("   %2d      %2d   ",i,s.a[i]);

  }
   printf("\n Wanted to enter any value ");
   flushall();
   scanf("%c",&ans4);
   if(ans4=='y' || ans4=='Y')
	goto UP1;


}

  void with_out(K s)
 {
 int i,key,hval,k,flag=0;
 char ans,ans5;

 for(i=0;i<MAX;i++)
  s.a[i]=-1;

  UP2:
 do
{
 printf("\n\tEnter the Data:");
 scanf("%d",&key);

  hval=hfunc(key);
  if(s.a[hval]==-1)
  {
   s.a[hval]=key;

   }
   else
 {
	 k=hval+1;
	while(s.a[k]!=-1)
	{

	 if(hval==k)
	  {
		printf("\n\t Hash Table is Full: ");
		flag=1;
		break;
	   }
	  else
	  {
	   k=(k+1)%MAX;
	   }
	 }
	 if(flag==0)
	  s.a[k]=key;


   }
   printf("\n Wanna more ");
   flushall();
   scanf("%c",&ans);
   }while(ans=='y' || ans=='Y');


  printf("\n\n Hash Table is :\n\n");
  printf(" INDEX    DATA   ");
   for(i=0;i<MAX;i++)

  {
  printf("\n");
  printf("   %2d      %2d   ",i,s.a[i]);

	}
	printf("\n Wanted to enter any value ");
   flushall();
   scanf("%c",&ans5);
   if(ans5=='y' || ans5=='Y')
	goto UP2;

  }

 void swap(int *a,int *b)
 {
   int temp;
   temp=*a;
   *a=*b;
   *b=temp;
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -