|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +// template for generic type |
| 5 | +template <typename K, typename V> |
| 6 | + |
| 7 | +// Hashnode class |
| 8 | +class HashNode { |
| 9 | +public: |
| 10 | + V value; |
| 11 | + K key; |
| 12 | + |
| 13 | + // Constructor of hashnode |
| 14 | + HashNode(K key, V value) |
| 15 | + { |
| 16 | + this->value = value; |
| 17 | + this->key = key; |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +// template for generic type |
| 22 | +template <typename K, typename V> |
| 23 | + |
| 24 | +// Our own Hashmap class |
| 25 | +class HashMap { |
| 26 | + // hash element array |
| 27 | + HashNode<K, V>** arr; |
| 28 | + int capacity; |
| 29 | + // current size |
| 30 | + int size; |
| 31 | + // dummy node |
| 32 | + HashNode<K, V>* dummy; |
| 33 | + |
| 34 | +public: |
| 35 | + HashMap() |
| 36 | + { |
| 37 | + // Initial capacity of hash array |
| 38 | + capacity = 20; |
| 39 | + size = 0; |
| 40 | + arr = new HashNode<K, V>*[capacity]; |
| 41 | + |
| 42 | + // Initialise all elements of array as NULL |
| 43 | + for (int i = 0; i < capacity; i++) |
| 44 | + arr[i] = NULL; |
| 45 | + |
| 46 | + // dummy node with value and key -1 |
| 47 | + dummy = new HashNode<K, V>(-1, -1); |
| 48 | + } |
| 49 | + // This implements hash function to find index |
| 50 | + // for a key |
| 51 | + int hashCode(K key) |
| 52 | + { |
| 53 | + return key % capacity; |
| 54 | + } |
| 55 | + |
| 56 | + // Function to add key value pair |
| 57 | + void insertNode(K key, V value) |
| 58 | + { |
| 59 | + HashNode<K, V>* temp = new HashNode<K, V>(key, value); |
| 60 | + |
| 61 | + // Apply hash function to find index for given key |
| 62 | + int hashIndex = hashCode(key); |
| 63 | + |
| 64 | + // find next free space |
| 65 | + while (arr[hashIndex] != NULL |
| 66 | + && arr[hashIndex]->key != key |
| 67 | + && arr[hashIndex]->key != -1) { |
| 68 | + hashIndex++; |
| 69 | + hashIndex %= capacity; |
| 70 | + } |
| 71 | + |
| 72 | + // if new node to be inserted |
| 73 | + // increase the current size |
| 74 | + if (arr[hashIndex] == NULL |
| 75 | + || arr[hashIndex]->key == -1) |
| 76 | + size++; |
| 77 | + arr[hashIndex] = temp; |
| 78 | + } |
| 79 | + |
| 80 | + // Function to delete a key value pair |
| 81 | + V deleteNode(int key) |
| 82 | + { |
| 83 | + // Apply hash function |
| 84 | + // to find index for given key |
| 85 | + int hashIndex = hashCode(key); |
| 86 | + |
| 87 | + // finding the node with given key |
| 88 | + while (arr[hashIndex] != NULL) { |
| 89 | + // if node found |
| 90 | + if (arr[hashIndex]->key == key) { |
| 91 | + HashNode<K, V>* temp = arr[hashIndex]; |
| 92 | + |
| 93 | + // Insert dummy node here for further use |
| 94 | + arr[hashIndex] = dummy; |
| 95 | + |
| 96 | + // Reduce size |
| 97 | + size--; |
| 98 | + return temp->value; |
| 99 | + } |
| 100 | + hashIndex++; |
| 101 | + hashIndex %= capacity; |
| 102 | + } |
| 103 | + |
| 104 | + // If not found return null |
| 105 | + return NULL; |
| 106 | + } |
| 107 | + |
| 108 | + // Function to search the value for a given key |
| 109 | + V get(int key) |
| 110 | + { |
| 111 | + // Apply hash function to find index for given key |
| 112 | + int hashIndex = hashCode(key); |
| 113 | + int counter = 0; |
| 114 | + |
| 115 | + // finding the node with given key |
| 116 | + while (arr[hashIndex] != NULL) { // int counter =0; // BUG! |
| 117 | + |
| 118 | + if (counter++ > capacity) // to avoid infinite loop |
| 119 | + return NULL; |
| 120 | + |
| 121 | + // if node found return its value |
| 122 | + if (arr[hashIndex]->key == key) |
| 123 | + return arr[hashIndex]->value; |
| 124 | + hashIndex++; |
| 125 | + hashIndex %= capacity; |
| 126 | + } |
| 127 | + |
| 128 | + // If not found return null |
| 129 | + return NULL; |
| 130 | + } |
| 131 | + |
| 132 | + // Return current size |
| 133 | + int sizeofMap() |
| 134 | + { |
| 135 | + return size; |
| 136 | + } |
| 137 | + |
| 138 | + // Return true if size is 0 |
| 139 | + bool isEmpty() |
| 140 | + { |
| 141 | + return size == 0; |
| 142 | + } |
| 143 | + |
| 144 | + // Function to display the stored key value pairs |
| 145 | + void display() |
| 146 | + { |
| 147 | + for (int i = 0; i < capacity; i++) { |
| 148 | + if (arr[i] != NULL && arr[i]->key != -1) |
| 149 | + cout << "key = " << arr[i]->key |
| 150 | + << " value = " |
| 151 | + << arr[i]->value << endl; |
| 152 | + } |
| 153 | + } |
| 154 | +}; |
| 155 | + |
| 156 | +// Driver method to test map class |
| 157 | +int main() |
| 158 | +{ |
| 159 | + HashMap<int, int>* h = new HashMap<int, int>; |
| 160 | + h->insertNode(1, 1); |
| 161 | + h->insertNode(2, 2); |
| 162 | + h->insertNode(2, 3); |
| 163 | + h->display(); |
| 164 | + cout << h->sizeofMap() << endl; |
| 165 | + cout << h->deleteNode(2) << endl; |
| 166 | + cout << h->sizeofMap() << endl; |
| 167 | + cout << h->isEmpty() << endl; |
| 168 | + cout << h->get(2); |
| 169 | + |
| 170 | + return 0; |
| 171 | +} |
0 commit comments