Estou tentando entender um exemplo de código em C para contar palavras (do livro Programming Pearls). Mas estou com dúvida em alguns trechos do código (talvez eu não tenha digitado corretamente):
nodeptr bin[NHASH];
Isso declara uma array do tiponodeptr
com o tamanho de 29989?for i = [0, NHASH]
Este comando existe em C? É apenas uma forma
abreviada de um loopfor(;;)
?
Código completo:
typedef struct node *nodeptr;
typedef struct node {
char *word;
int count;
nodeptr next;
} node;
#define NHASH 29989
#define MULT 31
nodeptr bin[NHASH];
unsigned int hash(char *p){
unsigned int h = 0
for ( ; *p; p++)
h = MULT * h + *p
return h % NHASH
}
int main(void){
for i = [0, NHASH]
bin[i] = NULL
while scanf("%s", buf) != EOF
incword(buf)
for i = [0, NHASH]
for (p = bin[i]; p != NULL; p = p->next)
print p->word, p->count
return 0
}
void incword(char *s){
h = hash(s)
for (p = bin[h]; p != NULL; p = p->next)
if strcmp(s, p->word) == 0
(p->count)++
return
p = malloc(sizeof(hashnode))
p->count = 1
p->word = malloc(strlen(s)+1)
strcpy(p->word, s)
p->next = bin[h]
bin[h] = p
}