[关闭]
@zsh-o 2018-10-07T23:55:07.000000Z 字数 1943 阅读 841

简单HashMap,String2Int实现

算法


  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <stdlib.h>
  4. struct Node{
  5. long hcode;
  6. char* key;
  7. int* value;
  8. Node* next;
  9. };
  10. typedef Node* LNode;
  11. struct HashMap{
  12. int HSize;
  13. LNode* HArray;
  14. };
  15. Node* newNode();
  16. int _strlen(char* s);
  17. bool _stringEQ(char* a, char* b);
  18. Node* HArray;
  19. HashMap* initHashMap(int hsize);
  20. //自定义简易hashcode
  21. long hashcode(char* s);
  22. int* get(char* key, HashMap* hashmap);
  23. void put(char* key, int value, HashMap* hashmap);
  24. // main
  25. int main(){
  26. HashMap* hashmap = initHashMap(100);
  27. char S1[] = "abcd";
  28. char S2[] = "cdab";
  29. char S3[] = "nnnn";
  30. put(S1, 10, hashmap);
  31. put(S2, 20, hashmap);
  32. int* a = get(S1, hashmap);
  33. int* b = get(S2, hashmap);
  34. int* c = get(S3, hashmap);
  35. printf("%d %d %d\n", *a, *b, c==NULL);
  36. }
  37. int _strlen(char* s){
  38. int res = 0;
  39. while(s[res] != '\0'){
  40. res++;
  41. }
  42. return res;
  43. }
  44. bool _stringEQ(char* a, char* b){
  45. int len1 = _strlen(a);
  46. int len2 = _strlen(b);
  47. if(len1 != len2){
  48. return false;
  49. }
  50. for(int i=0; i<len1; i++){
  51. if(a[i] != b[i]){
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. Node* newNode(){
  58. Node* res = (Node*)malloc(sizeof(Node));
  59. res->hcode = -1;
  60. res->key = NULL;
  61. res->value = (int*)malloc(sizeof(int));
  62. res->next = NULL;
  63. }
  64. HashMap* initHashMap(int hsize){
  65. HashMap* hashmap = (HashMap*)malloc(sizeof(HashMap));
  66. hashmap->HSize = hsize;
  67. LNode* harray = (LNode*) malloc(sizeof(LNode) * hsize);
  68. for(int i=0; i<hsize; i++){
  69. harray[i] = newNode();
  70. }
  71. hashmap->HArray = harray;
  72. return hashmap;
  73. }
  74. long hashcode(char* s){
  75. long res = 0;
  76. while(*s != '\0'){
  77. res += ((int) *s);
  78. s++;
  79. }
  80. return res * res;
  81. }
  82. int* get(char* key, HashMap* hashmap){
  83. long hcode = hashcode(key);
  84. int index = hcode % hashmap->HSize;
  85. Node* hlroot = hashmap->HArray[index];
  86. Node* p = hlroot->next;
  87. while(p != NULL){
  88. if(p->hcode == hcode && _stringEQ(p->key, key)){
  89. return p->value;
  90. }
  91. p = p->next;
  92. }
  93. return NULL;
  94. }
  95. void put(char* key, int value, HashMap* hashmap){
  96. long hcode = hashcode(key);
  97. int index = hcode % hashmap->HSize;
  98. Node* hlroot = hashmap->HArray[index];
  99. Node* p = hlroot->next;
  100. bool finded = false;
  101. while(p != NULL){
  102. if(p->hcode == hcode && _stringEQ(p->key, key)){
  103. *(p->value) = value;
  104. finded = true;
  105. break;
  106. }
  107. p = p->next;
  108. }
  109. if(finded == false){
  110. Node* n = newNode();
  111. n->key = key;
  112. *(n->value) = value;
  113. n->hcode = hcode;
  114. n->next = hlroot->next;
  115. hlroot->next = n;
  116. }
  117. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注