I am making an anagram program that takes a word and finds its anagrams then returns the list of anagrams from a given dictionary file. I am using a linked list as a hash map and a list for each node of the hash map (as per my assignment). I am pretty new to C so I am having a lot of issues with memory and pointers.
Some of the common errors I am getting when I compile: warning: initialization makes pointer from integer without a cast list *pList = getListFromMap(key); I don't understand this one, pList is a list pointer and getListFromMap is returning a list pointer.
conflicting types for getListFromMap() list *getListFromMap(int key) {
previous delcaration of getListFromMap was here getListFromMap(key); I read something about forward declaration? Though I am not sure how this works, I got an error trying it.
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);
}