Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255413
  • 博文数量: 164
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1129
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 21:55
文章分类

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Java

2015-05-09 10:50:31



点击(此处)折叠或打开

  1. /**
  2.  * 员工信息类
  3.  * @author Administrator
  4.  *
  5.  */
  6. public class Info {
  7.     private String key;
  8.     private String name;
  9.     
  10.     public Info(String key, String name) {
  11.         this.key = key;
  12.         this.name = name;
  13.     }

  14.     public String getKey() {
  15.         return key;
  16.     }

  17.     public void setKey(String key) {
  18.         this.key = key;
  19.     }

  20.     public String getName() {
  21.         return name;
  22.     }

  23.     public void setName(String name) {
  24.         this.name = name;
  25.     }
  26.     
  27.     
  28. }

点击(此处)折叠或打开

  1. /*
  2.  * 链结点,相当于是车厢
  3.  */
  4. public class Node {
  5.     //数据域
  6.     public Info info;
  7.     //指针域
  8.     public Node next;
  9.     
  10.     public Node(Info info) {
  11.         this.info = info;
  12.     }
  13.     
  14. }


点击(此处)折叠或打开

  1. public class LinkList {
  2.     //头结点
  3.     private Node first;
  4.     
  5.     public LinkList() {
  6.         first = null;
  7.     }
  8.     
  9.     /**
  10.      * 插入一个结点,在头结点后进行插入
  11.      */
  12.     public void insertFirst(Info info) {
  13.         Node node = new Node(info);
  14.         node.next = first;
  15.         first = node;
  16.     }
  17.     
  18.     /**
  19.      * 删除一个结点,在头结点后进行删除
  20.      */
  21.     public Node deleteFirst() {
  22.         Node tmp = first;
  23.         first = tmp.next;
  24.         return tmp;
  25.     }
  26.     
  27.     
  28.     /**
  29.      * 查找方法
  30.      */
  31.     public Node find(String key) {
  32.         Node current = first;
  33.         while(!key.equals(current.info.getKey())) {
  34.             if(current.next == null) {
  35.                 return null;
  36.             }
  37.             current = current.next;
  38.         }
  39.         return current;
  40.     }
  41.     
  42.     /**
  43.      * 删除方法,根据数据域来进行删除
  44.      */
  45.     public Node delete(String key) {
  46.         Node current = first;
  47.         Node previous = first;
  48.         while(!key.equals(current.info.getKey())) {
  49.             if(current.next == null) {
  50.                 return null;
  51.             }
  52.             previous = current;
  53.             current = current.next;
  54.         }
  55.         
  56.         if(current == first) {
  57.             first = first.next;
  58.         } else {
  59.             previous.next = current.next;
  60.         }
  61.         return current;
  62.         
  63.     }
  64. }


点击(此处)折叠或打开

  1. public class HashTable {
  2.     private LinkList[] arr;
  3.     
  4.     /**
  5.      * 默认的构造方法
  6.      */
  7.     public HashTable() {
  8.         arr = new LinkList[100];
  9.     }
  10.     
  11.     /**
  12.      * 指定数组初始化大小
  13.      */
  14.     public HashTable(int maxSize) {
  15.         arr = new LinkList[maxSize];
  16.     }
  17.     
  18.     /**
  19.      * 插入数据
  20.      */
  21.     public void insert(Info info) {
  22.         //获得关键字
  23.         String key = info.getKey();
  24.         //关键字所自定的哈希数
  25.         int hashVal = hashCode(key);
  26.         if(arr[hashVal] == null) {
  27.             arr[hashVal] = new LinkList();
  28.         }
  29.         arr[hashVal].insertFirst(info);
  30.     }
  31.     
  32.     /**
  33.      * 查找数据
  34.      */
  35.     public Info find(String key) {
  36.         int hashVal = hashCode(key);
  37.         return arr[hashVal].find(key).info;
  38.     }
  39.     
  40.     /**
  41.      * 删除数据
  42.      * @param key
  43.      * @return
  44.      */
  45.     public Info delete(String key) {
  46.         int hashVal = hashCode(key);
  47.         return arr[hashVal].delete(key).info;
  48.     }
  49.     
  50.     public int hashCode(String key) {
  51. //        int hashVal = 0;
  52. //        for(int i = key.length() - 1; i >= 0; i--) {
  53. //            int letter = key.charAt(i) - 96;
  54. //            hashVal += letter;
  55. //        }
  56. //        return hashVal;
  57.         
  58.         BigInteger hashVal = new BigInteger("0");
  59.         BigInteger pow27 = new BigInteger("1");
  60.         for(int i = key.length() - 1; i >= 0; i--) {
  61.             int letter = key.charAt(i) - 96;
  62.             BigInteger letterB = new BigInteger(String.valueOf(letter));
  63.             hashVal = hashVal.add(letterB.multiply(pow27));
  64.             pow27 = pow27.multiply(new BigInteger(String.valueOf(27)));
  65.         }
  66.         return hashVal.mod(new BigInteger(String.valueOf(arr.length))).intValue();
  67.     }
  68. }




点击(此处)折叠或打开

  1. public class TestHashTable {
  2.     public static void main(String[] args) {
  3.         HashTable ht = new HashTable();
  4.         ht.insert(new Info("a","张三"));
  5.         ht.insert(new Info("ct","李四"));
  6.         ht.insert(new Info("b","王五"));
  7.         ht.insert(new Info("dt","赵柳"));
  8.         
  9.         System.out.println(ht.find("a").getName());
  10.         System.out.println(ht.find("ct").getName());
  11.         System.out.println(ht.find("b").getName());
  12.         System.out.println(ht.find("dt").getName());
  13.         
  14. //        System.out.println(ht.hashCode("a"));
  15. //        System.out.println(ht.hashCode("ct"));
  16.         
  17.    

  18.     }
  19. }


运行结果:
张三
李四
王五
赵柳
张三



阅读(1604) | 评论(0) | 转发(0) |
0

上一篇:阻塞队列

下一篇:Java排序算法:冒泡排序

给主人留下些什么吧!~~