Advertisement
0cionr

Untitled

Oct 21st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. //this shit is shared between functions
  7. struct Node{
  8. int value;
  9. struct Node* next;
  10. };
  11. int count; //number of unique addresses
  12. int size; //size of the hashtable
  13. int idx;
  14. static struct Node* hashTable1;
  15. struct Node* hT1; //this pointer points to the front of a hash table, allowing the hashtable to be passed and returned
  16.  
  17. //initialize load factor
  18. struct Node* ptr; //used in rebalance() to keep track of which node we look at
  19.  
  20. struct Node* prv;
  21. struct Node* del;
  22. int data;
  23.  
  24.  
  25.  
  26. int insert(int data){
  27. //invalid data
  28. if(data == -1){
  29. return 0;
  30. }
  31. hT1 = hashTable1;
  32. for(int i = idx; i!=0; i--){
  33. hT1++;
  34. }
  35. if(hT1 == NULL){
  36. hT1 = malloc(sizeof(struct Node));
  37. hT1-> value = data;
  38. hT1-> next = NULL;
  39. return 1;
  40. }
  41. while(ptr != NULL){
  42. if(data == ptr->value){
  43. return 0;
  44. }
  45. prv = ptr;
  46. ptr = ptr-> next;
  47. }
  48. prv->next = malloc(sizeof(struct Node));
  49. ptr = prv->next;
  50. ptr->value = data;
  51. ptr->next = NULL;
  52. return 1;
  53. }
  54. //rebalance -accepts hashtable
  55. //main -accepts file -declares hashtable -declarations
  56. int main (int argc, char** argv){
  57. if(argc !=2){
  58. printf("error\n");
  59. return -1;
  60. }
  61. size = 10000;
  62. count = 0;
  63. hashTable1 = malloc(size*sizeof(struct Node*));
  64. int data;
  65. FILE *fp;
  66. fp = fopen(argv[1], "r");
  67.  
  68. if(fp == NULL){
  69. printf("error\n");
  70. return -1;
  71. }
  72. //insert values into the hashtable
  73. while(fscanf(fp, "%d", &data)!= EOF){
  74. idx = data%size;
  75. for(int i = idx; i!=0; i--){
  76. hT1++;
  77. }
  78. count += insert(data);
  79. }
  80. fclose(fp);
  81. ptr = hashTable1;
  82. //ptr stays on the main array, ptr++ after every iteration of the main for looop
  83. //
  84. for(int f = size; f!=0; f--){
  85.  
  86. }//free's the buckets
  87. printf("%d",count);
  88. return 0;
  89. };//end of main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement