From 5d446f5820f979fff2c8c6bf862192fed69b0a7b Mon Sep 17 00:00:00 2001 From: MohamedKamal98 <32045725+MohamedKamal98@users.noreply.github.com> Date: Fri, 12 Oct 2018 06:41:14 +0200 Subject: [PATCH 1/3] Create Read me --- data_structures/Hash Map/Read me | 1 + 1 file changed, 1 insertion(+) create mode 100644 data_structures/Hash Map/Read me diff --git a/data_structures/Hash Map/Read me b/data_structures/Hash Map/Read me new file mode 100644 index 0000000..b4a83d8 --- /dev/null +++ b/data_structures/Hash Map/Read me @@ -0,0 +1 @@ +sadfasfds From 2024e16734247012906a5c5b4ce3d5baf18563cd Mon Sep 17 00:00:00 2001 From: MohamedKamal98 <32045725+MohamedKamal98@users.noreply.github.com> Date: Fri, 12 Oct 2018 06:41:37 +0200 Subject: [PATCH 2/3] Add files via upload --- data_structures/Hash Map/HashMap.cpp | 72 ++++++++++++++++++++++++++++ data_structures/Hash Map/HashMap.h | 31 ++++++++++++ data_structures/Hash Map/Source.cpp | 54 +++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 data_structures/Hash Map/HashMap.cpp create mode 100644 data_structures/Hash Map/HashMap.h create mode 100644 data_structures/Hash Map/Source.cpp diff --git a/data_structures/Hash Map/HashMap.cpp b/data_structures/Hash Map/HashMap.cpp new file mode 100644 index 0000000..7455fc9 --- /dev/null +++ b/data_structures/Hash Map/HashMap.cpp @@ -0,0 +1,72 @@ +#include "HashMap.h" + + + +HashMap::HashMap() +{ + table = new HashEntry *[TABLE_SIZE]; + for (int i = 0; i < TABLE_SIZE; i++) + { + table[i] = NULL; + } +} + +int HashMap::HashFunc(int key) +{ + return key % TABLE_SIZE; +} +void HashMap::Insert(int key, int value) +{ + int hash = HashFunc(key); + while (table[hash] != NULL && table[hash]->key != key) + { + hash = HashFunc(hash + 1); + } + if (table[hash] != NULL) + delete table[hash]; + table[hash] = new HashEntry(key, value); +} + +int HashMap::Search(int key) +{ + int hash = HashFunc(key); + while (table[hash] != NULL && table[hash]->key != key) + { + hash = HashFunc(hash + 1); + } + if (table[hash] == NULL) + return -1; + else + return table[hash]->value; +} + +void HashMap::Remove(int key) +{ + int hash = HashFunc(key); + while (table[hash] != NULL) + { + if (table[hash]->key == key) + break; + hash = HashFunc(hash + 1); + } + if (table[hash] == NULL) + { + cout << "No Element found at key " << key << endl; + return; + } + else + { + delete table[hash]; + } + cout << "Element Deleted" << endl; +} + +HashMap::~HashMap() +{ + for (int i = 0; i < TABLE_SIZE; i++) + { + if (table[i] != NULL) + delete table[i]; + delete[] table; + } +} diff --git a/data_structures/Hash Map/HashMap.h b/data_structures/Hash Map/HashMap.h new file mode 100644 index 0000000..7f3ad14 --- /dev/null +++ b/data_structures/Hash Map/HashMap.h @@ -0,0 +1,31 @@ +#pragma once +#include +#include +#include +#include +using namespace std; +const int TABLE_SIZE = 128; + +class HashEntry +{ +public: + int key; + int value; + HashEntry(int key, int value) + { + this->key = key; + this->value = value; + } +}; + +class HashMap +{ + HashEntry **table; +public: + HashMap(); + int HashFunc(int key); + void Insert(int key, int value); + int Search(int key); + void Remove(int key); + ~HashMap(); +}; diff --git a/data_structures/Hash Map/Source.cpp b/data_structures/Hash Map/Source.cpp new file mode 100644 index 0000000..5c2bc03 --- /dev/null +++ b/data_structures/Hash Map/Source.cpp @@ -0,0 +1,54 @@ +#include "HashMap.h" + +int main() +{ + HashMap hash; + int key, value; + int choice; + while (1) + { + cout << "\n----------------------" << endl; + cout << "Operations on Hash Table" << endl; + cout << "\n----------------------" << endl; + cout << "1.Insert element into the table" << endl; + cout << "2.Search element from the key" << endl; + cout << "3.Delete element at a key" << endl; + cout << "4.Exit" << endl; + 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.Search(key) == -1) + { + cout << "No element found at key " << key << endl; + continue; + } + else + { + cout << "Element at key " << key << " : "; + cout << hash.Search(key) << endl; + } + break; + case 3: + cout << "Enter key of the element to be deleted: "; + cin >> key; + hash.Remove(key); + break; + case 4: + exit(1); + default: + cout << "\nEnter correct option\n"; + } + } + return 0; +} \ No newline at end of file From 8828b4418dc759ac966d4ae86228f0f31dde6c02 Mon Sep 17 00:00:00 2001 From: MohamedKamal98 <32045725+MohamedKamal98@users.noreply.github.com> Date: Fri, 12 Oct 2018 06:42:00 +0200 Subject: [PATCH 3/3] Delete Read me --- data_structures/Hash Map/Read me | 1 - 1 file changed, 1 deletion(-) delete mode 100644 data_structures/Hash Map/Read me diff --git a/data_structures/Hash Map/Read me b/data_structures/Hash Map/Read me deleted file mode 100644 index b4a83d8..0000000 --- a/data_structures/Hash Map/Read me +++ /dev/null @@ -1 +0,0 @@ -sadfasfds