fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* 第四週:
  6.  
  7.   1. 新增一含 5 elements 的 Hash Table
  8.  
  9.   2. 請將之前你們新增的十個 list entries 均勻的加入此 Hash Table
  10.  
  11.   3. 請將此 Hash Table 的內容印出, 並藉由印出的方式證明你是均勻的 Hash 入 Hash Table
  12.  
  13. */
  14. int mod = 5;
  15. struct data {
  16. char name[16];
  17. unsigned long height;
  18. unsigned short index;
  19. struct data *next;
  20. }stu,*hash[10], *current, *previous, *tmp;;
  21.  
  22.  
  23. typedef struct data Node;
  24.  
  25. void insert_hash(int n, char *arr, unsigned long *arr1, unsigned short *arr2);
  26. void print_hash();
  27.  
  28. int main()
  29. {
  30.  
  31. struct data stu = {"Kerwin", 177, 77};
  32.  
  33. Node *first, *node;
  34. char *arr[] = {"Ariza", "Bryant", "Clarkson", "Divac", "Ennis", "Fisher", "Gasol", "Horry", "Ingram", "Johnson"};
  35. unsigned long arr1[] = {203, 198, 196, 216, 191, 185, 213, 206, 206, 206};
  36. unsigned short arr2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  37. int i = 0;
  38.  
  39. for(i=0; i<10; i++)
  40. insert_hash(i+1, arr[i], arr1[i], arr2[i]);
  41.  
  42. print_hash();
  43.  
  44. system("pause");
  45. return 0;
  46. }
  47.  
  48.  
  49.  
  50. void insert_hash(int n, char *arr, unsigned long *arr1, unsigned short *arr2)
  51. {
  52. int m = n % mod;
  53. if (hash[m] == NULL) {
  54. current = (struct data *)malloc(sizeof(struct data));
  55. strcpy(current->name, arr);
  56. current->height = arr1;
  57. current->index = arr2;
  58. current->next = NULL;
  59. hash[m] = current;
  60. return;
  61. }
  62. tmp = hash[m], previous = NULL;
  63. while(tmp->index <= arr2) {
  64. if(tmp->index == arr2)
  65. return;
  66. if(tmp->next != NULL)
  67. previous = tmp, tmp = tmp->next;
  68. else {
  69. current = (struct data *)malloc(sizeof(struct data));
  70. current->index = arr2;
  71. strcpy(current->name, arr);
  72. current->height = arr1;
  73. current->next = NULL;
  74. tmp->next = current;
  75. return;
  76. }
  77. }
  78. if(previous != NULL) {
  79. current = (struct data *)malloc(sizeof(struct data));
  80. current->index = arr2;
  81. previous->next = current, current->next = tmp;
  82. }
  83. else {
  84. current = (struct data *)malloc(sizeof(struct data));
  85. current->index = arr2;
  86. hash[m] = current, current->next = tmp;
  87. }
  88. return;
  89. }
  90.  
  91.  
  92. void print_hash()
  93. {
  94. int a;
  95. for(a = 0; a < mod; a++) {
  96. printf("[%03d]:", a);
  97. current = hash[a];
  98. while(current != NULL) {
  99. printf("%2d\t%8s\t%3d", current->index, current->name, current->height);
  100. printf("\n ");
  101. current = current->next;
  102. }
  103. printf("\n");
  104. }
  105. }
  106.  
Success #stdin #stdout #stderr 0s 4432KB
stdin
Standard input is empty
stdout
[000]: 5	   Ennis	191
      10	 Johnson	206
      
[001]: 1	   Ariza	203
       6	  Fisher	185
      
[002]: 2	  Bryant	198
       7	   Gasol	213
      
[003]: 3	Clarkson	196
       8	   Horry	206
      
[004]: 4	   Divac	216
       9	  Ingram	206
      
stderr
sh: 1: pause: not found