Hash set add and search functions don't work properly - c++ -


i'm trying implement hash set. seems search function , add function don't work properly. add function lets me add person(which has phonenumber , name) when 2 persons in collision, something's not going well. collisions solved through separate chaining , persons added @ end of linked lists. made tests using assertions on search function. if element exists in set, there's no problem if doesn't exist, assertions fails. here's code:

#ifndef set_h_ #define set_h_  //#include "person.h"  class set{ private:     class node{         private:             person info;             node* next;         public:             node(){                 this->next=null;             }             node(person info, node* next){                 this->info=info;                 this->next=next;             }             node(const node& node){                 this->info=node.info;                 this->next=node.next;             }             ~node(){}             person getinfo(){                 return this->info;             }             node* getnext(){                 return this->next;             }             void setnext(node* value){                 this->next=value;             }             void setinfo(person el){                 this->info=el;             }         };     node** head;     int size;     int* bucketsize;     int totalelements; public:     set();     ~set();     int hashfunction(long long int);     bool isemptyatindex(int index);     bool isempty();     bool search(person e);     void add(person e);      int totalelementsintheset(){         return this->totalelements;     }     int hashsize(){         return this->size;     }     int bucketnumberofelements(int index){         return this->bucketsize[index];     } };  set::set(){     this->size=11;     this->head = new set::node*[this->size];     this->bucketsize= new int[this->size];     for(int i=0; < this->size; i++){         this->head[i]=null;         this->bucketsize[i]=0;     }     totalelements = 0; }  set::~set(){     delete[] head;     delete[] bucketsize; }   int set::hashfunction(long long int nr){     int sum=0;     int divisor=10;     while(nr != 0){         sum+=nr % divisor;         nr=nr / divisor;     }     int hashcode = sum % size;     return hashcode; }  bool set::isempty(){     if(totalelements==0){         return true;     }     return false; }  void set::add(person p){     if(search(p)==false){         int index = hashfunction(p.getphonenumber());         node* addnode = new set::node(p,null);         if(head[index]==null){             head[index]=addnode;             ++totalelements;             ++bucketsize[index];         }         else{             node* cursor = head[index];             while(cursor != null){                 cursor = cursor->getnext();             }             addnode->setnext(cursor->getnext());             cursor->setnext(addnode);             ++totalelements;             ++bucketsize[index];         }     }     else{         cout<<"there's person given phone number!";     }  }  bool set::search(person p){     int index = hashfunction(p.getphonenumber());     if(head[index]==null){         return false;     }     else{         node* cursor = head[index];         while((cursor->getinfo().getphonenumber() != p.getphonenumber()) , cursor != null){             cursor = cursor->getnext();         }         if(cursor->getinfo().getphonenumber()== p.getphonenumber()){             return true;         }     }     return false; }  #endif /* set_h_ */ 

where doing wrong?

here's problem:

node* cursor = head[index]; while (cursor != null) {     cursor = cursor->getnext(); } addnode->setnext(cursor->getnext()); 

when exit while loop, cursor null. therefore, cursor->getnext() produce access violation.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -