0

我正在尝试实现一个哈希集。但似乎搜索功能和添加功能无法正常工作。add 函数让我可以添加一个 Person(它有 phoneNumber 和 name),但是当两个 Person 发生冲突时,事情就不顺利了。冲突通过单独的链接解决,并且 Persons 被添加到链表的末尾。我使用搜索功能上的断言进行了一些测试。如果元素存在于集合中,则没有问题,但如果它不存在,则断言失败。这是我的代码:

#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_ */

我在哪里做错了?

4

1 回答 1

3

这是你的问题:

Node* cursor = head[index];
while (cursor != NULL)
{
    cursor = cursor->getNext();
}
addNode->setNext(cursor->getNext());

当您退出 while 循环时,cursor为空。因此,cursor->getNext()会产生访问冲突。

于 2013-05-19T20:44:43.463 回答