You are on page 1of 2

#include #include #include #include

<stdio.h> <string.h> <iostream> <fstream>

#include "list.h" using namespace std; const int TABLE_SIZE = 100; class hashtable { public: list **table; char uword[40],sword[40]; int len,hvalue; public: hashtable() { table = new list*[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; i++) table[i] = NULL; ifstream indic("dictionary.txt"); while (!indic.eof()) { indic.getline(uword,40); hvalue=findhash(uword); sort(); put(hvalue,uword,sword); } } void get(char input[]) { int count; int len = strlen(input); int hash = findhash(input); list *entry = table[hash]; if (table[hash] != NULL) { count=0; //cout<<input; while (entry != NULL ) { if(strcmp(input,entry->sword)==0) { cout<< " "<<entry->getword()<<" count++; entry = entry->next; } } cout<<count<<endl; } } void put(int hvalue,char uword[],char sword[]) { if (table[hvalue] == NULL) table[hvalue] = new list(uword,sword);

";

else { list *entry = table[hvalue]; while (entry->next != NULL) entry = entry->next; if(entry->next == NULL) entry->next=new list(uword,sword); } } int findhash(char uword[]) { int i=0,sum=0; while(uword[i]!='\0') { uword[i] = tolower(uword[i]); sum = sum + (int)uword[i]-96; i++; } return sum%TABLE_SIZE; } void sort() { strcpy(sword,uword); int len= strlen(sword); int i,j; for(i=0;i<len;i++) { for(j=0;j<i;j++) { if((int)sword[i]<(int)sword[j]) { char temp=sword[i]; sword[i]=sword[j]; sword[j]=temp; } } } } };

You might also like