Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Hash Table in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 69
    Comment on it

    Hash table is data structure to store or retrieve data in key value pair format. So it is like a table where key and value pertaining to that key exists . So, it is an efficient way of mapping and accessing data.

     

    [key 1] -> Data 1
    [key 2] -> Data 2
    [key 3] -> Data 3

     

     

    Hash table working:

    • Hash Function - It is the function to search data in the hash table.
    • Key Value Pair - The Key-Value pair is the primary thing of the hash table.
    • Collisions - Collision is for that scenario when we have more than one key to store for a value.

     

     

    void deleteEntry(char* state)
    {
        int index = hashForKey(state);
        if (index < 0)
        {
            printf("Error deleting entry\n");
        }
        code[index] = 0;
    }
    
    void initHashTable ()
    {
        int i;
        for (i = 0; i < TOTAL; i++)
            code[i] = 0;
    }
    
    int getCode( char *state)
    {
        int index = hashForKey(state);
        if (index < 0)
        {
            printf("Error getting state code\n");
        }
    
        return code[index];
    }
    
    int main()
    {
        char state[10][3] = {""};
        int codeNum[10];
        int i;
        int codeObtained = 0;
    
        for (i = 0; i < 10; i+=2)
        {
            printf ("Enter state (should be 2 chars in upper case)\n");
            scanf("%s", state[i]);
    
            printf ("Enter its code (integral - 3 numbers)\n");
            scanf("%d", &codeNum[i]);
    
            createEntry(state[i], codeNum[i]);
        }
    
        for (i = 9; i >=0; i--)
        {
            codeObtained = getCode(state[i]);
            printf("%s \t %d\n", state[i], codeObtained);
        }
    
        return 0;
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: