The code is ment for memory optimization and quick search of desired words. Such technique can be used for any type of efficient data search
//**************************************
//INCLUDE files for :Hash Table for creating Dictionary
//**************************************
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <alloc.h>
# include <string.h>
//**************************************
// Name: Hash Table for creating Dictionary
// Description:The code is ment for memory optimization and quick search of desired words. Such technique can be used for any type of efficient data search
// By: Yogesh Ranade
//
//
// Inputs:words and their meanings
//
// Returns:searching facility for word search which will return appropriate meaning
//
//Assumes:None
//
//Side Effects:Nothing
//This code is copyrighted and has limited warranties.
//Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.6108/lngWId.3/qx/vb/scripts/ShowCode.htm
//for details.
//**************************************
/*
HASH TABLE FOR CREATING A WORD LIST AND ITS DEFINITION
AUTHOR : YOGESH
*/
# define HASHSIZE 100
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <alloc.h>
# include <string.h>
/////////////////////////////////////////////////////
struct nlist
{
char *name;
char *def;
struct nlist *next;
};
/////////////////////////////////////////////////////
struct nlist *hashtab[HASHSIZE];
/////////////////////////////////////////////////////
struct nlist * nalloc(void)
{
struct nlist *np;
np=(struct nlist *)malloc(sizeof(struct nlist));
if(np==NULL)
{
printf("mem limit");
exit(1);
}
return(np);
}
char * strsave(char *s)
{
char *p;
p=(char *)malloc(strlen(s)+1);
if(p==NULL)
{
printf("mem limit");
exit(1);
}
strcpy(p,s);
return(p);
}
int hash(char *s)
{
int hashval=0;
for( ;*s!='\0';s++)
hashval=hashval+(*s);
// eprintf("\n%d",hashval%HASHSIZE);
return(hashval%HASHSIZE);
}
struct nlist * lookup(char *s)
{
struct nlist *np;
np=hashtab[hash(s)];
for( ; np!=NULL;np=np->next)
{
if(strcmp(s,np->name)==0)
return(np);
}
return(NULL);
}
struct nlist * install(char *n,char *d)
{
struct nlist *np;
int hashval;
np=lookup(n);
if(np==NULL)
{
np=nalloc();
np->name=strsave(n);
np->def=strsave(d);
hashval=hash(n);
np->next=hashtab[hashval];
hashtab[hashval]=np;
}
else
{
free(np->def);
np->def=strsave(d);
}
return(np);
}
void main(void)
{
int n=0;
char *word,*def;
struct nlist *temp;
clrscr();
printf("HASH TABLE FOR CREATING A WORD LIST AND ITS DEFINITION\n\n");
printf("Enter Word and it's meaning or Enter 'quit' to exit.\n");
while(strcmp(gets(word),"quit")!=0)
{
//gets(word);
gets(def);
if(strcmp(def,"quit")==0)
break;
temp=lookup(word);
if(temp!=NULL)
{
printf("Word '%s' is already entered",temp->name);
continue;
}
else
temp=install(word,def);
}
printf("\nWord list : \n");
for(n=0;n<HASHSIZE;n++)
{
temp=hashtab[n];
while(temp!=NULL)
{
printf("\nWords at index %d\n",n);
printf("%s : %s\n",temp->name,temp->def);
temp=temp->next;
}
}
getch();
}
No comments:
Post a Comment