// Add a webpage to curl at the end of the list // returns 0 if successful, -1 otherwise int appendList(WebPage *page) { if (!page) { return -1; } //First add to hashtable and check the page hasn't already been added, if so return and don't add to list if (inHashTable(page->url) != 0) { free(page->url); free(page); return -1; } addToHashTable(page->url); ListNode *node = calloc(1, sizeof(ListNode)); if (!node) { fprintf(stderr, "Calloc failed: Not enough memory for a new node\n"); free(page->url); free(page); return -1; } //Initalize the node node->page = page; //Is this the first node? if (list_size == 0) { URLList.head = node; URLList.tail = node; node->next = NULL; node->prev = NULL; } else { node->prev = URLList.tail; node->next = NULL; URLList.tail->next = node; URLList.tail = node; } list_size++; return 0; }
long memo_coin_sums(int* dens, int size, int amount, HashTable *memo) { Tuple* tuple = convert_to_tuple(dens, size, amount); if(inHashTable(tuple, memo)) { return getFromHashTable(memo, tuple); } else { if(amount<0) return 0; else if(amount==0) return 1; else if(size==1) { if(amount%dens[0]==0) return 1; else return 0; } else { long result = memo_coin_sums(dens, size-1, amount, memo) + memo_coin_sums(dens, size, amount-dens[size-1], memo); insertInHashTable(memo, tuple, result); return result; } } }
// returns 1 if successful, 0 otherwise int addToHashTable(HashTable *hashtable, char *word, int doc_id) { //If it is already in hash table you can leave, because inHashTable handles this case if (inHashTable(hashtable, word, doc_id)) return 1; //otherwise make new node, wordnode, and docnode HashTableNode *node = malloc(sizeof(HashTableNode)); if (!node) { fprintf(stderr, "Malloc failed: not enough memory to allocate a new HashNode in the HashTable.\n"); return 0; } node->next = NULL; WordNode *word_node = malloc(sizeof(WordNode)); if(!word_node) { fprintf(stderr, "Malloc failed: not enough memory to allocate a new WordNode in the HashTable.\n"); return 0; } word_node->word = calloc(1+strlen(word), sizeof(char)); if (word_node->word == NULL) { fprintf(stderr, "Calloc failed: not enough memory to allocate a new word in the HashTable.\n"); return 0; } strcpy(word_node->word, word); //copy the value from the word to our node DocumentNode *add_doc = calloc(1, sizeof(DocumentNode)); if (!add_doc) { fprintf(stderr, "Calloc failed: not enough memory to allocate a new DocumentNode in the HashTable.\n"); return 0; } add_doc->doc_id = doc_id; add_doc->freq = 1; add_doc->next = NULL; word_node->page = add_doc; word_node->next = NULL; word_node->num_docs = 1; node->key = word_node; // get the index we want to insert the has into int index = JenkinsHash(word, MAX_HASH_SLOT); if (hashtable->table[index] == NULL) { //Nothing in the slot so add the node hashtable->table[index] = node; } else { HashTableNode *current = hashtable->table[index]; while(current->next != NULL) { current = current->next; } current->next = node; } return 1; }