Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define MAX 130000
- struct Route
- {
- int a;
- int b;
- int c;
- int interface;
- };
- // Inicializa a struct 'Route'
- struct Route *new_route(int a, int b, int c, int interface)
- {
- struct Route* nova = malloc(sizeof(struct Route));
- if (nova != NULL)
- {
- nova->a = a;
- nova->b = b;
- nova->c = c;
- nova->interface = interface;
- }
- return nova;
- }
- struct Route *hashArray[MAX];
- // Função Hash
- // (Como lidar com as colisões? Incrementar para a posição seguinte?)
- int hashCode(int a, int b, int c)
- {
- int hash = (((a * 1000) + (b * 100) + c) % MAX);
- //return key % SIZE; // Hash value
- return hash;
- }
- // Função para procurar o endereço
- struct Route *search(int a, int b, int c)
- {
- int hashIndex = hashCode(a); // Para obter o valor de hash
- // While array is not empty...
- while (hashArray[hashIndex] != NULL)
- {
- if (hashArray[hashIndex]->a == a && hashArray[hashIndex]->b == b && hashArray[hashIndex]->c == c)
- {
- return hashArray[hashIndex];
- }
- ++hashIndex; // Vai para a próxima posição.
- hashIndex %= MAX; // Wrap around the table.
- }
- return NULL;
- }
- // Função para inserir o endereço na Hashtable
- void insert(int a, int b, int c, int interface)
- {
- struct Route *item = new_route(a, b, c, interface);
- int hashIndex = hashCode(interface); // Para obter o valor de hash
- // Move in array until an empty or deleted cell
- while (hashArray[hashIndex] != NULL)
- {
- ++hashIndex; // Incrementa na tabela.
- hashIndex %= MAX; // Wrap around the table.
- }
- hashArray[hashIndex] = item;
- }
- int main()
- {
- int num_routes; // número de routes
- scanf("%d\n", &num_routes);
- int a;
- int b;
- int c;
- int d;
- int interface;
- for (int i = 0; i < num_routes; i++)
- {
- scanf("%d.%d.%d.0 %d\n", &a, &b, &c, &interface);
- insert(a, b, c, interface);
- }
- while (scanf("%d.%d.%d.%d\n", &a, &b, &c, &d) != EOF)
- {
- search(a, b, c);
- if (search(a, b, c) == NULL)
- {
- if (search(0, 0, 0) == NULL) // Default route
- {
- printf("no route\n");
- }
- }
- printf("%d\n", search(a, b, c)->interface);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement