• 数据结构— —哈希表顺序实现


    哈希表的初始化

    1. bool initHash(Hash& hash, int size)
    2. {
    3. //int n = HashFound(hash, key);
    4. hash.size = size;
    5. hash.list = new Sqlist[size];
    6. for (int i = 0; i < hash.size; i++)
    7. {
    8. hash.list[i].data = new Data[30];
    9. if (hash.list[i].data == NULL)
    10. {
    11. return false;
    12. }
    13. hash.list[i].length = 0;
    14. }
    15. return true;
    16. }


    哈希表的插入

    1. bool insert(Hash& hash, int key, int value)
    2. {
    3. if (found(hash, key))
    4. {
    5. return false;
    6. }
    7. else
    8. {
    9. Data D;
    10. D.data1 = value;
    11. D.key = key;
    12. int n = HashFound(hash, key);
    13. hash.list[n].data[hash.list[n].length] = D;
    14. hash.list[n].length++;
    15. return true;
    16. }
    17. }

    哈希表的查询

    1. bool found(Hash& hash, int key)
    2. {
    3. int n = HashFound(hash, key);
    4. for (int i = 0; i < hash.list[n].length; i++)
    5. {
    6. if (hash.list[n].data[i].key == key)
    7. {
    8. return true;
    9. }
    10. }
    11. return false;
    12. }

    哈希表的删除

    1. bool Delete(Hash& hash, int key)
    2. {
    3. int n = HashFound(hash, key);
    4. int pos = -1;
    5. for (int i = 0; i < hash.list[n].length; i++)
    6. {
    7. if (hash.list[n].data[i].key == key)
    8. {
    9. pos = i;
    10. }
    11. }
    12. if (pos != -1)
    13. {
    14. for (int j = pos; j < hash.list[n].length - 1; j++)
    15. {
    16. hash.list[n].data[j] = hash.list[n].data[j + 1];
    17. }
    18. hash.list[n].length--;
    19. }
    20. return true;
    21. }

    哈希表的桶查找

    1. int HashFound(Hash& hash, int key)
    2. {
    3. int n = key % hash.size;
    4. return n;
    5. }

    哈希表的销毁

    1. bool Destory(Hash& hash)
    2. {
    3. for (int i = 0; i < hash.size; i++)
    4. {
    5. delete (hash.list[i].data);
    6. }
    7. delete (hash.list);
    8. return true;
    9. }

    哈希表的代码实现

    1. #include
    2. #include
    3. using namespace std;
    4. typedef struct _Data
    5. {
    6. int data1;
    7. int key;
    8. }Data;
    9. typedef struct _Sqlist
    10. {
    11. Data* data;
    12. int length;
    13. }Sqlist;
    14. typedef struct _Hash
    15. {
    16. int size;
    17. Sqlist* list;
    18. }Hash;
    19. bool initHash(Hash& hash, int size);
    20. bool insert(Hash& hash, int key, int value);
    21. int HashFound(Hash& hash, int key);
    22. bool Delete(Hash& hash, int key);
    23. bool Destory(Hash& hash);
    24. bool found(Hash& hash, int key);
    25. void Print(Hash& hash);
    26. bool Destory(Hash& hash)
    27. {
    28. for (int i = 0; i < hash.size; i++)
    29. {
    30. delete (hash.list[i].data);
    31. }
    32. delete (hash.list);
    33. return true;
    34. }
    35. bool Delete(Hash& hash, int key)
    36. {
    37. int n = HashFound(hash, key);
    38. int pos = -1;
    39. for (int i = 0; i < hash.list[n].length; i++)
    40. {
    41. if (hash.list[n].data[i].key == key)
    42. {
    43. pos = i;
    44. }
    45. }
    46. if (pos != -1)
    47. {
    48. for (int j = pos; j < hash.list[n].length - 1; j++)
    49. {
    50. hash.list[n].data[j] = hash.list[n].data[j + 1];
    51. }
    52. hash.list[n].length--;
    53. }
    54. return true;
    55. }
    56. int HashFound(Hash& hash, int key)
    57. {
    58. int n = key % hash.size;
    59. return n;
    60. }
    61. bool insert(Hash& hash, int key, int value)
    62. {
    63. if (found(hash, key))
    64. {
    65. return false;
    66. }
    67. else
    68. {
    69. Data D;
    70. D.data1 = value;
    71. D.key = key;
    72. int n = HashFound(hash, key);
    73. hash.list[n].data[hash.list[n].length] = D;
    74. hash.list[n].length++;
    75. return true;
    76. }
    77. }
    78. bool found(Hash& hash, int key)
    79. {
    80. int n = HashFound(hash, key);
    81. for (int i = 0; i < hash.list[n].length; i++)
    82. {
    83. if (hash.list[n].data[i].key == key)
    84. {
    85. return true;
    86. }
    87. }
    88. return false;
    89. }
    90. bool initHash(Hash& hash, int size)
    91. {
    92. //int n = HashFound(hash, key);
    93. hash.size = size;
    94. hash.list = new Sqlist[size];
    95. for (int i = 0; i < hash.size; i++)
    96. {
    97. hash.list[i].data = new Data[30];
    98. if (hash.list[i].data == NULL)
    99. {
    100. return false;
    101. }
    102. hash.list[i].length = 0;
    103. }
    104. return true;
    105. }
    106. void Print(Hash& hash)
    107. {
    108. for (int i = 0; i < hash.size; i++)
    109. {
    110. for (int j = 0; j < hash.list[i].length; j++)
    111. {
    112. cout << "key:" << hash.list[i].data[j].key << " data:" << hash.list[i].data[j].data1 << endl;
    113. }
    114. }
    115. }
    116. int main(void)
    117. {
    118. Hash hash;
    119. int size = 5;
    120. initHash(hash, size);
    121. insert(hash, 1, 11);
    122. insert(hash, 2, 12);
    123. insert(hash, 3, 13);
    124. insert(hash, 4, 14);
    125. Print(hash);
    126. Delete(hash, 2);
    127. Print(hash);
    128. Destory(hash);
    129. system("pasue");
    130. return 0;
    131. }

  • 相关阅读:
    ChatGPT如何训练自己的模型
    Centos7 设置定时任务
    jQuery提供的获取元素位置的接口方法
    python execute() 使用%s 拼接sql 避免sql注入攻击 好于.format
    《C和指针》笔记31:多维数组的数组名、指向多维数组的指针、作为函数参数的多维数组
    2023-2024年云赛道模拟题库
    C/C++语言100题练习计划 81——圆星人贸易(签到题)
    Linux:firewalled服务常规操作汇总
    运输层课后作业
    数据治理-数据模型计分卡
  • 原文地址:https://blog.csdn.net/m0_65635427/article/details/127811347