Everytime I run this code, the process never ends... maybe there's something wrong with the code.
The code is about picking a text, making a list with every word of the text, then making use of a hashfunction (R&K) to create a dictionary with one instance each word, and printing the whole table.
Here's the code.
Thanks in advice,
Lucas.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list {
list* next;
char word[40];
};
struct hashtable{
char word[40];
};
// Structs defined
unsigned int hashfunction(char *s) {
unsigned int hashval;
for (hashval = 0; *s != '\0'; s++){
hashval = *s + 31 * hashval;
}
return hashval % 5000;
}
// Not sure if the hashfunction is correct. Got that at the internet.
list* parser(char* c){
list *p = (list*) malloc(sizeof(list));
list *q = p;
char word[40]="";
int i = 0;
int j = 0;
while (c[i+1]!="\0"){
if(c[i] != "," || c[i] != "." || c[i] != " " || c[i] != "/"){
word[j]=c[i];
j++;
}
if( c[i] == "," || c[i] == "." || c[i] == " " || c[i] == "/"){
strcpy(word,q->word);
q=q->next;
j=0;
memset ((void *) &word, '\0', sizeof(word));
}
i++;
}
return p;
}
// Parser implemented
hashtable* hashing(hashtable* table, list* l){
list* p = l;
unsigned int val;
while(p->next!=NULL){
val = hashfunction(p->word);
if(table[val].word==NULL){
strcpy(p->word, table[val].word);
}
p=p->next;
}
return table;
}
// Hashing process implemented.
int main() {
char text[50000];
hashtable table[5000];
printf("What's your text? ");
gets(texto);
hashing(table,parser(text));
for (int i=0;i < 5000;i++){
if(table[i].word!='\0'){
printf("%c", table[i].word);
}
}
system("pause");
return 0;
}