Do the class/structure description for a Hash table, and write the source code for the insert function.
A very simplistic and limited implementation of Hash table may look something like this:
For a 100 entry hash table of words, put in the arguments from the command line into the hash
table:
#define MAX 100
char **hashTable;
int hashIndex(char *);
int main(int argc, char **argv)
{
if(argc < 2) {
printf("invalid input\n");
exit(1);
}
hashTable = (char **)malloc(argc * sizeof(char *));
for(i=0;ihashTable[i] = NULL;
}
while(count < argc) {
index = hashIndex(argv[count]);
if(!hashTable[index]) {
hashTable[index] = (char *)malloc(strlen(argv[count])
*sizeof(char));
strcpy(hashTable[index], argv[count]);
}
else {
//resolve collision
count++;
}
}
return 0;
}
int hashIndex(char *wd)
{
int count, index=0;
int len = strlen(wd);
while(count < len) {
index += atoi(wd[count]);
}
index = (int) index % MAX;
return index;
}
For a 100 entry hash table of words, put in the arguments from the command line into the hash
table:
#define MAX 100
char **hashTable;
int hashIndex(char *);
int main(int argc, char **argv)
{
if(argc < 2) {
printf("invalid input\n");
exit(1);
}
hashTable = (char **)malloc(argc * sizeof(char *));
for(i=0;i
}
while(count < argc) {
index = hashIndex(argv[count]);
if(!hashTable[index]) {
hashTable[index] = (char *)malloc(strlen(argv[count])
*sizeof(char));
strcpy(hashTable[index], argv[count]);
}
else {
//resolve collision
count++;
}
}
return 0;
}
int hashIndex(char *wd)
{
int count, index=0;
int len = strlen(wd);
while(count < len) {
index += atoi(wd[count]);
}
index = (int) index % MAX;
return index;
}
Comments
Post a Comment
https://gengwg.blogspot.com/