Esempio n. 1
0
bool AppendNode(char * data){
	NODE * pNode = (NODE *)malloc(sizeof(NODE));
	if( !pNode ){
		return false;
	}
	pNode->data = data;
	pNode->hashVal = CalHash(data);
	pNode->pNext = gHeadNode.pNext;
	gHeadNode.pNext = pNode;
	gLinkLength++;
	return true;
}
Esempio n. 2
0
int main(void) {
    // Construct list
    pWord* hashList = (pWord*)malloc(260 * sizeof(pWord));
    
    for (int i = 0; i < 260; ++i) {
        hashList[i] = NULL;
    }
    
    // Read and save words
    char allInput[40];
    int i = 0;
    while (1) {
        fgets(allInput, sizeof(allInput), stdin);
        strtok(allInput, "\n");
        if (strcmp(allInput, "\n") == 0) {
            break;
        }
        // Split words
        pWord wordGrp = SpiltWord(allInput);
        
        // Calculate Hash Number
        int wordHash = CalHash(wordGrp);
        
        // Store the section to the list
        if (hashList[wordHash] == NULL) {
            hashList[wordHash] = wordGrp;
        } else {
            // Go to the end of the list
            TraceList(hashList[wordHash])->nextWord = wordGrp;
        }
        i = 0;
    }
    char output[100000][10];
    int pointer = 0;
    // Search
    while (1) {
        char queryWord[100];
        i = 0;
        void* saver = fgets(queryWord, sizeof(queryWord), stdin);
        if (saver == NULL) {
            break;
        }
        strtok(queryWord, "\n");
        strtok(queryWord, " ");
        int queryHash = WordHash(queryWord);
        pWord foundNode = FindMatch(hashList, queryHash, queryWord);

        if (foundNode) {
            strcpy(output[pointer++], foundNode->word);
        } else {
            strcpy(output[pointer++], "eh");
        }
        
    }
    if (pointer != 0) {
        for (int i = 0; i < pointer; ++ i) {
            printf("%s\n", output[i]);
        }
    }
    return 0;
}