"Z Code Writer" is an Online Blog for Programmer & Beginner. Here, You will Find tech, Build your Programming Skills and other Programming Language Like C, C++, HTML, Java Script Java Code, Web Designing and University Of Sialkot (USKT) CGPA / GPA Calculator etc.

Saturday, 21 December 2019

Implementation Of Hash Table Code

Implementation Of Hash Table Code


#include<iostream>
using namespace std;
// Create an Entity
class Entry{
public:
            int key;
            int value;
Entry(int k,int v){
            key= k;
            value = v;
}
};
class HashTable{
Entry **temp;
            public:
            int size;
//Constructor
            HashTable() {
                        cout<<" Enter Size of Table: ";
                        cin>>size;
                        temp = new Entry * [size];
                        for (int i = 0; i< size; i++) {
                        temp[i] = NULL;
                        }
            }
// Hashing Funtion
int HashFun(int k) {
        return k % size;
    }
// Insertion In Hashing
void Insert(int k,int v){
            if(k<=size && k>0){
int h = HashFun(k);
        while(temp[h] != NULL && temp[h]->key == k){
        h = HashFun(h + 1);
        }
        if(temp[h] != NULL)
            delete temp[h];
        temp[h] = new Entry(k, v);
        }
    else{
            cout<<" You Are Enter Invalid Key "<<endl<<endl;
}
    }
// Searching In Hashing
            int SearchKey(int k){
                        int h = HashFun(k);
while (temp[h] != NULL && temp[h]->key != k){
                        h = HashFun(h + 1);
                        }
                        if(temp[h] == NULL){
                        return -1;
                        }
                        else{
                        return temp[h]->value;
                        }
            }
// Delection In Hashing
void Delete(int k){
            int h = HashFun(k);
while (temp[h] != NULL){
                                    if(temp[h]->key == k)
                                    break;
                                    h = HashFun(h + 1);
                        }
                        if (temp[h] == NULL){
                                    cout<<" No Element Found at Key "<<k<<endl<<endl;
                        return;
                        }
                        else{
                                    delete temp[h];
                        }
            cout<<" Element Successfuly Deleted "<<endl<<endl;
            }
};

int main(){
            HashTable h;
            int k, v;
            int c;
                        while(1){
                                    cout<<endl;
                                    cout<<" 1. Insert Element into Table"<<endl;
                                    cout<<" 2. Search Element from Key"<<endl;
                                    cout<<" 3. Delete Element from Key"<<endl;
                                    cout<<" 4. Exit"<<endl;
                                    cout<<"\nEnter your Choice: ";
                                    cin>>c;
            switch(c){
                        case 1:
                                    cout<<"Enter Element to Insert: ";
                                    cin>>v;
                                    cout<<"Enter key at which Element to be Insert: ";
                                    cin>>k;
                                    h.Insert(k, v);
                                    break;
                        case 2:
                                    cout<<"Enter key which Element you want to Search: ";
                                    cin>>k;
                                                if(h.SearchKey(k) == -1){
                                                            cout<<" No Element Found at Key "<<k<<endl<<endl;
                                                            continue;
                                                }
                                    else{
                                                cout<<" Element at Key "<<k<<" : ";
                                                cout<<h.SearchKey(k)<<endl<<endl;
                                    }
                                    break;
                        case 3:
                                    cout<<"\n Enter Key which Element you want to Delete: ";
                                    cin>>k;
                                    h.Delete(k);
                                    break;
                        case 4:
                                    exit(1);
                        default:
                                    cout<<"\n Please Enter Correct Option\n";
                        }
            }
   return 0;
}


No comments:

Post a Comment

Thanks For Visiting Here...!! I will Reply you as soon as possible.