[ create a new paste ] login | about

Link: http://codepad.org/b03NxST2    [ raw code | output | fork ]

C++, pasted on Mar 4:
#include <stdio.h>

struct Item {
    Item *next;
    int key;
    int something;
};

struct ItemList {
    Item *first;
};

const int NUM_BUCKETS = 3;

ItemList keys[NUM_BUCKETS];

Item *addItemKey( int key, Item *o ) {
    int index = key % NUM_BUCKETS;
    if( keys[index].first != NULL ) {
        o->next = keys[index].first;
    }
    o->key = key;
    keys[index].first = o;
    return o;
}
Item *findItemKey( int key ) {
    int index = key % NUM_BUCKETS;
    Item *test = keys[index].first;
    while( test != NULL ) {
        if( test->key == key ) {
            return test;
        }
        test = test->next;
    }
    return NULL;
}

int main( ) {
    Item *myItem1 = addItemKey( 10, new Item( ) );
    myItem1->something = 7;
    Item *myItem2 = addItemKey( 5, new Item( ) );
    myItem2->something = 3;
    
    Item *myFoundItem = findItemKey( 10 );
    if( myFoundItem != NULL ) {
        cout << myFoundItem->something << endl; // should say 7 with the code above
    }
}


Output:
1
7


Create a new paste based on this one


Comments: