I have a problem with hash
I'm programming a hash with alphabetics, use as a dictionary, but I i have his problem:
When I add a new word after I had a hash with some word, old hash is deleted and it's creat a new hash, but I don't want so, I want it has the same hash because i want to save it:
And, this is my code:
Code:
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include <stdlib.h>
#define B 26
#define maxlegth 30
typedef char datatype;
typedef struct Node
{
datatype Tu[maxlegth];
datatype Nghia[maxlegth];
Node*Next;
};
typedef Node*Position;
typedef Position Dictionary[B];
//Thiet ke ham bam
int H(datatype X[maxlegth])
{
int chudau;
char*s;
s=strlwr(X);
chudau=int(s[0])-97;
return chudau%B;
}
//Tao bang bam rong;
void MakenullSet(Dictionary*D)
{
for(int i=0;i<B;i++)
(*D)[i]=NULL;
}
//Kiem tra thanh vien trong bang bam;
int MemberSet(datatype X[maxlegth],Dictionary D)
{
Position P;
P=D[H(X)];
int Found=0;
while((P!=NULL) && (Found!=1))
{
//so sanh chuoi
if(strcmp(P->Tu,X)==0)
Found=1;
else P=P->Next;
}
return Found;
}
//Them phan tu vao bang bam
void InsertSet(datatype X[maxlegth],datatype Y[maxlegth],Dictionary *D)
{
int Bucket;
Position P;
Bucket=H(X);
P=(*D)[Bucket];
(*D)[Bucket]=(Node*)malloc(sizeof(Node));
//Khoi tao chuoi
memset((*D)[Bucket]->Tu,'\0',maxlegth);
memset((*D)[Bucket]->Nghia,'\0',maxlegth);
//Chep chuoi
strcpy((*D)[Bucket]->Tu,X);
strcpy((*D)[Bucket]->Nghia,Y);
(*D)[Bucket]->Next=P;
}
//Nhap du lieu cho bang bam tu ban phim
void ReadSet(Dictionary*D)
{
int i;
int n;
datatype X[maxlegth];
datatype Y[maxlegth];
MakenullSet(D);
printf("\n--------------------------- Number of word to add < Number only>: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
flushall();
// Nhap chuoi ham gets
printf("\nWord number %d: ",i); gets(X);
printf("\nMeaning: "); gets(Y);
InsertSet(X,Y,D);
}
}
//In du lieu ra man hinh
void PrintSet(Dictionary D)
{
Position P;
for(int i=0;i<B;i++)
{
P=D[i];
printf("Bucket %c: ",char(i+65));
while(P!=NULL)
{
printf("%s",P->Tu);
printf(" - ");
printf("%s",P->Nghia);
P=P->Next;
}
printf("\n");
} printf("\n");
}
void FindSet(datatype F[maxlegth],Dictionary D)
{
Position P;
F=strlwr(F);
int Found=0;
for(int i=0;i<B;i++)
{
P=D[i];
while(P!=NULL && Found==0)
{
if(strcmp(F,P->Tu)==0)
{
printf("Meaning: %s",P->Nghia);
Found=1;
}
else
{
P=P->Next;
}
}
}
if(Found==0)
{
printf("\nWe don't see this word.");
}
printf("\n");
}
void showmenu()
{
printf("\n\t-------------------- COMPUTER DICTIONARY -------------------");
printf("\n[1]. Add WORDS.");
printf("\n[2]. Find a word.");
printf("\n[3]. Display all.");
printf("\n[4]. Save and Close.");
printf("\n\nEnter Choice: ");
}
void prog()
{
clrscr();
int menuchoice;
Dictionary D;
datatype X[maxlegth];
datatype F[maxlegth];
while(1)
{
clrscr();
showmenu();
scanf("%d",&menuchoice);
switch(menuchoice)
{
case 1:printf("\n\nAdd content of hash with %d Bucket: \n",B);
ReadSet(&D);
break;
case 2:flushall();
printf("\nAdd word which you want to find: ");
gets(F);
FindSet(F,D);
getch();
break;
case 3:printf("\n\nDictionary: \n");
PrintSet(D);
getch();
break;
case 4:exit(1);
break;
default:printf("Enter Again");
prog();
break;
}
}
}
int main()
{
clrscr();
showmenu();
prog();
return 0;
}