一个头结点数组,散列后的元素插入数组元素后的链表中。
#ifndef listAddress_H_
#define listAddress_H_
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
class hashTable
{
private:
int size;
node*table;
public:
hashTable();
~hashTable();
void insert(int elem);
void deletex(int elem);
bool search(int elem);
void print();
};
hashTable::hashTable()
{
cout<<"please input the size of hashTable.\n";
cin>>size;
table= new node[size];
for(int i=0; i<size; i++)
{
table[i].data=i;
table[i].next=NULL;
}
}
hashTable::~hashTable()
{
delete []table;
}
void hashTable::insert(int elem)
{
int index=elem%size;
node*cur=&table[index];
while(cur->next)
{
cur=cur->next;
}
cur->next=new node;
cur->next->data=elem;
cur->next->next=NULL;