I'm creating a database with chained hash tables using linked lists for the chaining feature. In this code the idea is that the user can introduce data about citizens but each one has a unique ID, then if the user introduces an ID that is already in the memory it has to print out an error. But I don't really know where in the code I'm getting mistakes iterating the linked list. The problem is where it asks for the ID and it shouldn't let you insert an already existing ID but for some reason it doesn't work. The problem is where the user input the ID and it loops through the hash table to check if that ID already exists but it seems that doesn't work.
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 10
#define MAX 256
#define IDLENGTH 8
/*
*
*/
struct snode{
int key;
int id;
struct snode *next;
};
struct hash
{
struct snode *head;
};
struct hash *hashTable = NULL;
void init(struct snode table[])
{
int j=0;
while(j<N)
{
table[j].next = NULL;
table[j].id =-1;
table[j].key =-1;
j++;
}
}
int numberofdigits(int temp)
{
int count =0;
while(temp !=0)
{
temp /=10;
count++;
}
return count;
}
void insert(struct snode table[])
{
int temp;
int t=1;
int digits;
struct snode *node;
while(t)
{
printf("Insert a citizen ID: ");
scanf("%d",&temp);
getchar();
digits = numberofdigits(temp);
if(digits == IDLENGTH && temp>0)
t =0;
else
printf("Citizen ID must be 8 numbers long and bigger than 0000000. Try again. \n");
node = hashTable[temp%N].head;
while(node!=NULL)
{
if(node->id == temp)
{
printf("ID already in use. Try again.\n");
t=1;
}
}
}
if(table[temp % N].id ==-1)
{
table[temp % N].key = temp % N;
table[temp % N].id = temp;
hashTable[temp%N].head=table;
printf("\n");
printf("ID: %d\tKey: %d\n\n",table[temp%N].id,table[temp%N].key);
}
else
{
struct snode *link;
link =(struct snode *)malloc(sizeof(struct snode*));
link->key = temp %N;
link->id = temp;
link->next = NULL;
table[temp%N].next = link;
hashTable[temp%N].head=table;
printf("\n");
printf("ID: %d\tKey: %d\n\n",link->id,link->key);
}
}
void display(struct snode table[])
{
printf("ID\t\tKey\n");
for(int i=0;i<N;i++)
{
if(table[i].id>0)
{
printf("%d\t%d\n",table[i].id,table[i].key);
struct snode *pntr;
pntr = table[i].next;
while(pntr != NULL)
{
printf("%d\t%d\n",pntr->id,pntr->key);
pntr = pntr -> next;
}
printf("\n");
}
}
}
int main()
{
int choice, option =1,j=0;
struct snode table[N];
hashTable =(struct hash*)calloc(N,sizeof(struct hash));
printf("==========================================\n");
printf("Welcome to the Population DataBase Program\n");
printf("==========================================\n\n");
init(table);
while(option)
{
printf("INSERT ------------------------- 1\n");
printf("DISPLAY ------------------------- 2\n");
printf("EXIT ------------------------- 3\n");
printf("\nWhich option do you want to perform? ");
scanf("%d",&choice);
switch(choice)
{
case1:
insert(table);
break;
case2:
display(table);
break;
case3:
exit(0);
default:
printf("Wrong option!\n");
break;
}
printf("Do you want to continue? (1 TO CONTINUE - 0 TO EXIT) ");
scanf("%d",&option);
}
return(EXIT_SUCCESS);
}