Edit the code so that quadratic probing is the searching strategy used. You will need to modify insert function, then search and then delete.
C Programming
#include
#define MAX_SIZE 29
#define TABLE_SIZE 59999
struct htable {
char entries[TABLE_SIZE][MAX_SIZE+1];
};
void initTable(struct htable *h);
int hashvalue(char word[]);
void insertTable(struct htable *h, char word[]);
int searchTable(struct htable *h, char word[]);
void deleteTable(struct htable *h, char word[]);
int main() {
char filename[MAX_SIZE+1], temp[MAX_SIZE+1];
FILE *ifp;
int numwords, i;
struct htable mytable;
int ans;
printf("What is the name of the dictionary file?\n");
scanf("%s", &filename);
ifp = fopen(filename, "r");
fscanf(ifp, "%d", &numwords);
printf("get here\n");
initTable(&mytable);
printf("iniit table\n");
for (i=0; ifscanf(ifp, "%s", temp);
insertTable(&mytable, temp);
}
do {
printf("Do you want to 1)search word, 2) add word, 3) delete a word?\n");
scanf("%d", &ans);
if (ans == 1) {
printf("What word are you looking for?\n");
scanf("%s", temp);
if (searchTable(&mytable, temp))
printf("%s was found.\n", temp);
else
printf("%s was NOT found.\n", temp);
}
else if (ans == 2) {
printf("What word do you want to add?\n");
scanf("%s", temp);
if (searchTable(&mytable, temp))
printf("%s was ALREADY in the table\n", temp);
else
insertTable(&mytable, temp);
}
else if (ans == 3) {
printf("What word do you want to delete?\n");
scanf("%s", temp);
deleteTable(&mytable, temp);
}
} while (ans < 4); // Not very user friendly, just quits for any number > 3.
system("PAUSE");
return 0;
}
void initTable(struct htable *h) {
int i;
for (i=0; istrcpy(h->entries[i], "");
}
.
int hashvalue(char word[]) {
int i, sum=0;
.
for (i=0; isum = (128*sum + (int)(word[i]))%TABLE_SIZE;
return sum;
}
void insertTable(struct htable *h, char word[]) {
int hashval;
hashval = hashvalue(word);
while (strcmp(h->entries[hashval], "") != 0)
hashval = (hashval+1)%TABLE_SIZE;
strcpy(h->entries[hashval], word);
}
int searchTable(struct htable *h, char word[]) {
int hashval;
hashval = hashvalue(word);
while (strcmp(h->entries[hashval], "") != 0 &&
strcmp(h->entries[hashval], word) != 0)
hashval = (hashval+1)%TABLE_SIZE;
if (strcmp(h->entries[hashval], word) == 0)
return 1;
return 0;
}
void deleteTable(struct htable *h, char word[]) {
int hashval;
hashval = hashvalue(word);
while (strcmp(h->entries[hashval], "") != 0 &&
strcmp(h->entries[hashval], word) != 0)
hashval = (hashval+1)%TABLE_SIZE;
if (strcmp(h->entries[hashval], word) == 0)
strcpy(h->entries[hashval],"");
}



Answer :

Other Questions