示例#1
0
int main(int argc, const char * argv[])
{
    struct hashTable *ht;
    char *value;
    
    createHashTable(10, &ht);
    
    printf("Adding 'hello'=>'world', 'color'=>'blue' and printing:\n-----\n");
    
    // add "hello" => "world", and verify
    assert(!valueForKeyInHashTable(ht, "hello", &value));
    assert(NULL == value);
    assert(addToHashTable(ht, "hello", "world"));   // do it once,
    assert(addToHashTable(ht, "hello", "world"));   // then again, for good measure
    assert(valueForKeyInHashTable(ht, "hello", &value));
    assert(0 == strcmp("world", value));
    
    // add "color" => "blue", and verify
    assert(!valueForKeyInHashTable(ht, "color", &value));
    assert(NULL == value);
    assert(addToHashTable(ht, "color", "blue"));
    assert(valueForKeyInHashTable(ht, "color", &value));
    assert(0 == strcmp("blue", value));
    
    
    // print all the values we've added
    assert(printAllKeysAndValues(ht));
    
    
    printf("\n\nChanging 'hello' value to 'goodbye', then printing:\n-----\n");
    
    // change "hello"'s value to "goodbye"
    assert(addToHashTable(ht, "hello", "goodbye"));   // do it once,
    assert(valueForKeyInHashTable(ht, "hello", &value));
    assert(0 == strcmp("goodbye", value));
    
    
    // print all the values we've added
    assert(printAllKeysAndValues(ht));
    
    // --
    // now remove and print
    
    printf("\n\nRemoving 'hello' and printing:\n-----\n");
    assert(removeFromHashTable(ht, "hello"));
    assert(printAllKeysAndValues(ht));
    
    printf("\n\nRemoving 'color' and printing:\n-----\n");
    assert(removeFromHashTable(ht, "color"));
    assert(printAllKeysAndValues(ht));
    
    
    // clean up
    destroyHashTable(&ht);
    assert(NULL == ht);
}
示例#2
0
int main() {
    int capacity = 11, i;
    HashTablePtr t = newHashTable(capacity, hashInt, compare);
    HashNodePtr node;
    putIntoHashTable(t, 1, 1);
    putIntoHashTable(t, 2, 2);
    putIntoHashTable(t, 3, 3);
    putIntoHashTable(t, 4, 4);
    putIntoHashTable(t, 5, 5);
    printHashTable(t);
    for(i = 0; i <= 5; i++) {
        HashNodePtr node = getFromHashTable(t, i);
        if(node) {
            printf("key %d values %d\n", node->key, node->value);
        } else {
            printf("cann't find key %d\n", i);
        }
    }
    for(i = 11; i <= 16; i++) {
        HashNodePtr node = getFromHashTable(t, i);
        if(node) {
            printf("key %d values %d\n", node->key, node->value);
        } else {
            printf("cann't find key %d\n", i);
        }
    }
    removeFromHashTable(t, 2);
    removeFromHashTable(t, 3);
    printHashTable(t);
    putIntoHashTable(t, 2, 2);
    putIntoHashTable(t, 13, 13);
    putIntoHashTable(t, 24, 24);
    printHashTable(t);
    removeFromHashTable(t, 2);
    printHashTable(t);
    node = getFromHashTable(t, 13);
    printf("key %d values %d\n", node->key, node->value);
    node = getFromHashTable(t, 24);
    printf("key %d values %d\n", node->key, node->value);
    putIntoHashTable(t, 35, 35);
    printHashTable(t);
    freeHashTable(t);
    return 0;
}