#include
#include
#define MAX 13
using namespace std;
template
struct Node
{
Type *data;
Node *next;
Node(const Type *str = ) :data(new Type[strlen(str) + 1]), next(NULL)
{
strcpy(data,str);
}
};
template
struct Mnode
{
Node *adj;
int count;//計數。
Mnode() :adj(NULL), count(0){}
};
template
class Hash
{
public:
Hash(){}
long hash(const char *str)
{
const char *p = str;
long nhash=0;
while (*p)
nhash = (nhash << 5) + nhash + *p++;
return nhash;
//Times33算法。
}
void Insert(const char *str)
{
Node *s = new Node(str);
long key = hash(str)%MAX;
Node *p = node[key].adj;
Node *pr = NULL;
while (p != NULL)
{
pr = p;
p = p->next;
}
if (pr == NULL)
{
node[key].adj = s;
}
else
{
s->next = p;
pr->next = s;
}
node[key].count++;
}
void Printf()
{
int i = 0;
Node *p = NULL;
for (; i < MAX; i++)
{
if (node[i].count>0)
{
p = node[i].adj;
while (p != NULL)
{
cout << p->data << ;
p = p->next;
}
cout << endl;
}
}
}
private:
Mnode node[MAX];
};
int main()
{
Hash sh;
sh.Insert(liuuiyan);
sh.Insert(benleng);
sh.Insert(huang);
sh.Printf();
return 0;
}