我正在尝试实现哈希集。但似乎搜索功能和添加功能无法正常工作。 add函数允许我添加一个Person(具有phoneNumber和name)但当两个人碰撞时,某些东西不顺利。通过单独的链接解决冲突,并在链表的末尾添加人员。我在搜索功能上使用断言进行了一些测试。如果元素存在于集合中,则没有问题,但如果它不存在,则断言失败。这是我的代码:
#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; i < 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 already a person with the 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()) and cursor != NULL){
cursor = cursor->getNext();
}
if(cursor->getInfo().getPhoneNumber()== p.getPhoneNumber()){
return true;
}
}
return false;
}
#endif /* SET_H_ */
我在哪里做错了?
答案 0 :(得分:3)
这是你的问题:
Node* cursor = head[index];
while (cursor != NULL)
{
cursor = cursor->getNext();
}
addNode->setNext(cursor->getNext());
退出while循环时,cursor
为空。因此,cursor->getNext()
会产生访问冲突。