To implement Separate Chaining in Hashing
#include<stdio.h>
#include<stdlib.h>
#define size 10
struct Node
{
int data;
struct Node *next;
};
typedef struct Node N;
N *ht[size];
void insertValue(int value)
{
int index=value%size;
N *newnode=(N*)malloc(sizeof(N));
newnode->data = value;
newnode->next = NULL;
if(ht[index] == NULL)
ht[index] = newnode;
else
{
N *temp = ht[index];
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
}
}
void displayValue()
{
for(int i=0;i<size;i++)
{
N *temp=ht[i];
if(temp==NULL)
printf("Null");
else
{
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
printf("\n");
}
}
int main()
{
for(int i=0;i<size-1;i++)
ht[i]=NULL;
int repeat = 1;
while(repeat)
{
printf("1.Insert \t 2.display \t 3.exit \n Enter your choice");
int ch;
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter value");
int value;
scanf("%d",&value);
insertValue(value);
break;
case 2:
printf("values are");
displayValue();
break;
case 3:
repeat = 0;
}
}
}