943,699 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5022
  • C RSS
Dec 12th, 2007
0

hash- linear probing

Expand Post »
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
Similar Threads
Reputation Points: 8
Solved Threads: 1
Newbie Poster
its.romi is offline Offline
17 posts
since Dec 2007
Dec 13th, 2007
0

Re: hash- linear probing

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.
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Dec 13th, 2007
1

Re: hash- linear probing

I think I got to 10 bugs, then stopped counting.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 4th, 2010
0

suggest

nice job...
but code is difficult for understanding
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zohaib yousuf is offline Offline
1 posts
since Dec 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: please !! see what is the problem with this !!
Next Thread in C Forum Timeline: how to input into array from keyboard





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC