mọi người chú ý dùm em vào 2 hàm chính là <add()> của class hash và hàm <getelem()> của class node ạ. Không hiểu sao cứ gọi hàm thông qua thì node nó lại không gán được giá trị
#ifndef NODE_CPP
#define NODE_CPP 1
#include"iostream"
using namespace std;
template <class k,class t>
class node{
private:
k key;
t elem;
node* next;
public:
node();
node* getnext(); //tra lai dia chi cua doi tuong do
void setnext(node<k,t>* p);
t getelem(); //tra lai dia chi cua phantu luu trong node;
k getkey();
void setelem(t e); //dat thay the phan tu luu tru trong node
void setkey(k key);
};
//----------------------------------------------------------------------
template<class k,class t>
node<k,t>::node(){
next=NULL;
}
//----------------------------------------------------------------------
template <class k,class t>
node<k,t> *node<k,t>::getnext(){
return next;
}
//----------------------------------------------------------------------
template <class k,class t>
void node<k,t>::setnext(node<k,t> *p){
next=p;
}
//----------------------------------------------------------------------
template<class k,class t>
t node<k,t>::getelem(){
return elem;
}
//----------------------------------------------------------------------
template<class k,class t>
k node<k,t>::getkey(){
return key;
}
template <class k,class t>
void node<k,t>::setelem(t e){
this->elem=e;
}
template <class k,class t>
void node<k,t>::setkey(k key){
this->key=key;
}
#endif
#include"iostream"
#include"slist.cpp"
using namespace std;
template<class k,class t>
class hash{
private:
singlelist<k,t>* a;
int N;
public:
hash(){
N=26;
a=new singlelist<k,t>[N];
}
~hash(){
for(int i=0;i<26;i++){
node<k,t> *pre=a[i].first();
while(pre!=NULL){
node<k,t> *temp=pre;
pre=pre->getnext();
delete temp;
}
}
delete a;
}
int hashfunc(k key){
cout<<"hashfunc";
return key%26;
}
void add(k key,t val);
void remove(k key);
singlelist<k,t> getlist(int i){
return a[i];
}
node<k,t>* find(k key);
bool contains(k key);
int count(int i){return a[i].size();};
};
//=======================================================
template<class k,class t>
void hash<k,t>::add(k key,t e){
node<k,t> *p;
cout<<"them tu";
p->setelem(e);
cout<<"setrlem";
p->setkey(key);
cout<<"setup";
int index=hashfunc(key);
cout<<index;
a[index].insertlast(p->getelem());
cout<<"ok";
}
template<class k,class t>
void hash<k,t>::remove(k key){
int index=hashfunc(key);
a[index]->first()=NULL;
a[index]->last()=NULL;
}
template<class k,class t>
node<k,t>* hash<k,t>::find(k key){
int index=hashfunc(key);
if(index<=26&&index>=1)
return a[index].first();
return NULL;
}
template <class k,class t>
bool hash<k,t>::contains(k key)
{
for(int i=0;i<26;i++)
if(key==a[i].first()->getkey())
return true;
return false;
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?