Hashing- Linklist Chaining

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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

Hashing- Linklist Chaining

 
0
  #1
Dec 12th, 2007
Added just to help other....
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. void createHashTable(void);
  6. void getData(void);
  7. void formatting(void);
  8. int insertData(int);
  9. int collision_OpenHashing(struct hashOpen*& ,int);
  10. int generateKey(int);
  11. void DispData(void);
  12. void DispHash(void);
  13.  
  14. #define dataSize 15
  15. #define empty -1
  16. #define fail 0
  17. #define success 1
  18.  
  19. struct hashOpen{
  20. int item;
  21. struct hashOpen *next;
  22. }*head, hashTable[10];
  23.  
  24. int dataArray[dataSize];
  25. struct hashOpen* cur;
  26. int count, status;
  27. int filledSpaces = 0;
  28. float loadFactor = 0;
  29.  
  30. void main(void)
  31. {
  32. clrscr();
  33. formatting();
  34.  
  35. getData();
  36. createHashTable();
  37.  
  38. count = 0;
  39. do
  40. {
  41.  
  42. DispData();
  43. DispHash();
  44. printf("\ncount=%d", count);
  45.  
  46. status = insertData(dataArray[count]);
  47. count++;
  48. } while (count <= dataSize);
  49.  
  50. printf("\n\n-------------Programming Successfully Completed-------------");
  51. getch();
  52. }
  53.  
  54. void createHashTable(void)
  55. {
  56. int i;
  57. /******** Initialize HashTable ************************/
  58. for( i = 0; i <= 9; i++)
  59. {
  60. hashTable[i].item = empty;
  61. hashTable[i].next = NULL;
  62. }
  63. }
  64.  
  65. void getData(void)
  66. {
  67. int i;
  68. randomize();
  69. for(i =0; i < dataSize; i++)
  70. dataArray[i] = random(1)+2;
  71. }
  72.  
  73. void DispData(void)
  74. {
  75. int i;
  76. printf("\nData[]={");
  77. for(i =0; i < dataSize; i++)
  78. printf("%d,", dataArray[i]);
  79. printf("\b}");
  80. }
  81.  
  82. void DispHash(void)
  83. {
  84. int count;
  85. printf("\nHash Table content = [");
  86. for(count = 0; count < 9 ; count++)
  87. {
  88. printf("%d, ", hashTable[count].item);
  89. cur++;
  90. }
  91. printf("\b\b]");
  92. }
  93.  
  94. void formatting(void)
  95. {
  96. int i;
  97. printf("\n");
  98. for(i =0; i<=79 ; i++)
  99. printf("*");
  100.  
  101. printf("\n......... Prgogram title\t\t# Hash Table");
  102. printf("\n......... Created by\t\t # Romasa Qasim");
  103. printf("\n......... Description\t\t # Creation of Hash Table, generation of,");
  104. printf("\t\t\t\t\t Key, Insertion, Searching and Deletion");
  105. printf("\t\t\t\t\t of data in the Hash Table\n");
  106.  
  107. for( i =0; i<=79 ; i++)
  108. printf("*");
  109. }
  110.  
  111. int insertData(int data)
  112. {
  113. int hashIndex;
  114. hashIndex = generateKey(data);
  115. if( hashTable[hashIndex].item == empty)
  116. {
  117. hashTable[hashIndex].item = data;
  118. return(success);
  119. }
  120.  
  121. else
  122. return(collision_OpenHashing(hashTable[hashIndex].next, data));
  123.  
  124. }
  125.  
  126. int collision_OpenHashing(struct hashOpen *&pNode, int data)
  127. {
  128. if(pNode == NULL)
  129. {
  130. head = (struct hashOpen*)malloc(sizeof(struct hashOpen));
  131. head->item = data;
  132. head->next = NULL;
  133. pNode = head;
  134. }
  135.  
  136. else
  137. collision_OpenHashing(pNode->next, data);
  138.  
  139. }
  140.  
  141. int generateKey(int data)
  142. {
  143. int key;
  144. key = data % 10;
  145. return(key);
  146. }
Last edited by Narue; Dec 12th, 2007 at 3:21 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 22
Reputation: Rajith Cherian is an unknown quantity at this point 
Solved Threads: 4
Rajith Cherian's Avatar
Rajith Cherian Rajith Cherian is offline Offline
Newbie Poster

Re: Hashing- Linklist Chaining

 
0
  #2
Jan 8th, 2008
For better performance if the no.of elements is very large us a large primary value as the hash key for better hashing.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 18
Reputation: rizrash is an unknown quantity at this point 
Solved Threads: 0
rizrash rizrash is offline Offline
Newbie Poster

Re: Hashing- Linklist Chaining

 
0
  #3
Apr 26th, 2008
hey friends just come across this program and found it very fruitfull....but would be more so happy if some one coould explain what tasks are we trying to achieve....or in short how would i be able to explain this program to some one ...so if someone with good knowledge about this topic can explain to me what

void createHashTable(void);
void getData(void);
void formatting(void);
int insertData(int);
int collision_OpenHashing(struct hashOpen*& ,int);
int generateKey(int);
void DispData(void);
void DispHash(void)

what the following functions are doing in this program

please some one who can guide me through various stages of this program so i can grasp it properly and be able to explain and perform it !!

regards,
Rash
Reply With Quote Quick reply to this message  
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

its.romi

 
0
  #4
Apr 27th, 2008
void createHashTable(void);
This function creates a hash table in which the data items are to be inserted in sorted form for better searching

void getData(void);
This function collects data from user to be inserted in the hash table

void formatting(void);
This function is just to beautify the output of the program

int insertData(int);
This function ineserts the data in the hash table

int collision_OpenHashing(struct hashOpen*& ,int);
This function works when there is a collision occured in the table e.g if two data items with same key are to be inserted in the hash table then there must be a collision, both data items are the candidate for the same cell of hash table. To remove this collision This function acts as a collision resolver between that two data items.

int generateKey(int);
This function generates the key for the data items to be inserted with the help of a formula

void DispData(void);
This function displays the data to the end user

void DispHash(void)
This function displays the state of hash table after each entry
----------------------------------------------------------------------------------------------------

this is the introduction of all the funstion's performance, if you need description of coding then do acknowledge me.

Regards,

Romasa
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 18
Reputation: rizrash is an unknown quantity at this point 
Solved Threads: 0
rizrash rizrash is offline Offline
Newbie Poster

Re: Hashing- Linklist Chaining

 
0
  #5
Apr 27th, 2008
Thanks alot ramosa....very you`ve described all the functions very nicely....and i`ve understood most of it...Thanks once again !!
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum


Views: 968 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC