Guest User

Untitled

a guest
Oct 15th, 2017
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. /*-----------------------------------------------------------------------------
  2. the reference is from UdemyCourse: LearningDataStructuresinJavascriptFromScratch
  3. -----------------------------------------------------------------------------*/
  4.  
  5. function HashTable(size) {
  6. this.buckets = Array(size);
  7. this.numBuckets = this.buckets.length;
  8. }
  9.  
  10. function HashNode(key, value, next) {
  11. this.key = key;
  12. this.value = value;
  13. this.next = next || null;
  14. // 沒有next則給予null
  15. }
  16.  
  17. HashTable.prototype.hash = function(key) {
  18. var total = 0;
  19. for (var i = 0; i < key.length; i++) {
  20. total += key.charCodeAt(i);
  21. }
  22. var buckets = total % this.numBuckets;
  23. return buckets;
  24. // buckets [0, 29]
  25. };
  26.  
  27. HashTable.prototype.insert = function(key, value) {
  28. var index = this.hash(key);
  29. // if初始化;else找到尾巴加進去
  30. if (!this.buckets[index]) {
  31. this.buckets[index] = new HashNode(key, value);
  32. } else if (this.buckets[index].key === key) {
  33. this.buckets[index].value = value;
  34. } else {
  35. var currentNode = this.buckets[index];
  36.  
  37. while (currentNode.next) {
  38. if (currentNode.next.key === key) {
  39. currentNode.next.value = value;
  40. return;
  41. // to stop the rest of this method from running
  42. }
  43. currentNode = currentNode.next;
  44. }
  45. currentNode.next = new HashNode(key, value);
  46. }
  47. };
  48.  
  49. HashTable.prototype.get = function(key) {
  50. var index = this.hash(key);
  51. if (!this.buckets[index]) {
  52. return null;
  53. } else {
  54. var currentNode = this.buckets[index];
  55. while (currentNode) {
  56. if (currentNode.key === key) {
  57. return currentNode['value'];
  58. }
  59. currentNode = currentNode.next;
  60. }
  61. return null;
  62. }
  63. };
  64.  
  65. // HashTable.prototype.getNode = function() {
  66. // for (var i in this.buckets) {
  67. // if (this.buckets[i] !== '') {
  68. // console.log(this.buckets[i]);
  69. // }
  70. // }
  71. // };
  72.  
  73. HashTable.prototype.retrieveAll = function() {
  74. var allNode = [];
  75. for (var i = 0; i < this.numBuckets; i++) {
  76. var currentNode = this.buckets[i];
  77. while (currentNode) {
  78. allNode.push(currentNode);
  79. currentNode = currentNode.next;
  80. }
  81. }
  82. return allNode;
  83. };
  84.  
  85. var myHT = new HashTable(30);
  86. myHT.insert('Dean', 'dean@gmail.com');
  87. myHT.insert('Megan', 'megan@gmail.com');
  88. myHT.insert('Dane', 'Dane@yahoo.com');
  89. myHT.insert('Dane', 'Danechin@yahoo.com');
  90. myHT.get('Dean');
  91. // 'dean@gmail.com'
  92.  
  93. // return currentNode[value] 會變成 not defined
  94. // 要寫成currentNode["value"];
  95. // var obj = {a:['da',{dsa:12,jroe:13}], b:{}}
  96. // obj[a]; 不行
  97. // var a = "a"
  98. // obj[a]; 可以
  99.  
  100.  
  101. // 差別是我的子目錄指涵蓋最上頭的那一項目,而Dane在裡面。
  102. // 官方解答除了包含我的邏輯外,還另外log出了下一層的項目。
Add Comment
Please, Sign In to add comment