Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- using namespace std;
- enum CollisionHashFunction{
- linear,
- classic
- };
- class HashTable{
- public:
- CollisionHashFunction mode;
- int array[997];
- bool insert(int);
- int hashFunction(int);
- int hashFunctionOnCollision(int);
- bool remove(int);
- HashTable();
- void display(int,int);
- bool insertBulk(int);
- int find(int);
- };
- int HashTable::find(int key){
- int value = hashFunction(key);
- while(array[value]!=0 && array[value]!=key){
- if(mode==linear){
- value++;
- }
- if(mode==classic){
- value+=hashFunctionOnCollision(key);
- }
- }
- return value;
- }
- //int HashTable::find(int key, int incr){
- // int value = hashFunction(key);
- //
- // while(array[value]!=0 && array[value]!=key){
- // value+=incr;
- // }
- // return value;
- //}
- bool HashTable::insertBulk(int n){
- int counter = 1;
- while(counter<=n){
- if(insert((rand()%20000)+20001)){
- counter++;
- }
- }
- }
- void HashTable::display(int start, int stop){
- for(int i=start;i<stop+1;i++){
- cout << array[i] << " ";
- }
- }
- bool HashTable::insert(int key){
- int value = hashFunction(key);
- while(array[value]!=0){
- if(mode==linear){
- value++;
- }
- if(mode==classic){
- value+=hashFunctionOnCollision(key);
- }
- }
- array[value]=key;
- return true;
- }
- bool HashTable::remove(int key) {
- int index = find(key);
- if(array[index]==key){
- array[index]=-1;
- return true;
- }
- return false;
- }
- int HashTable::hashFunctionOnCollision(int key){
- return ((3*key) %19)+1;
- }
- int HashTable::hashFunction(int key) {
- return ((key %1000)+2^(key%10)+1)%997;
- }
- HashTable::HashTable(){
- for(int i=0;i<997;i++){
- array[i]=0;
- }
- }
- int main() {
- srand(time(NULL));
- clock_t begin, end;
- double time_spent;
- FILE* fp = fopen("inlab05.txt", "r");
- int X;
- int k1, k2, k3, k4;
- if (fp == NULL)
- return -1;
- fscanf (fp, "%d %d %d %d %d", &X ,&k1, &k2, &k3, &k4);
- fclose(fp);
- begin = clock();
- HashTable* ht = new HashTable();
- ht->mode = classic;
- ht->remove(k1);
- ht->insert(k1);
- ht->display(0,100);
- ht->insertBulk(X);
- ht->display(0,100);
- ht->insert(k2);
- ht->insert(k3);
- ht->insert(k4);
- ht->display(0,100);
- ht->display(500,600);
- end = clock();
- time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- cout << endl << to_string(time_spent);
- begin = clock();
- ht = new HashTable();
- ht->mode = linear;
- ht->remove(k1);
- ht->insert(k1);
- ht->display(0,100);
- ht->insertBulk(X);
- ht->display(0,100);
- ht->insert(k2);
- ht->insert(k3);
- ht->insert(k4);
- ht->display(0,100);
- ht->display(500,600);
- end = clock();
- time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- cout << endl << to_string(time_spent);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement