hashlist.h
/******************************************************************** purpose: 链表散列 author: xianyun1230 QQ: 836663997 e-mail: xianyun1230@163.com created: 2014/02/23*********************************************************************/templateclass hashlist{ public: hashlist(int num); ~hashlist(); bool search(const K k, T& val); bool insert(const K k, T val); bool del(const K k); void show()const; private: typedef struct _node { K key; T data; struct _node *next; }node; node* hsearch(K k); int D; node **lst;};template hashlist ::hashlist(int num){ D = num; lst = new node *[D]; for (int i = 0; i < D; i++) { lst[i] = new node; lst[i]->next = NULL; }}template hashlist ::~hashlist(){ node *tmp, *ph; for (int i = 0; i < D; ++i) { tmp = lst[i]->next; while (tmp) { ph = tmp->next; delete tmp; tmp = ph; } delete lst[i]; } delete lst;}template hashlist ::node* hashlist ::hsearch(K k){ node *tmp = lst[k % D]; while(tmp->next) { if (tmp->next->key == k) return tmp; tmp = tmp->next; } return tmp;}template bool hashlist ::search(const K k, T& val){ node* pt = hsearch(k); if (pt->next == NULL) return false; if (pt->next->key == k) { val = pt->next->data; return true; } else return false;}template bool hashlist ::insert(const K k,T val){ node* pt = hsearch(k); if (pt) { node* tmp = new node; tmp->key = k; tmp->data = val; tmp->next = pt->next; pt->next = tmp; return true; } return false;}template bool hashlist ::del(const K k){ node* pt = hsearch(k); if (pt->next != NULL) { node* tmp = pt->next; pt->next = tmp->next; delete tmp; if (pt == lst[k%D]) pt->next = NULL; return true; } return false;}template void hashlist ::show() const{ node* tmp; for (int i = 0; i < D; ++i) { tmp = lst[i]->next; while (tmp) { std::cout < data <<" "; tmp = tmp->next; } } std::cout <
main.cpp
#include#include #include "hashlist.h"int main(){ hashlist a(12); a.insert(12,"abc"); a.insert(2,"skldjf"); a.insert(2,"skl"); a.insert(2,"skldj"); a.show(); a.del(12); a.del(23); a.show(); return 0;}