C
- voluntas
- @ignis_fatuus
- ブン
- @Linda_pp
- 清楚なC++メイドBOT
- @tzik_tack
- 長谷川一輝
- wraith13
- @jj1bdx
- @cpp_akira
- 安藤敏彦
- @srz_zumix
- Siv3D
- takezoh
- まろ
- @okdshin
- @hnokx
- @ishidakei
- @take_cheeze
- TAKEI Yuya
- @mumumu
- I (@wx257osn2)
- Tommy6
- @tyottyoworks
- ___shanon
- わたやん
- @KorekaraSEDB
- @kariya_mitsuru
- @ciniml
- @beam2d
- @grafi_tt
- @nekketsuuu
- LouiS0616
- @volanja
- 大鎌広
- むてら
- ガチKGB
- 三重野賢人
x
72
1
2
3
4
typedef struct hashElem{5
char *key;6
int freq;7
}hashElem;8
9
struct hashTable{10
int N;11
hashElem **array;12
};13
14
int hash(char *key, int i){//hash function to find the position in the hashtable15
int sum=0;16
while( *key!='\0' ){17
sum = sum + *key;18
key++;19
}20
return (sum+i)%m;21
}22
23
struct hashTable *createHashTable(){24
struct hashTable *ht=malloc(sizeof(struct hashTable));25
if( ht==NULL )26
printf("Error in memory allocation.\n");27
28
ht->N = 0;29
ht->array=malloc(m*sizeof(hashElem *));30
if( ht->array==NULL )31
printf("Error in memory allocation.\n");32
for(int i=0; i<m; i++ )33
ht->array[i] = NULL;34
return ht;35
}36
37
void printHashTable(struct hashTable *ht){38
printf("Printing hash table:\n");39
for(int i=0; i<m; i++)40
if( ht->array[i]!=NULL )41
printf("%s: %d\n", ht->array[i]->key, ht->array[i]->freq); //line of the warning!42
printf("---------end.\n");43
}44
45
int insert(struct hashTable *ht, keyType key){ //insert function... which is a mess but should work46
int index;47
int i=0;48
int collision=1;49
while( collision && i<m ){50
index = hash(key, i);51
if( ht->array[index]!=NULL ){52
if( ht->array[index]!=key ){53
collision=1;54
i++; //if it finds a collision it tries the next free space so i++55
}else56
ht->array[index]->freq++;57
}else{58
collision = 0;59
hashElem *s;60
s=malloc(sizeof(hashElem));61
s->freq++;62
s->key = key;63
ht->array[index] = s;64
ht->N++;65
}66
}67
if( collision==1 )68
return -1;69
else70
return 1;71
}72
$ gcc prog.c -Wall -Wextra -std=gnu11
Start
prog.c: In function 'hash':
prog.c:20:20: error: 'm' undeclared (first use in this function)
20 | return (sum+i)%m;
| ^
prog.c:20:20: note: each undeclared identifier is reported only once for each function it appears in
prog.c: In function 'createHashTable':
prog.c:29:22: error: 'm' undeclared (first use in this function)
29 | ht->array=malloc(m*sizeof(hashElem *));
| ^
prog.c: In function 'printHashTable':
prog.c:39:20: error: 'm' undeclared (first use in this function)
39 | for(int i=0; i<m; i++)
| ^
prog.c: At top level:
prog.c:45:34: error: unknown type name 'keyType'
45 | int insert(struct hashTable *ht, keyType key){ //insert function... which is a mess but should work
| ^~~~~~~
prog.c: In function 'hash':
prog.c:21:1: warning: control reaches end of non-void function [-Wreturn-type]
21 | }
| ^
1
Finish