哈希表,开放地址法之线性探测代码(JAVA)

Java代码 复制代码 收藏代码
  1. import java.io.*;
  2. class DataItem { //数据
  3. private int iData; // data item (key)
  4. public DataItem(int ii) {
  5. iData = ii;
  6. }
  7. public int getKey(){
  8. return iData;
  9. }
  10. }
  11. class HashTable{//数组实现的哈希表,开放地址法之线性探测
  12. private DataItem[] hashArray; //存放数据的数组
  13. private int arraySize;
  14. private DataItem nonItem; //用作删除标志
  15. public HashTable(int size) {//构造函数
  16. arraySize = size;
  17. hashArray = new DataItem[arraySize];
  18. nonItem = new DataItem(-1); // deleted item key is -1
  19. }
  20. public void displayTable(){//显示哈希表
  21. System.out.print("Table: ");
  22. for(int j=0; j<arraySize; j++)
  23. {
  24. if(hashArray[j] != null)
  25. System.out.print(hashArray[j].getKey() + " ");
  26. else
  27. System.out.print("** ");
  28. }
  29. System.out.println("");
  30. }
  31. //哈希函数
  32. public int hashFunc(int key)
  33. {
  34. return key % arraySize;
  35. }
  36. //在哈希表中插入数据
  37. public void insert(DataItem item){
  38. int key = item.getKey(); // 获取数据的键值
  39. int hashVal = hashFunc(key); // 计算其哈希值
  40. while(hashArray[hashVal] != null && hashArray[hashVal].getKey() != -1){
  41. ++hashVal; // 插入位置被占,线性探测下一位置
  42. hashVal %= arraySize; // 不让超过数组的大小
  43. }
  44. hashArray[hashVal] = item; // 找到空位后插入
  45. }
  46. //在哈希表中删除
  47. public DataItem delete(int key) {
  48. int hashVal = hashFunc(key); // 计算其哈希值
  49. while(hashArray[hashVal] != null){
  50. if(hashArray[hashVal].getKey() == key){
  51. DataItem temp = hashArray[hashVal]; // 记录已删除的数据
  52. hashArray[hashVal] = nonItem; // 删除它
  53. return temp;
  54. }
  55. ++hashVal; // 到下一单元找
  56. hashVal %= arraySize;
  57. }
  58. return null; // 没有找到要删除的数据
  59. }
  60. //在哈希表中查找
  61. public DataItem find(int key) {
  62. int hashVal = hashFunc(key); //哈希这个键
  63. while(hashArray[hashVal] != null) { // 直到空的单元
  64. if(hashArray[hashVal].getKey() == key)
  65. return hashArray[hashVal]; // 找到
  66. ++hashVal; // 去下一单元找
  67. hashVal %= arraySize; // 不让超过数组的大小
  68. }
  69. return null; // 没有找到
  70. }
  71. }
  72. public class HashTableApp{//测试
  73. public static void main(String[] args) throws IOException {
  74. DataItem aDataItem;
  75. int aKey, size, n, keysPerCell;
  76. System.out.print("Enter size of hash table: ");
  77. size = getInt();
  78. System.out.print("Enter initial number of items: ");
  79. n = getInt();
  80. keysPerCell = 10;
  81. HashTable theHashTable = new HashTable(size);
  82. for(int j=0; j<n; j++){
  83. aKey = (int)(java.lang.Math.random() * keysPerCell * size);
  84. aDataItem = new DataItem(aKey);
  85. theHashTable.insert(aDataItem);
  86. }
  87. while(true){
  88. System.out.print("Enter first letter of ");
  89. System.out.print("show, insert, delete, or find: ");
  90. char choice = getChar();
  91. switch(choice){
  92. case 's':
  93. theHashTable.displayTable();
  94. break;
  95. case 'i':
  96. System.out.print("Enter key value to insert: ");
  97. aKey = getInt();
  98. aDataItem = new DataItem(aKey);
  99. theHashTable.insert(aDataItem);
  100. break;
  101. case 'd':
  102. System.out.print("Enter key value to delete: ");
  103. aKey = getInt();
  104. theHashTable.delete(aKey);
  105. break;
  106. case 'f':
  107. System.out.print("Enter key value to find: ");
  108. aKey = getInt();
  109. aDataItem = theHashTable.find(aKey);
  110. if(aDataItem != null)
  111. {
  112. System.out.println("Found " + aKey);
  113. }
  114. else
  115. System.out.println("Could not find " + aKey);
  116. break;
  117. default:
  118. System.out.print("Invalid entry\n");
  119. }
  120. }
  121. }
  122. public static String getString() throws IOException{
  123. InputStreamReader isr = new InputStreamReader(System.in);
  124. BufferedReader br = new BufferedReader(isr);
  125. String s = br.readLine();
  126. return s;
  127. }
  128. public static char getChar() throws IOException
  129. {
  130. String s = getString();
  131. return s.charAt(0);
  132. }
  133. public static int getInt() throws IOException
  134. {
  135. String s = getString();
  136. return Integer.parseInt(s);
  137. }
  138. }
import java.io.*;
class DataItem { //数据                           
   private int iData;    // data item (key)

   public DataItem(int ii) { 
    iData = ii; 
  }
      public int getKey(){
       return iData; 
   }

   }  

class HashTable{//数组实现的哈希表,开放地址法之线性探测
   private DataItem[] hashArray; //存放数据的数组
   private int arraySize;
   private DataItem nonItem; //用作删除标志

   public HashTable(int size) {//构造函数
      arraySize = size;
      hashArray = new DataItem[arraySize];
      nonItem = new DataItem(-1);   // deleted item key is -1
   }

   public void displayTable(){//显示哈希表
      System.out.print("Table: ");
      for(int j=0; j<arraySize; j++)
         {
         if(hashArray[j] != null)
            System.out.print(hashArray[j].getKey() + " ");
         else
            System.out.print("** ");
         }
      System.out.println("");
      }

   //哈希函数
   public int hashFunc(int key)
      {
      return key % arraySize;      
      }


   //在哈希表中插入数据
   public void insert(DataItem item){
      int key = item.getKey();      // 获取数据的键值
      int hashVal = hashFunc(key);  // 计算其哈希值
                                  
      while(hashArray[hashVal] != null && hashArray[hashVal].getKey() != -1){
         ++hashVal;                 // 插入位置被占,线性探测下一位置
         hashVal %= arraySize;   // 不让超过数组的大小
     }
      hashArray[hashVal] = item;  // 找到空位后插入
   }  

   //在哈希表中删除
   public DataItem delete(int key) {
      int hashVal = hashFunc(key);  // 计算其哈希值

      while(hashArray[hashVal] != null){                             
         if(hashArray[hashVal].getKey() == key){
            DataItem temp = hashArray[hashVal]; // 记录已删除的数据
            hashArray[hashVal] = nonItem;       // 删除它
            return temp;                        
         }
         ++hashVal;  // 到下一单元找
         hashVal %= arraySize;    
      }
      return null;    // 没有找到要删除的数据
      } 

   //在哈希表中查找
   public DataItem find(int key) {
      int hashVal = hashFunc(key);  //哈希这个键

      while(hashArray[hashVal] != null) { // 直到空的单元                     
         if(hashArray[hashVal].getKey() == key)
            return hashArray[hashVal];   // 找到
         ++hashVal;                 // 去下一单元找
         hashVal %= arraySize;      // 不让超过数组的大小
         }
      return null;  // 没有找到
  }

}  

public class HashTableApp{//测试
   public static void main(String[] args) throws IOException {
      DataItem aDataItem;
      int aKey, size, n, keysPerCell;                                  
      System.out.print("Enter size of hash table: ");
      size = getInt();
      System.out.print("Enter initial number of items: ");
      n = getInt();
      keysPerCell = 10;
                                   
      HashTable theHashTable = new HashTable(size);

      for(int j=0; j<n; j++){
         aKey = (int)(java.lang.Math.random() * keysPerCell * size);
         aDataItem = new DataItem(aKey);
         theHashTable.insert(aDataItem);
      }

      while(true){
         System.out.print("Enter first letter of ");
         System.out.print("show, insert, delete, or find: ");
         char choice = getChar();
         switch(choice){
            case 's':
               theHashTable.displayTable();
               break;
            case 'i':
            System.out.print("Enter key value to insert: ");
               aKey = getInt();
               aDataItem = new DataItem(aKey);
               theHashTable.insert(aDataItem);
               break;
            case 'd':
               System.out.print("Enter key value to delete: ");
               aKey = getInt();
               theHashTable.delete(aKey);
               break;
            case 'f':
               System.out.print("Enter key value to find: ");
               aKey = getInt();
               aDataItem = theHashTable.find(aKey);
               if(aDataItem != null)
                  {
                  System.out.println("Found " + aKey);
                  }
               else
                  System.out.println("Could not find " + aKey);
               break;
            default:
               System.out.print("Invalid entry\n");
            }  
         }  
      }  

   public static String getString() throws IOException{
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(isr);
      String s = br.readLine();
      return s;
      }

   public static char getChar() throws IOException
      {
      String s = getString();
      return s.charAt(0);
      }

   public static int getInt() throws IOException
      {
      String s = getString();
      return Integer.parseInt(s);
      }

   }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值