博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
散列 - 链表散列实现字典
阅读量:4588 次
发布时间:2019-06-09

本文共 2269 字,大约阅读时间需要 7 分钟。

hashlist.h

/********************************************************************    purpose:    链表散列    author:     xianyun1230    QQ:         836663997    e-mail:     xianyun1230@163.com    created:    2014/02/23*********************************************************************/template
class 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;}

转载于:https://www.cnblogs.com/xyyh/p/3980309.html

你可能感兴趣的文章
python之路
查看>>
【leetcode❤python】 219. Contains Duplicate II
查看>>
Moss/sharepoint 匿名访问之看例做题
查看>>
[C# 基础知识系列]专题十六:Linq介绍
查看>>
Ubuntu10下MySQL搭建Amoeba_基础
查看>>
react typescript 父组件调用子组件
查看>>
[Android]Service总结1
查看>>
Super关键字
查看>>
ATG Form Handler 最佳实践
查看>>
linux | 管道符、输出重定向
查看>>
自定义MVC HtmlHelpe之分页
查看>>
WEB程序开发基础-作业3-WEB(JSP)下的JDBC操作
查看>>
為什麼擠出頭的喉管需要散熱?
查看>>
1036. 跟奥巴马一起编程(15)
查看>>
ssdb常用知识点
查看>>
新闻cms管理系统 (补)-----路由优化一
查看>>
nginx配置文件详解
查看>>
网络攻防第一次实验
查看>>
js-ajax-02
查看>>
统计机器学习
查看>>