해시 세트를 구현하려고합니다. 하지만 검색 기능과 추가 기능이 제대로 작동하지 않는 것 같습니다. 추가 기능을 사용하면 Person (phoneNumber 및 name을 가짐)을 추가 할 수 있지만 두 사람이 충돌하는 경우 무언가가 잘 진행되지 않습니다. 충돌은 별도의 연결을 통해 해결되며 링크 된 목록의 끝에 사람이 추가됩니다. 나는 검색 기능에 대한 단언을 사용하여 몇 가지 테스트를했다. 요소가 세트에 존재하면 문제는 없지만 그것이 존재하지 않으면 어설 션이 실패합니다. 여기 내 코드는 : 어디에서 잘못하고있는 중이 야해시 세트 추가 및 검색 기능이 제대로 작동하지 않습니다. - C++
#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_ */
? 당신은 while 루프를 종료하면
Node* cursor = head[index];
while (cursor != NULL)
{
cursor = cursor->getNext();
}
addNode->setNext(cursor->getNext());
이 cursor
가 null :
메서드의 멤버에 액세스하려면 this->를 사용할 필요가 없습니다. –
@brianbeuning 나는 그것을 얻지 못한다. 내가 사용할 수있는 곳 -> 내가 할 수있는 곳 –
@MonsterMonster 그는 당신의 생성자에서'this->'를 언급하고있다. 'this->'는 필요 없습니다. –