hash- linear probing

Reply

Join Date: Dec 2007
Posts: 17
Reputation: its.romi is an unknown quantity at this point 
Solved Threads: 1
its.romi its.romi is offline Offline
Newbie Poster

hash- linear probing

 
0
  #1
Dec 12th, 2007
im a student of 2nd year BS, doing my BS in Computer Science.
Here I want to include a program by me, may be someone will be helped by me
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. int* createHashTable(void);
  6. void getData(void);
  7. void formatting(void);
  8. int insertData(int);
  9. float chkLoadFactor(int, int);
  10. int collision_LinearProbing(int, int,int);
  11. int generateKey(int);
  12. void rehashing(void);
  13.  
  14. #define dataSize 15
  15. #define empty -1
  16. #define fail 0
  17. #define success 1
  18.  
  19. int dataArray[dataSize];
  20. int hashSize = 5;
  21. int* head;
  22. int* cur;
  23.  
  24. void main(void)
  25. {
  26. int count, status;
  27. int filledSpaces = 0;
  28. float loadFactor = 0;
  29. clrscr();
  30. formatting();
  31. getData();
  32. head = createHashTable();
  33. for( count = 0; count < dataSize ; count++)
  34. {
  35. if(loadFactor > 0.5)
  36. rehashing();
  37. status = insertData(dataArray[count]);
  38. if(status == success)
  39. filledSpaces++;
  40. loadFactor = chkLoadFactor(status, filledSpaces);
  41.  
  42. if(loadFactor <= 0.5)
  43. printf("LF aftr %d is = %f\t", dataArray[count], loadFactor);
  44.  
  45. if(status == fail)
  46. printf("\nNo room left for %d\n", dataArray[count]);
  47. }
  48. cur = head;
  49. printf("\n\nHash Table content = [");
  50. for(count = 0; count < hashSize ; count++)
  51. {
  52. printf("%d, ", *cur);
  53. cur++;
  54. }
  55. printf("\b\b]");
  56. getch();
  57. }
  58.  
  59. int* createHashTable(void)
  60. {
  61. int i;
  62.  
  63. /********************** Allocating Memory for Hash Table *********************/
  64.  
  65. head = (int*)malloc(sizeof(hashSize*2));
  66. cur = head;
  67.  
  68. /********************** Initialize HashTable *********************************/
  69.  
  70. for( i = 0; i <=hashSize; i++)
  71. {
  72. *cur = empty;
  73. cur++;
  74. }
  75. cur = head;
  76. return(head);
  77. }
  78.  
  79. void getData(void)
  80. {
  81. int i;
  82. randomize();
  83. printf("\n");
  84. printf("Data[]={");
  85. for(i =0; i < dataSize; i++)
  86. {
  87. dataArray[i] = random(50);
  88. printf("%d,", dataArray[i]);
  89. }
  90. printf("\b}\n\n");
  91. }
  92.  
  93. void formatting(void)
  94. {
  95. int i;
  96. printf("\n");
  97. for(i =0; i<=79 ; i++)
  98. printf("*");
  99.  
  100. printf("\n......... Prgogram title\t\t# Hash Table");
  101. printf("\n......... Created by\t\t # Romasa Qasim");
  102. printf("\n......... Description\t\t # Creation of Hash Table, generation of,");
  103. printf("\t\t\t\t\t Key, Insertion, Searching and Deletion");
  104. printf("\t\t\t\t\t of data in the Hash Table\n");
  105.  
  106. for( i =0; i<=79 ; i++)
  107. printf("*");
  108. }
  109.  
  110. int insertData(int data)
  111. {
  112. int hashIndex,count = 0;
  113. hashIndex = generateKey(data);
  114. if( *(head+hashIndex) == empty)
  115. {
  116. *(head+hashIndex) = data;
  117. return(success);
  118. }
  119.  
  120. else
  121. return(collision_LinearProbing((++hashIndex%hashSize), count, data));
  122.  
  123. }
  124.  
  125. float chkLoadFactor(int status, int filledSpaces)
  126. {
  127. float loadFactor;
  128. if (status == success)
  129. {
  130. loadFactor = filledSpaces / (float)hashSize;
  131. return(loadFactor);
  132. }
  133. else
  134. return(1);
  135. }
  136.  
  137.  
  138. int collision_LinearProbing(int hashIndex, int count, int data)
  139. {
  140. if(count == hashSize)
  141. return(fail);
  142.  
  143. if (*(head+hashIndex) == empty)
  144. {
  145. *(head+hashIndex) = data;
  146. return(success);
  147. }
  148. else
  149. {
  150. collision_LinearProbing((++hashIndex % hashSize), ++count, data);
  151. }
  152.  
  153. }
  154.  
  155. int generateKey(int data)
  156. {
  157. int key;
  158. key = data % hashSize;
  159. return(key);
  160. }
  161.  
  162. void rehashing(void)
  163. {
  164. int count;
  165. int status;
  166. hashSize = hashSize * 2;
  167. head = (int*)realloc(head, sizeof(hashSize*2));
  168.  
  169. for( count = 0; count <hashSize; count++)
  170. {
  171. *(head+count) = -1;
  172. }
  173. for(count = 0; count < dataSize; count++)
  174. {
  175. status = insertData(dataArray[count]);
  176. }
  177. }
Last edited by Narue; Dec 12th, 2007 at 3:22 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: hash- linear probing

 
0
  #2
Dec 13th, 2007
Hey, we appreciate your goodwill but this is not the way to do so. Go to 'contribute code' in the 'code snippets' section. Forums are meant only for problem solving.
Last edited by Jishnu; Dec 13th, 2007 at 5:49 am.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: hash- linear probing

 
0
  #3
Dec 13th, 2007
I think I got to 10 bugs, then stopped counting.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC