Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define MAX 130000
  6.  
  7. struct Route
  8. {
  9. int a;
  10. int b;
  11. int c;
  12. int interface;
  13. };
  14.  
  15. // Inicializa a struct 'Route'
  16. struct Route *new_route(int a, int b, int c, int interface)
  17. {
  18. struct Route* nova = malloc(sizeof(struct Route));
  19.  
  20. if (nova != NULL)
  21. {
  22. nova->a = a;
  23. nova->b = b;
  24. nova->c = c;
  25. nova->interface = interface;
  26. }
  27. return nova;
  28. }
  29.  
  30. struct Route *hashArray[MAX];
  31.  
  32. // Função Hash
  33. // (Como lidar com as colisões? Incrementar para a posição seguinte?)
  34. int hashCode(int a, int b, int c)
  35. {
  36. int hash = (((a * 1000) + (b * 100) + c) % MAX);
  37. //return key % SIZE; // Hash value
  38.  
  39. return hash;
  40. }
  41.  
  42. // Função para procurar o endereço
  43. struct Route *search(int a, int b, int c)
  44. {
  45. int hashIndex = hashCode(a); // Para obter o valor de hash
  46.  
  47. // While array is not empty...
  48. while (hashArray[hashIndex] != NULL)
  49. {
  50. if (hashArray[hashIndex]->a == a && hashArray[hashIndex]->b == b && hashArray[hashIndex]->c == c)
  51. {
  52. return hashArray[hashIndex];
  53. }
  54. ++hashIndex; // Vai para a próxima posição.
  55.  
  56. hashIndex %= MAX; // Wrap around the table.
  57. }
  58. return NULL;
  59. }
  60.  
  61. // Função para inserir o endereço na Hashtable
  62. void insert(int a, int b, int c, int interface)
  63. {
  64. struct Route *item = new_route(a, b, c, interface);
  65.  
  66. int hashIndex = hashCode(interface); // Para obter o valor de hash
  67.  
  68. // Move in array until an empty or deleted cell
  69. while (hashArray[hashIndex] != NULL)
  70. {
  71. ++hashIndex; // Incrementa na tabela.
  72.  
  73. hashIndex %= MAX; // Wrap around the table.
  74. }
  75.  
  76. hashArray[hashIndex] = item;
  77. }
  78.  
  79. int main()
  80. {
  81. int num_routes; // número de routes
  82. scanf("%d\n", &num_routes);
  83.  
  84. int a;
  85. int b;
  86. int c;
  87. int d;
  88. int interface;
  89.  
  90. for (int i = 0; i < num_routes; i++)
  91. {
  92. scanf("%d.%d.%d.0 %d\n", &a, &b, &c, &interface);
  93.  
  94. insert(a, b, c, interface);
  95. }
  96.  
  97. while (scanf("%d.%d.%d.%d\n", &a, &b, &c, &d) != EOF)
  98. {
  99. search(a, b, c);
  100.  
  101. if (search(a, b, c) == NULL)
  102. {
  103. if (search(0, 0, 0) == NULL) // Default route
  104. {
  105. printf("no route\n");
  106. }
  107. }
  108. printf("%d\n", search(a, b, c)->interface);
  109. }
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement