public class ChainingHash<Key,Value>{
private int N;
private int M;
private doublylinked<Key,Value>[] s;
public ChainingHash(int M){
this.M = M;
s = new doublylinked [M];
for(int i=0;i<M;i++){
s[i] = new doublylinked();
}
} public int hash(Key key){
return (key.hashCode() & 0x7fffffff)%M;
} private void put(Key key,Value val){
s[hash(key)].put(key, val);
} private Value get(Key key){
return s[hash(key)].get(key);
} private void delet(Key key){
s[hash(key)].delet(key);
} public void print(){
for (int i = 0;i<M;i++){
s[i].print();
System.out.println(" ");
}
} public class doublylinked<Key,Value>{
private Node first;
public doublylinked(){
first = null;
}
private class Node{
Key key;
Value val;
Node next,last;
public Node(Key key,Value val,Node next,Node last){
this.key = key;this.val = val;this.next = next; this.last = last;
}
}
public void put(Key key,Value val){
for(Node x = first;x!= null;x= x.next){
if(key.equals(x.key)){
x.val = val;
return;
}
}
if(first == null){
first = new Node(key,val,null,null);
}
else{
first.last = new Node(key,val,first,null);
first = first.last;
}
} public Value get(Key key){
for(Node x = first;x!= null;x= x.next){
if(key.equals(x.key)){
return x.val;
}
}
return null; } public void delet(Key key){
for(Node x = first;x!= null;x= x.next){
if(key.equals(x.key)){
x.last.next = x.next;
return;
}
}
return;
} public void print(){
int i = 0;
if(first == null){
System.out.println(" ");
return;
}
for(Node x = first;x!= null;x= x.next){
i++;
System.out.println(x.key+" "+x.val );
} } }
public static void main(String[] args) {
ChainingHash<String, Integer> st = new ChainingHash<String, Integer>(517);
int N = args.length;
for (int i = 0;i < N; i++) {
String key = args[i];
st.put(key, i);
} // print keys
st.print();
} }

ChainingHash的更多相关文章

随机推荐

  1. 转-【exp/imp】将US7ASCII字符集的dmp文件导入到ZHS16GBK字符集的数据库中

    原帖地址:http://blog.csdn.net/lihuarongaini/article/details/71512116 1.2  前言部分 1.2.1  导读和注意事项 各位技术爱好者,看完 ...

  2. python爬虫---BeautifulSoup的用法

    BeautifulSoup是一个灵活的网页解析库,不需要编写正则表达式即可提取有效信息. 推荐使用lxml作为解析器,因为效率更高. 在Python2.7.3之前的版本和Python3中3.2.2之前 ...

  3. 前端基础之html常用标签

    前言: 1.在B-S模式下,server服务端和客户端之间 使用http协议(规定 客户端应该怎么请求服务端,服务端应该怎么响应)通信: 2.传输过程 浏览器 向服务端发起 post/get请求 服务 ...

  4. 【转】MVC中code first方式开发,数据库的生成与更新(Ef6)

    一,在models文件夹中,建立相应的model文件         这里注意一点,这里建立的class名,就是数据库里表的名字.         在这里面,可以建立表之间的关系. 这里要说明一点的事 ...

  5. 【转】JQuery插件定义

    一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写("#"),("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人,直 ...

  6. ffmpeg+libmp3lame库源码安装教程(CentOS)

    lame--libmp3lame的安装包,支持MP3编码:yasm--NASM的重写,用于编译ffmpeg. 1.下载 ffmpeg下载链接:http://ffmpeg.org/download.ht ...

  7. java 一些容易忽视的小点-数据类型和运算符篇

    注释 文档注释:   以"/**"开头以"*/"结尾,注释中包含一些说明性的文字及一些JavaDoc标签(后期写项目时,可以生成项目的API) 行注释:   以 ...

  8. Java代理:静态代理、动态代理

    要理解动态代理,需要先理解反射(http://www.cnblogs.com/Donnnnnn/p/7729443.html) 通俗理解: 在很多底层框架中都会用得到,比如struts,Spring等 ...

  9. C++ string类insert用法总结

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  10. Sql Server 中 根据列名查询表名

    已知列名 ELEMENT_ID ,查询所属表名称 Select O.name objectName, C.name ColumnName from sys.columns C inner join s ...