// Add an new word to the global list // wordStart is coming from the stack, so we must allocate a block // of memory to hold actual word here. void addNewWord(char * wordStart, int length, size_t index) { wordInfo_t * wordInfo = NULL; char * word = NULL; wordIndex_t * wordIndex; // Malloc a block of memory with size "wordInfo_t" wordInfo = (wordInfo_t *)malloc(sizeof(wordInfo_t)); // We must allocate a new block of memory to hold actual word. // We can not point to wordStart since it is an local variable word = (char *)malloc(length+1); if(wordInfo == NULL || word == NULL) { fprintf(stderr, "Failed to allocate memory for wordInfo_t and word.\n"); return; } // Initialize the word related information. memcpy(word, wordStart, length); word[length] = '\0'; wordInfo->word = word; wordInfo->count = 1; wordInfo->dup = 0; listInit(&wordInfo->list); listInit(&wordInfo->indexlist); // Add this word into the global list. //fprintf(stderr, "Add new word: %s length %d. ADDING wordInfo %p\n", word, length,& wordInfo->list); listInsertTail(&wordInfo->list, &gWordsList); // Add corresponding index into the index list. wordIndex = (wordIndex_t *)malloc(sizeof(wordIndex_t)); if(wordIndex == NULL) { fprintf(stderr, "malloc failed for wordIndex\n"); return; } wordIndex->index = index; //fprintf(stderr, "Adding wordIndex %p with index %ld\n", &wordIndex->list, index); listInit(&wordIndex->list); listInsertTail(&wordIndex->list, &wordInfo->indexlist); //listPrintItems(&gWordsList, 1); fprintf(stderr, "Adding wordIndex %p index %ld at %p\n", &wordIndex->list, wordIndex->index, &wordIndex->index); totalWords++; totalDiffWords++; }
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode *pHead = new ListNode(1); int nCarry = 0; pHead->val = -1; pHead->next = NULL; while (l1 != NULL || l2 != NULL) { ListNode *pData = new ListNode(1); if (l1 != NULL && l2 != NULL) { pData->val = l1->val + l2->val + nCarry; nCarry = pData->val / 10; pData->val = pData->val % 10; listInsertTail(pHead, pData); l1 = l1->next; l2 = l2->next; } else if (l1 != NULL) { pData->val = l1->val + nCarry; nCarry = pData->val / 10; pData->val = pData->val % 10; listInsertTail(pHead, pData); l1 = l1->next; } else if (l2 != NULL) { pData->val = l2->val + nCarry; nCarry = pData->val / 10; pData->val = pData->val % 10; listInsertTail(pHead, pData); l2 = l2->next; } } if (nCarry != 0) { ListNode *pData = new ListNode(1);; pData->val = nCarry; nCarry = 0; listInsertTail(pHead, pData); } return pHead->next; }
// Add an existing word to the global list void addExistingWord(wordInfo_t * wordInfo, char * wordStart, int length, size_t index) { wordIndex_t * lastIndex; wordIndex_t * wordIndex; if(wordInfo == NULL) { fprintf(stderr, "Wrong: wordInfo not existing here.\n"); return; } // Check whether the tail has the same index. list_t * list = tailList(&wordInfo->indexlist); if(lastIndex == NULL) { fprintf(stderr, "Wrong: lastIndex list is not existing.\n"); return; } lastIndex = getWordIndexFromList(list); //fprintf(stderr, "lastIndex %p index %d at %p when existing\n", lastIndex, lastIndex->index, &lastIndex->index); if(lastIndex->index != index) { // Malloc a block of memory to hold it. wordIndex = (wordIndex_t *)malloc(sizeof(wordIndex_t)); wordIndex->index = index; listInit(&wordIndex->list); listInsertTail(&wordIndex->list, &wordInfo->indexlist); wordInfo->count++; //listPrintItems(&gWordsList, 1); //fprintf(stderr, "NOt duplicate, lastIndex %d now index %d!!\n", lastIndex->index, index); } else { //fprintf(stderr, "duplicate!!\n"); wordInfo->dup++; totalDuplicateWords++; } totalWords++; }