Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #define EMPTY 0
- #define EMPTY_DEL 1
- #define FULL 2
- int hash(int value) {
- return value % 11;
- }
- int hash2(int value) {
- return 1 + value % 10;
- }
- //ubacuje *value* u hash tabelu
- void add(int *table, int *table_indicators, int size, int value) {
- if(hash(value) >= size || hash(value) < 0) {
- printf("SOMETHING VERY WRONG\n");
- exit(1);
- }
- int idx = hash(value);
- for(int i=0; i<size; i++) {
- int idx2 = (idx+i*hash2(value))%size;
- if(table_indicators[idx2] == EMPTY || table_indicators[idx2] == EMPTY_DEL) {
- table[idx2] = value;
- table_indicators[idx2] = FULL;
- return;
- }
- }
- printf("NO MORE SPACE\n");
- exit(1);
- }
- //vraca index iz hash tabele za vrednost *value*
- int find(int *table, int *table_indicators, int size, int value) {
- if(hash(value) >= size || hash(value) < 0) {
- printf("SOMETHING VERY WRONG\n");
- exit(1);
- }
- int idx = hash(value);
- for(int i=0; i<size; i++) {
- int idx2 = (idx+i*hash2(value))%size;
- if(table_indicators[idx2] == EMPTY) {
- printf("NOT FOUND\n");
- exit(1);
- }
- if(table_indicators[idx2] == EMPTY_DEL)
- continue;
- if(table[idx2] == value)
- return idx2;
- }
- printf("NOT FOUND\n");
- exit(1);
- }
- //brise element iz hash tabele po vrednosti *value*
- void remove(int *table, int *table_indicators, int size, int value) {
- int idx = find(table, table_indicators, size, value);
- table_indicators[idx] = EMPTY_DEL;
- return;
- }
- int main(int argc, char **argv) {
- int size = 11;
- int *table_indicators = (int*)malloc(sizeof(int) * size);
- for(int i=0; i<size; i++)
- table_indicators[i] = EMPTY;
- int *table = (int*)malloc(sizeof(int) * size);
- for(int i=1; i<argc; i++) {
- add(table, table_indicators, size,
- strtol(argv[i], NULL, 10));
- }
- for(int i=0; i<size; i++)
- if(table_indicators[i] == FULL)
- printf("%d : %d\n", i, table[i]);
- printf("Remove?\n");
- int input = -1;
- while(input != 0) {
- scanf("%d", &input);
- if(input == 0) break;
- remove(table, table_indicators, size, input);
- }
- printf("Add?\n");
- input = -1;
- while(input != 0) {
- scanf("%d", &input);
- if(input == 0) break;
- add(table, table_indicators, size, input);
- }
- for(int i=0; i<size; i++)
- if(table_indicators[i] == FULL)
- printf("%d : %d\n", i, table[i]);
- free(table);
- free(table_indicators);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement