算法-----开放寻址法

  1. packagecom.eshore.sweetop.dataframe;
  2. importcom.eshore.sweetop.data.KeyData;
  3. /*
  4. *开放寻址法解决碰撞问题
  5. */
  6. publicclassOpenHash{
  7. protectedKeyData[]table;
  8. publicOpenHash(intsize){
  9. table=newKeyData[size];
  10. }
  11. publicvoidinsert(KeyDatakd){
  12. intj=0,k=kd.getKey();
  13. for(inti=0;i<table.length;i++){
  14. j=hash(k,i);
  15. if(table[j]==null){
  16. table[j]=kd;
  17. return;
  18. }
  19. }
  20. thrownewRuntimeException("hashtableoverflow!");
  21. }
  22. publicKeyDatasearch(intk){
  23. intj=0;
  24. for(inti=0;i<table.length;i++){
  25. j=hash(k,i);
  26. if(table[j].getKey()==k){
  27. returntable[j];
  28. }
  29. }
  30. returnnull;
  31. }
  32. /*
  33. *当然不能这么写,这里重点是开放寻址法,关于探查序列,后面有续
  34. */
  35. publicinthash(intk,inti){
  36. returni;
  37. }
  38. publicstaticvoidmain(String[]args){
  39. OpenHashoh=newOpenHash(10);
  40. oh.insert(newKeyData(1));
  41. oh.insert(newKeyData(2));
  42. oh.insert(newKeyData(3));
  43. oh.insert(newKeyData(14));
  44. KeyDatak=oh.search(2);
  45. System.out.println(k);
  46. }
  47. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值