Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include <string.h>
  6.  
  7.  
  8. int hash (int c, int M){
  9. return c%M;
  10. }
  11.  
  12. int deslocamento(int c){
  13. return (c%2) + 1;
  14. }
  15.  
  16. void inserir(int chave, int pos, int hash_table[], int M){
  17. if(hash_table[pos] == -1){
  18. hash_table[pos] = chave;
  19. }else{
  20. int novaposicao = pos + deslocamento(chave);
  21. while(novaposicao > 7){
  22. novaposicao = pos + deslocamento(chave);
  23. }
  24. hash_table[novaposicao] = chave;
  25. }
  26. }
  27.  
  28. void imprimeTabelaEncadeada(int tabela[], int m){
  29. int i;
  30. for(i = 0; i < m; i++){
  31. if((tabela[i] == -1)){
  32. printf("Vazio");
  33. }else{
  34. printf("Chave: %d", tabela[i]);
  35. }
  36. printf("n");
  37. }
  38. }
  39.  
  40. int main(){
  41. int M = 7;
  42. int hash_table[7];
  43. int i;
  44. for (i = 0 ; i < M; i++){
  45. hash_table[i] = -1;
  46. }
  47. for (i = 0 ; i < M; i++){
  48. printf("%d", hash_table[i]);
  49. }
  50. printf("n");
  51. int N = 5;
  52. int chaves[] = {24,76,39,61,25};
  53. int pos;
  54. for (i = 0; i < N; i++){
  55. pos = hash(chaves[i], M);
  56. printf("%dn", pos);
  57. inserir(chaves[i], pos, hash_table, M);
  58. }
  59. imprimeTabelaEncadeada(hash_table, M);
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement