Thread: Chained Hash Tables - Check if <key><data> exists

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    1

    Chained Hash Tables - Check if <key><data> exists

    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);
    }
    
    

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your main problem is that you have a "table" and a "hashTable". Get rid of the useless, confusing, global "hashTable" and just use "table", but make it an array of pointers to Nodes, initialized to NULL.

    And your malloc in insert is only allocating space for a pointer to a Node where it should be allocating a Node.

    You have a lot of bad names:
    snode: what does 's' stand for?
    temp: almost NEVER a good name.
    t: also meaningless
    option: basically unused ... isn't it the same as choice?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-23-2015, 08:49 PM
  2. Hash tables / hash maps
    By dlwlsdn in forum C Programming
    Replies: 3
    Last Post: 05-26-2010, 11:45 AM
  3. Help with Data structures: Hash tables
    By Shaun32887 in forum C++ Programming
    Replies: 18
    Last Post: 07-02-2008, 04:47 PM
  4. Chained hash tables using an array of lists
    By misswaleleia in forum C Programming
    Replies: 2
    Last Post: 05-26-2003, 09:33 PM
  5. hash tables
    By ender in forum C++ Programming
    Replies: 0
    Last Post: 05-08-2002, 07:36 AM

Tags for this Thread