1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #include "linear.h"
HashTable *initHashTable(int *input, int len) { HashTable *hashTable = (HashTable *)malloc(sizeof(HashTable)); hashTable->hash = (int *)malloc(N * sizeof(int)); hashTable->aslSuccess = (int *)malloc(N * sizeof(int)); hashTable->length = len;
memset(hashTable->hash, 0, N * sizeof(int)); memset(hashTable->aslSuccess, 0, N * sizeof(int));
for (int i = 0; i < len; i++) { insertVal(hashTable, *(input + i)); }
return hashTable; }
void insertVal(HashTable *hashTable, int x) { int pos = x % MOD; int successCount = 1; while (hashTable->hash[pos] && hashTable->hash[pos] != -1) { pos = (pos + 1) % N; successCount++; } hashTable->hash[pos] = x; hashTable->aslSuccess[pos] = successCount; }
void quadraticInsert(HashTable *hashTable, int x) { int step[] = {1, -1, 4, -4, 9, -9}; int indexStep = 0; int initPos = x % MOD;
int pos = initPos; int successCount = 1; while (hashTable->hash[pos] != 0 && hashTable->hash[pos] != -1) { pos = (initPos + step[indexStep++]) % N; successCount++; } hashTable->hash[pos] = x; hashTable->aslSuccess[pos] = successCount; }
int quadraticSearch(HashTable *hashTable, int x) { int step[] = {1, -1, 4, -4, 9, -9}; int indexStep = 0; int pos = x % MOD; while (hashTable->hash[pos] != x && hashTable->hash[pos] != -1 && hashTable->hash[pos] != 0) { pos = (pos + step[indexStep++]) % N; } return hashTable->hash[pos] == x ? pos : -1; }
int search(HashTable *hashTable, int x) { int pos = x % MOD; while (hashTable->hash[pos] != x && hashTable->hash[pos] != 0) { pos = (pos + 1) % N; } return hashTable->hash[pos] == x ? pos : -1; }
int deleteVal(HashTable *hashTable, int x) { int pos = search(hashTable, x); if (pos == -1) return -1; hashTable->hash[pos] = -1; return pos; }
void getASLSuccess(HashTable *hashTable) {
int sum = 0; int howManyPlace = 0; for (int i = 0; i < N; i++) { if (hashTable->hash[i] == 0 || hashTable->hash[i] == -1) continue; sum += hashTable->aslSuccess[i]; howManyPlace++; } printf("%d/%d\n", sum, howManyPlace); }
void getASLFail(HashTable *hashTable) { int ASLFail[N]; memset(ASLFail, 1, N); int count = 1; for (int i = 0; i < MOD; i++) { if (hashTable->hash[i] == 0) { ASLFail[i] = 1; count = 1; continue; } if (count == 1) { int j = i; while (hashTable->hash[j] != 0) { count++; j = (j + 1) % N; } ASLFail[i] = count; count--; } else { ASLFail[i] = count; count--; } }
int sum = 0; for (int i = 0; i < MOD; i++) { sum += ASLFail[i]; } printf("%d/%d\n", sum, MOD); }
void printHashTable(HashTable *hashTable) { for (int i = 0; i < N; i++) printf("%d\t", i); puts(""); for (int i = 0; i < N; i++) { printf("%d\t", hashTable->hash[i]); } puts(""); }
|