Friday 24 November 2017

C++ Program to Implement Hash Tables Chaining with List Heads


Code:

#include   iostream
#include   cstdio
#include   cstring
#include   algorithm
#include   cmath
#include   vector
#include   cstdlib
using namespace std;
const int TABLE_SIZE = 5;
/*
 * Link List Class Declaration
 */
class LinkedHash
{
public:
        int key, value;
        LinkedHash *next;
        LinkedHash(int key, int value)
        {
            this->key = key;
            this->value = value;
            this->next = NULL;
        }
};
/*
 * HashMap Class Declaration
 */
class HashMap
{
    private:
        LinkedHash **htable;
    public:
        HashMap()
        {
            htable = new LinkedHash*[TABLE_SIZE];
            for (int i = 0; i < TABLE_SIZE; i++)
            {
                htable[i] = NULL;
            }
        }
        ~HashMap()
        {
            for (int i = 0; i < TABLE_SIZE; i++)
            {
                if (htable[i] != NULL)
                {
                    LinkedHash *prev = NULL;
                    LinkedHash *entry = htable[i];
                    while (entry != NULL)
                    {
                        prev = entry;
                        entry = entry->next;
                        delete prev;
                    }
                }
                delete[] htable;
            }
        }
        /*
     * Hash Function
         */
        int HashFunc(int key)
{
            return key % TABLE_SIZE;
        }
        /*
     * Insert Element at a key
         */
        void Insert(int key, int value)
        {
            int hash_val = HashFunc(key);
            if (htable[hash_val] == NULL)
                htable[hash_val] = new LinkedHash(key, value);
            else
            {
                LinkedHash *entry = htable[hash_val];
                while (entry->next != NULL)
                    entry = entry->next;
                    if (entry->key == key)
                        entry->value = value;
                    else
                        entry->next = new LinkedHash(key, value);
            }
        }
        /*
     * Search Element at a key
         */
        int Find(int key)
        {
            int hash_val = HashFunc(key);
            if (htable[hash_val] == NULL)
                return -1;
            else
            {
                LinkedHash *entry = htable[hash_val];
                while (entry != NULL && entry->key != key)
                    entry = entry->next;
                if (entry == NULL)
                    return -1;
                else
                    return entry->value;
            }
        }
        /*
     * Delete Element at a key
         */
        void Delete(int key)
        {
            int hash_val = HashFunc(key);
            if (htable[hash_val] != NULL)
            {
                LinkedHash *entry = htable[hash_val];
                LinkedHash *prev = NULL;
                while (entry->next != NULL && entry->key != key)
                {
                    prev = entry;
                    entry = entry->next;
                }
                if (entry->key == key)
                {
                    if (prev == NULL)
                    {
                        LinkedHash *next = entry->next;
                        delete entry;
                        htable[hash_val] = next;
                    }
                    else
                    {
                        LinkedHash *next = entry->next;
                        delete entry;
                        prev->next = next;
                    }
                }
            }
        }
};
/*
 * Main Contains Menu
 */
int main()
{
    HashMap hash;
    int key, value;
    int choice;
    while(1)
    {
        cout<<"\n----------------------"<
        cout<<"Operations on Hash Table"<
        cout<<"\n----------------------"<
        cout<<"1.Insert element into the table"<
        cout<<"2.Search element from the key"<
        cout<<"3.Delete element at a key"<
        cout<<"4.Exit"<
        cout<<"Enter your choice: ";
        cin>>choice;
        switch(choice)
        {
        case 1:
            cout<<"Enter element to be inserted: ";
            cin>>value;
            cout<<"Enter key at which element to be inserted: ";
            cin>>key;
            hash.Insert(key, value);
            break;
        case 2:
            cout<<"Enter key of the element to be searched: ";
            cin>>key;
            if (hash.Find(key) == -1)
            cout<<"No element found at key "<
            else
            {
                cout<<"Elements at key "<
                cout<
            }
            break;
        case 3:
            cout<<"Enter key of the element to be deleted: ";
            cin>>key;
            if (hash.Find(key) == -1)
                cout<<"Key "<
            else
            {
                hash.Delete(key);
                cout<<"Entry Deleted"<
            }
            break;
        case 4:
            exit(1);
        default:
           cout<<"\nEnter correct option\n";
       }
    }
    return 0;
}


Output:

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 100
Enter key at which element to be inserted: 1

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 200
Enter key at which element to be inserted: 2

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 300
Enter key at which element to be inserted: 3

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 600
Enter key at which element to be inserted: 6

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 700
Enter key at which element to be inserted: 7

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 800
Enter key at which element to be inserted: 8

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 2
Elements at key 2 : 200

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 7
Elements at key 7 : 700

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 9
No element found at key 9

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 3
Enter key of the element to be deleted: 7
Entry Deleted

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 7
No element found at key 7

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 2
Elements at key 2 : 200

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter element to be inserted: 1111
Enter key at which element to be inserted: 7

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter key of the element to be searched: 7
Elements at key 7 : 1111

----------------------
Operations on Hash Table

----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 4

------------------
(program exited with code: 1)
Press return to continue



More C++ Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...