/** author: Ivan Cukic version: 0.1d */ #include #include #define HASH_COUNT 50 #define MAX_ARRAY_LENGTH 500 struct fenode { struct fenode *child; int key, num; // key - index u nizu, num - vrednost u nizu } *hash[HASH_COUNT]; // bice indexirano na osnovu num-a, a ne key-a int arrayCount[MAX_ARRAY_LENGTH]; // ovde se pamti broj pojavljivanja u datoteci int init() { // inicijalizacija pocetnih vrednosti za promenljive for (int i = 0; i < MAX_ARRAY_LENGTH; i++) arrayCount[i] = 0; for (int i = 0; i < HASH_COUNT; i++) hash[i] = 0; } int fehash(int key) { return key % HASH_COUNT; } int addElement(int key, int num) { int h = fehash(num); struct fenode *nn, *root, *oroot; nn = (struct fenode *)malloc(sizeof(*nn)); nn->key = key; nn->num = num; nn->child = 0; if (hash[h] == 0) { hash[h] = nn; } else { //if (nn->num <= hash[h]->num) { nn->child = hash[h]; hash[h] = nn; /*} else { root = hash[h]->child; oroot = hash[h]; while (root) { if (nn-> num <= root->num) { oroot->child = nn; nn->child = root; root = 0; } else { oroot = root; root = root->child; } } }*/ } } struct fenode * find(int num) { int i = fehash(num); struct fenode *nn = hash[i]; while (nn) { if (nn->num == num) return nn; nn = nn->child; } return 0; } main () { int i = 0, k; init(); struct fenode *fn; // Ucitavace se niz dok se ne unese nula scanf("%d", &k); while (k) { addElement(i++, k); scanf("%d", &k); } for (int _ = 0; _ < HASH_COUNT; _++) { printf("\n%d----------\n", _); fn = hash[_]; while (fn != 0) { printf("%d %d\n", fn->key, fn->num); fn = fn->child; } } FILE* fin = fopen("ceo.dat", "r"); FILE* fout = fopen("notmached.dat", "w"); if (!fin) return 0; if (!fout) return 0; fscanf(fin, "%d", &k); while (k) { fn = find(k); //printf("O"); if (fn > 0) arrayCount[fn->key]++; else fprintf(fout, "%d\n", k); fscanf(fin, "%d", &k); } fclose(fin); fclose(fout); for(int _ = 0; (_ < MAX_ARRAY_LENGTH); _++) { printf("%d ", arrayCount[_]); } }