Hashing - How to find collision rate?
Jan 14, 2013 at 1:16am UTC
Guys, I'm having issues calculating the collision rate of this table. It's currently printing out values same as the number of elements inserted. Any ideas?
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
#include <iostream>
#include <cstdlib>
#include "Question_3.hpp"
#include <stdint.h>
int collision = 0;
int hash(int num) {
int key;
key = num % 10;
return key;
}
list::list() {
first = last = cur = prev = NULL;
}
void list::insert(long long int a) {
//No collision occurance
if (first = NULL) {
first = new Node;
first->data = a;
first->next = NULL;
last = first;
}
else {
Node* newNode = new Node;
newNode->data = a;
newNode-> next = NULL;
last = first;
collision++;
}
}
void list::display() {
cur = first;
while (cur != NULL) {
cout << " " << cur -> data << " " ;
cur = cur-> next;
}
cout << endl;
}
int main()
{
int numInserted;
int hashvalue;
list table[10];
cout << "Please enter 10 elements after each element\n" ;
for (int i = 0; i < 20; i++) {
cout << "Element " << i + 1 << " -> " ;
numInserted = rand()%10;
cout << numInserted << endl;
//cin >> numInserted;
table[hash(numInserted)].insert(numInserted);
}
system("pause" );
cout << endl;
for (int k = 0; k<10; k++) {
cout << "Bucket [" << k << "]" ;
table[k].display();
}
cout << "Collsion rate: " << collision << endl;
return 0;
Jan 14, 2013 at 4:03am UTC
Line 22 should be using ==, not =.
Jan 14, 2013 at 4:06am UTC
Thanks. That solved the problem :)
Topic archived. No new replies allowed.