我正在编写一个anagram程序,它接受一个单词并找到它的anagram,然后返回给定字典文件中的anagram列表。我使用一个链表作为散列映射,并为散列映射的每个节点创建一个列表(根据我的赋值)。我是C语言的新手,所以我在内存和指针方面有很多问题。
我在编译时遇到的一些常见错误:警告:初始化使指针来自整数而没有强制转换列表*pList = getListFromMap (键);我不理解这个错误,pList是一个列表指针,而getListFromMap返回一个列表指针。
getListFromMap()列表的类型冲突*getListFromMap(整键){
之前getListFromMap的delcaration在这里是getListFromMap(key);我读到了一些关于转发声明的东西?虽然我不确定这是如何工作的,但我在尝试它时遇到了错误。
typedef struct list {
char *word;
struct list *next;
} list;
typedef struct hashmap {
list *words;
int key;
struct hashmap *next;
} hashmap;
hashmap *pMapHead;
hashmap *pMapCurr;
list *pListHead;
list *pListCurrd;
int sum = 0; // sum of words
int hCount = 0;
void addWordsToMap(int key, list *words) { // create hashmap
hashmap *pHash = pMapHead;
pMapCurr = pHash;
while (pMapCurr) {
pMapCurr = pMapCurr->next;
}
pMapCurr->words = words;
pMapCurr->key = key;
pMapCurr->next = NULL;
hCount += 1;
}
list *addWord(int key) {
pListHead = getListFromMap(key);
pListCurr = pListHead;
while (pListCurr) {
pListCurr = pListCurr->next;
}
pList->word = word;
pList->next = NULL;
pCurr->next = pList;
pCurr = pList;
return pListHead;
}
list *getListFromMap(int key) {
hashmap *map = pMapHead;
pMapCurr = map;
while (pMapCurr != NULL) {
if (pMapCurr->key == key) {
return pMapCurr->words;
free(map);
}
pMapCurr = pMapCurr->next;
}
}
int getSum(char* word) {
int sum = 0;
int i = 0;
while (word[i]) {
word[i] = toupper(word[i]);
sum += word[i] - '@';
i++;
}
return sum;
}
void loadWords() {
FILE* dict = fopen("/home/akw54/Desktop/CS283/akw54-cs283- summer2016/A1/dict", "r");
if (dict == NULL) {
return;
}
pListHead = (list *) malloc(sizeof(pListHead));
pListCurr = pListHead;
pMapHead = (hashmap *) malloc(sizeof(pMapHead));
pMapCurr = pMapHead;
int key = 0;
list wordList;
char word[128];
while(fgets(word, sizeof(word), dict) != NULL) {
key = getSum(word);
addWordsToMap(key, addWord(key));
}
free(dict);
}
void main() {
loadWords();
free(pMapHead);
free(pMapCurr);
free(pListHead);
free(pListCurr);
}https://stackoverflow.com/questions/38295618
复制相似问题