diff --git a/12.c b/12.c new file mode 100644 index 0000000..d7b8475 --- /dev/null +++ b/12.c @@ -0,0 +1,48 @@ +#include +#include + +typedef struct Item item; +struct Item { + unsigned int key; + int value; +}; + +unsigned int hash(char *x, unsigned int n) { + const unsigned int offset = 2166136261; + unsigned int hash = 0; + + for (unsigned int i = 0; i < n; x++, i++) { + hash *= offset; + hash ^= (*x); + } + + return hash; +} + +int search(item items[], int n, char *x, int xn) { + unsigned int key = hash(x, xn); + for (int i = 0; i < n; i++) { + if (items[i].key == key) + return items[i].value; + } + + printf("Not found"); + exit(1); +} + +int main() { + int const SIZE = 4; + item items[SIZE]; + items[0].key = hash("nita", 4); + items[0].value = 99; + items[1].key = hash("amaang", 5); + items[1].value = -129; + items[2].key = hash("unreal", 6); + items[2].value = 4; + items[3].key = hash("alpaviraam", 10); + items[3].value = 10000; + + char x[] = "unreal"; + + printf("Value for string %s is: %d", x, search(items, SIZE, x, 6)); +} diff --git a/12.png b/12.png new file mode 100644 index 0000000..4fb4c96 Binary files /dev/null and b/12.png differ