Advertisement
lordasif

Hash

Jul 16th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. int SIZE;
  8. struct Node
  9. {
  10. int val;
  11. Node * next;
  12. };
  13.  
  14. struct LinkedList
  15. {
  16. private:
  17. Node ** hashTable;
  18. public:
  19. void init(int s)
  20. {
  21. SIZE = s;
  22. hashTable = (Node**)calloc(SIZE,sizeof(Node*));
  23. }
  24.  
  25. int hashFunc(int key)
  26. {
  27. return key%SIZE;
  28. }
  29.  
  30. void insertValue(int v)
  31. {
  32. Node * newnode = new Node;
  33. newnode->val = v;
  34. newnode->next = NULL;
  35.  
  36. int index = hashFunc(v);
  37. if(hashTable[index] == NULL)
  38. {
  39. hashTable[index]=(Node*)malloc(sizeof(Node*));
  40. hashTable[index]=newnode;
  41. }
  42. else
  43. {
  44. Node* temp = new Node;
  45. temp = hashTable[index];
  46. newnode->next=temp;
  47. hashTable[index]=newnode;
  48. }
  49. }
  50. int searchValue(int v)
  51. {
  52.  
  53. }
  54.  
  55. void display()
  56. {
  57. for(int i=0;i<SIZE;i++)
  58. {
  59. cout<<i<<"-->";
  60. if(hashTable[i] != NULL)
  61. {
  62. Node* temp=new Node;
  63. temp=hashTable[i];
  64. while(temp != NULL)
  65. {
  66. cout<<temp->val<<"-->";
  67. temp=temp->next;
  68. }
  69. }
  70. cout<<"\n";
  71. }
  72.  
  73. }
  74. };
  75.  
  76. int main()
  77. {
  78. LinkedList l1;
  79.  
  80. l1.init(10);
  81.  
  82. l1.insertValue(22);
  83. l1.insertValue(32);
  84. l1.insertValue(21);
  85. l1.insertValue(25);
  86. l1.insertValue(27);
  87.  
  88. l1.display();
  89.  
  90. l1.searchValue(32);
  91. l1.searchValue(2);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement