哈希表-链地址法
来源:互联网 发布:fedora和centos 编辑:程序博客网 时间:2024/06/18 15:05
链地址法其实就是将实现哈希表的底层数组的类型变成链表,每次插入数据插入到链表中即可
package map;public class Node{ private int data ; public Node next ; public Node ( int data ) { this.data = data ; } public int getData () { return data; } public void setData ( int data ) { this.data = data; }}---------------package map;public class LinkList{ private Node first ;//首节点 private Node current ;//用来记录当前节点 private Node newNode ; //插入数据 public void insert ( int i ) { newNode = new Node ( i ) ; if ( first == null ) { first = newNode ; return ; } newNode.next = first ; first = newNode ; } public void delete ( int value ) { current = first ; Node parent = first ; while ( current != null ) { if ( current.getData () == value ) { parent.next = current.next ; return ; } parent = current ; current = current.next ; } } public Node find ( int value ) { current = first ; while ( current != null && current.getData () != value ) { current = current.next ; } //跳出循环说明找到或者为空 if ( current == null ) { System.out.println ( "null" ); return null ; } else { return current ; } } public void display () { current = first ; while ( current != null ) { System.out.print ( current.getData () + " " ); current = current.next ; } System.out.println ( ); }}-----------------------package map;public class LinkHash{ private LinkList [] arr ; private int size ; private Node newNode ; public LinkHash ( int size ) { this.size = size ; arr = new LinkList [size] ; } public int Hash ( int data ) { return data % arr.length ; } //插入 public void insert ( int data ) { int index = Hash ( data ) ;//计算索引下标 //如果为空,就new一个list进去 if ( arr [index] == null ) { LinkList l = new LinkList () ; l.insert ( data ); arr [index] = l ; } else {//不为空直接入 arr [index].insert ( data ); } } public Node find ( int data ) { int index = Hash ( data ) ; if ( arr [index] == null ) { return null ; } else { Node n = arr [index].find ( data ) ; if ( n == null ) { return null ; } else { return n ; } } } public void delete ( int data ) { int index = Hash ( data ) ; arr [index].delete ( data ); } public void display () { for ( int i = 0 ; i < arr.length ; ++ i ) { if ( arr [i] != null ) arr [i].display (); else System.out.println (); } System.out.println ( "-------------" ); }}
阅读全文
0 0
- 哈希表-链地址法
- 哈希表-链地址法
- 哈希表(链地址法)
- 哈希表之链地址法
- 哈希表应用(链地址法)
- 哈希表模版(链地址表示法)
- (7)哈希表的链地址法实现
- C++链地址法实现哈希表
- SDUTOJ 1480 -- 哈希表(链地址法)
- 链地址处理法构造简单哈希表
- (7)哈希表的链地址法实现
- 哈希表-开发地址法
- 链地址法
- 解决哈希表的冲突-开放地址法和链地址法
- 解决哈希表的冲突-开放地址法和链地址法
- 解决哈希表的冲突-开放地址法和链地址法
- 解决哈希表的冲突-开放地址法和链地址法
- NOJ1073成绩排名&链地址法
- Python之学习总结第一天
- UART中的硬件流控RTS/CTS和软件流XON/XOFF
- 切换城市之根据ip获取城市名称--淘宝
- JDBC(1) 简单的连接数据库和封装
- 嵌智捷达科技产品CM-AM335X
- 哈希表-链地址法
- Node.js
- 记录Mac 下反编译apk 步骤
- 【洛谷3467】[POI2008]海报PLA-Postering(单调栈)
- 判断浏览器内核
- 分治---最接近点对问题
- RFID-上位机软件界面设计
- axios Post无法传递数据的解决方案
- AngularJS 的作用