/** * Inserts a new item into the hash table using double * hashing or linear probing. * * @param h the htable to be hashed into. * @param s the value to be inserted into the hash table. * @return returns whether the value was inserted successfully or not. */ int htable_insert(htable h, const char *s, int docid){ int collisions; int position = htable_word_to_int(s) % h-> capacity; int step = htable_step(h, htable_word_to_int(s)); for (collisions = 0; collisions <= h->capacity; collisions++){ if ((h->keys[position]).key == NULL) { (h->keys[position]).key = emalloc((strlen(s) + 1) * sizeof(s[0])); strcpy((h->keys[position]).key, s); (h->keys[position]).postings = flexarray_new(); flexarray_append((h->keys[position]).postings, docid); h->keys[position].term_freq = 1; h->num_keys++; h->count[position]++; return 1; } else if (!strcmp((h->keys[position]).key, s)){ h->count[position]++; if (flexarray_get_last_id((h->keys[position]).postings) != docid) { flexarray_append((h->keys[position]).postings, docid); h->keys[position].term_freq++; } else { flexarray_updatecount((h->keys[position]).postings); } return 1; } else { position = (position + step) % h->capacity; } } return 0; }
int main(void) { int item; flexarray my_flexarray = flexarray_new(); while (1 == scanf("%d", &item)) { flexarray_append(my_flexarray, item); } flexarray_sort(my_flexarray); flexarray_print(my_flexarray); flexarray_free(my_flexarray); return EXIT_SUCCESS; }
int main(void) { int item; flexarray f1 = flexarray_new(); while(1 == scanf("%d",&item)) { flexarray_append(f1,item); } flexarray_sort(f1); flexarray_print(f1); flexarray_delete(f1); return EXIT_SUCCESS; }