Chain hash table

From Wikipedia, the free encyclopedia

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<stdio.h>
    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  flag=0;
   18  printf("\n Enter numbers to be added into the dictionary!\n");
   19  for(i=0;i<13;i++)
   20   {
   21    printf("\n Enter any number: ");
   22    scanf("%d",&k);
   23    h=k%13;
   24    addtolist(h,k);
   25
   26   }
   27   printlist();
   28  printf("\n\nEnter an element to be searched : ");
   29  scanf("%d",&j);
   30  r=j%13;
   31  flag=search_list(r,j);
   32  int ch=0;
   33  if(flag==0)
   34    {
   35         printf("Element not found!!!");
   36         printf("\n\n\tAdd it in the list (1 for y/2 no n) : ");
   37         scanf("%d",&ch);
   38         if(ch)
   39         addtolist(r,j);
   40         printlist();
   41    }
   42  else
   43    printf("Element found");
   44  printf("\nEnter the element to be deleted : ");
   45    scanf("%d",&ch);
   46   dell(ch);
   47
   48   printlist();
   49 }
    50
    51
    52 void addtolist(int index,int data1)
    53 {
    54  struct node *q,*r,*temp;
    55  temp=q=bucks[index];
    56  r=(struct node *)malloc(sizeof(struct node));
    57  r->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("\n");
    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("\nELement 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 }