- Chain hash table
This is a
hash table implementation. Here collisions are handled by Chaining.The divisor used is 13. A double Array is used for implementation ie Array of pointers.1 // Authors:- Prerit Goel and Anant Sinha 2 #include
3 struct node 4 { 5 int data; 6 struct node *next; 7 }; 8 struct node *bucks [13] ; 9 10 void addtolist(int,int); 11 void printlist(void); 12 int search_list(int,int); 13 void dell(int); 14 main() 15 { 16 int i,j,k,h,r,flag; 17
<13;i++) 20 { 21 printf(" Enter any number: "); 22 scanf("%d",&k); 23 h=k%13; 24 addtolist(h,k); 25 26 } 27 printlist(); 28 printf(" Enter an element to be searched : "); 29 scanf("%d",&j); 30 r=j%13; 31
data=data1; 58 if(q=NULL) 59 { 60 q=r; 61 q->next=temp; 62 bucks [index] =q; 63 } 64 else 65 { 66 while(temp->next!=NULL) 67 { 68 temp=temp->next; 69 } 70 71 temp->next=r; 72 r->next=NULL; 73 } 74 } 75 void printlist(void) 76 { 77 struct node *p; 78 int i,d; 79 for(i=0;i"><13;i++) 80 { 81 printf(" "); 82 83 p=bucks [i] ; 84 85 while(p!=NULL) 86 { 87 d=p->data; 88 printf("%d -> ",d); 89 p=p->next; 90 } 91 } 92 } 93 94 int search_list(int index, int data) 95 { 96 struct node *p; 97 p=bucks [index] ; 98 if(p=NULL) 99 return 0; 100 else 101 { 102 while(p!=NULL) 103 { 104 if(p->data=data) 105 return 1; 106 else 107 p=p->next; 108 } 109 return 0; 110 } 111 } 112 113 void dell(int data) 114 { 115 struct node *old,*temp,*q; 116 int f,x; 117 f=data%13; 118 q=temp=bucks [f] ; 119 x=search_list(f,data); 120 if(x=0) 121 { 122 printf(" ELement not present"); 123 return; 124 } 125 else 126 { 127 while(temp!=NULL) 128 { 129 if(temp->data=data) 130 { 131 if(temp=q) 132 { 133 q=temp->next; 134 free(temp); 135 return; 136 } 137 else 138 { 139 old->next=temp->next; 140 free(temp); 141 return; 142 } 143 } 144 else 145 { 146 old=temp; 147 temp=temp->next; 148 } 149 150 } 151 } 152 }
Wikimedia Foundation. 2010.