Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int hash_(string &c){
  7. int h = 0;
  8. for(int i= 0 ; i <= c.length() ; i++){
  9. h = h + int(c[i]);
  10. }
  11. return h;
  12. }
  13.  
  14. int hash_small(string &c){
  15.  
  16. return (c.length() % 5)-1;
  17. }
  18.  
  19.  
  20. struct node{
  21. node* next;
  22. string data;
  23. };
  24.  
  25.  
  26.  
  27. void insert(node* hash_table[], string data){
  28. node* nw = new node;
  29. nw->data = data;
  30. nw->next = NULL;
  31. int index = hash_small(data);
  32. cout << "index = " << index << endl;
  33.  
  34. if(hash_table[index] == NULL){
  35. hash_table[index] = nw;
  36. cout << "Inserted the data = " << data << endl;
  37. }else{
  38. node* temp = hash_table[index];
  39. while(true){
  40. if(temp->next == NULL) break;
  41. temp = temp->next;
  42. }
  43. temp->next = nw;
  44. cout << "The chain is inserted " << endl;
  45. }
  46. }
  47.  
  48. void print(node* hash_table[], int n){
  49. for(int i=0; i<n; i++){
  50. if(hash_table[i]!=NULL) {
  51. node* item = hash_table[i];
  52. while(true){
  53. cout << setw(10) << item->data ;
  54. if(item->next == NULL) break;
  55. item = item->next;
  56. }
  57. cout << endl;
  58. }
  59. }
  60. }
  61.  
  62. int main(){
  63.  
  64.  
  65. string a = "ajay";
  66. cout << hash_(a) << endl;
  67. string a1 = "ajay";
  68. cout << hash_(a1) << endl;
  69. a = "a";
  70. cout << hash_(a) << endl;
  71. a = "a";
  72. cout << hash_(a) << endl;
  73. a = "z";
  74. cout << hash_(a) << endl;
  75.  
  76. // insertions
  77. node* hash_table[100] ;
  78. insert(hash_table, "ajay");
  79. insert(hash_table, "avi");
  80. insert(hash_table, "ajayf");
  81. insert(hash_table, "ajaywe");
  82. insert(hash_table, "ajayrw");
  83. insert(hash_table, "ajaewy");
  84. insert(hash_table, "ajayewer");
  85. insert(hash_table, "ajay");
  86.  
  87.  
  88. print(hash_table, 5);
  89.  
  90.  
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97. /** output
  98.  
  99.  
  100. 421
  101. 421
  102. 97
  103. 97
  104. 122
  105. index = 3
  106. Inserted the data = ajay
  107. index = 2
  108. Inserted the data = avi
  109. index = -1
  110. Inserted the data = ajayf
  111. index = 0
  112. Inserted the data = ajaywe
  113. index = 0
  114. The chain is inserted
  115. index = 0
  116. The chain is inserted
  117. index = 2
  118. The chain is inserted
  119. index = 3
  120. The chain is inserted
  121. ajaywe ajayrw ajaewy
  122. avi ajayewer
  123. ajay ajay
  124.  
  125. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement