fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node {
  5. int data;
  6. node *next;
  7. };
  8.  
  9. int hash_function(int data)
  10. {
  11. return (rand() %100);
  12. }
  13.  
  14. void insert_node(node **q,int data)
  15. {
  16. node *temp,*temp2;
  17. int pos;
  18. pos=hash_function(data);
  19. if(q[pos]==NULL)
  20. {
  21. temp=new node;
  22. temp->next=NULL;
  23. temp->data=data;
  24. q[pos]=temp;
  25. }
  26. else
  27. {
  28. temp2=q[pos];
  29. while(temp2->next != NULL )
  30. {
  31. temp2=temp2->next;
  32. }
  33. temp=new node;
  34. temp->next=NULL;
  35. temp->data=data;
  36. temp2->next=temp;
  37. }
  38. }
  39.  
  40. void display(node **q)
  41. {
  42. node * temp;
  43. for(int i=0;i<100;i++)
  44. {
  45. temp=q[i];
  46. if(temp !=NULL)
  47. {
  48.  
  49. cout<<"\n Position : "<<i<<" , Elements : ";
  50. while(temp->next != NULL)
  51. {
  52. cout<<" "<<temp->data;
  53. temp=temp->next;
  54. }
  55. cout<<" "<<temp->data;
  56. }
  57. }
  58.  
  59.  
  60. }
  61.  
  62. int main() {
  63. node* a[100],*b,*temp,*temp2;
  64. int num;
  65. for(int i=0;i<100;i++)
  66. {
  67. a[i]=NULL;;
  68. }
  69. for(int i=0;i<100;i++) // If this loop is less than 30 , it runs
  70. {
  71. num=(1+ (rand() %100) );
  72.  
  73. insert_node(a,num);
  74. }
  75. display(a);
  76. return 0;
  77. }
  78.  
  79.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
 Position : 0 , Elements :  94
 Position : 1 , Elements :  68
 Position : 2 , Elements :  30 98
 Position : 5 , Elements :  37
 Position : 7 , Elements :  76
 Position : 8 , Elements :  88
 Position : 11 , Elements :  44
 Position : 12 , Elements :  40
 Position : 14 , Elements :  25
 Position : 15 , Elements :  78 9
 Position : 17 , Elements :  32
 Position : 19 , Elements :  22 45 4
 Position : 21 , Elements :  50 75
 Position : 23 , Elements :  63 97
 Position : 24 , Elements :  99 20
 Position : 25 , Elements :  6
 Position : 26 , Elements :  64 41 14
 Position : 27 , Elements :  63 85 10
 Position : 28 , Elements :  65 38 19
 Position : 29 , Elements :  68 47 41 33 29
 Position : 30 , Elements :  83
 Position : 35 , Elements :  94 68
 Position : 36 , Elements :  73 60
 Position : 37 , Elements :  85 30
 Position : 39 , Elements :  95
 Position : 41 , Elements :  87
 Position : 42 , Elements :  12
 Position : 45 , Elements :  83 19
 Position : 49 , Elements :  41
 Position : 50 , Elements :  44 42
 Position : 51 , Elements :  4 47 33
 Position : 53 , Elements :  98
 Position : 55 , Elements :  22
 Position : 56 , Elements :  94 53 68 88
 Position : 57 , Elements :  14
 Position : 58 , Elements :  23
 Position : 59 , Elements :  91
 Position : 60 , Elements :  33
 Position : 64 , Elements :  35 35
 Position : 65 , Elements :  87 28
 Position : 67 , Elements :  70 15
 Position : 68 , Elements :  12 77 71
 Position : 70 , Elements :  16 63 96
 Position : 71 , Elements :  98 29
 Position : 73 , Elements :  30 57
 Position : 75 , Elements :  82
 Position : 78 , Elements :  77 35
 Position : 80 , Elements :  92 2
 Position : 81 , Elements :  97
 Position : 83 , Elements :  7
 Position : 84 , Elements :  89
 Position : 86 , Elements :  84 27
 Position : 88 , Elements :  80
 Position : 89 , Elements :  66
 Position : 91 , Elements :  44
 Position : 92 , Elements :  87 18
 Position : 93 , Elements :  36
 Position : 95 , Elements :  25 59
 Position : 99 , Elements :  55