哈希表,开放地址法之线性探测代码(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. }

原文地址:https://www.cnblogs.com/bjanzhuo/p/3575920.html