0% found this document useful (0 votes)
41 views4 pages

Hashmap Implementation

The document defines a MapNode class template that represents a node in a linked list with a key, value, and next node pointer. It also defines an ourmap class template that implements a hash map with chaining. The ourmap uses an array of buckets, where each bucket is a linked list of MapNodes. It provides functions to initialize the map, get the size, get a value by key, insert a key-value pair, and remove a key-value pair.

Uploaded by

Resham Saharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views4 pages

Hashmap Implementation

The document defines a MapNode class template that represents a node in a linked list with a key, value, and next node pointer. It also defines an ourmap class template that implements a hash map with chaining. The ourmap uses an array of buckets, where each bucket is a linked list of MapNodes. It provides functions to initialize the map, get the size, get a value by key, insert a key-value pair, and remove a key-value pair.

Uploaded by

Resham Saharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <string>

using namespace std;

template <typename V>


class MapNode {
public:
string key;
V value;
MapNode* next;

MapNode(string key, V value) {


this->key = key;
this->value = value;
next = NULL;
}

~MapNode() {
delete next;
}
};

template <typename V>


class ourmap {
MapNode<V>** buckets;
int count;
int numBuckets;

public:
ourmap() {
count = 0;
numBuckets = 5;
buckets = new MapNode<V>*[numBuckets];
for (int i = 0; i < numBuckets; i++) {
buckets[i] = NULL;
}
}

~ourmap() {
for (int i = 0; i < numBuckets; i++) {
delete buckets[i];
}
delete [] buckets;
}

int size() {
return count;
}

V getValue(string key) {
int bucketIndex = getBucketIndex(string key);
MapNode<V>* head = buckets[bucketIndex];
while (head != NULL) {
if (head->key == key) {
return head->value;
}
head = head->next;
}
return 0;
}

private:
int getBucketIndex(string key) {
int hashCode = 0;

int currentCoeff = 1;
for (int i = key.length() - 1; i >= 0; i--) {
hashCode += key[i] * currentCoeff;
hashCode = hashCode % numBuckets;
currentCoeff *= 37;
currentCoeff = currentCoeff % numBuckets;
}

return hashCode % numBuckets;


}

public:
void insert(string key, V value) {
int bucketIndex = getBucketIndex(string key);
MapNode<V>* head = buckets[bucketIndex];
while (head != NULL) {
if (head->key == key) {
head->value = value;
return;
}
head = head->next;
}
head = buckets[bucketIndex];
MapNode<V>* node = new MapNode<V>(key, value);
node->next = head;
buckets[bucketIndex] = node;
count++;
}

V remove(string key) {
int bucketIndex = getBucketIndex(string key);
MapNode<V>* head = buckets[bucketIndex];
MapNode<V>* prev = NULL;
while (head != NULL) {
if (head->key == key) {
if (prev == NULL) {
buckets[bucketIndex] = head->next;
} else {
prev->next = head->next;
}
V value = head->value;
head->next = NULL;
delete head;
count--;
return value;
}
prev = head;
head = head->next;
}
return 0;
}

};
#include <string>
using namespace std;

template <typename V>


class MapNode {
public:
string key;
V value;
MapNode* next;

MapNode(string key, V value) {


this->key = key;
this->value = value;
next = NULL;
}

~MapNode() {
delete next;
}
};

template <typename V>


class ourmap {
MapNode<V>** buckets;
int count;
int numBuckets;

public:
ourmap() {
count = 0;
numBuckets = 5;
buckets = new MapNode<V>*[numBuckets];
for (int i = 0; i < numBuckets; i++) {
buckets[i] = NULL;
}
}

~ourmap() {
for (int i = 0; i < numBuckets; i++) {
delete buckets[i];
}
delete [] buckets;
}

int size() {
return count;
}

V getValue(string key) {

private:
int getBucketIndex(string key) {
int hashCode = 0;

int currentCoeff = 1;
for (int i = key.length() - 1; i >= 0; i--) {
hashCode += key[i] * currentCoeff;
hashCode = hashCode % numBuckets;
currentCoeff *= 37;
currentCoeff = currentCoeff % numBuckets;
}

return hashCode % numBuckets;


}

public:
void insert(string key, V value) {
int bucketIndex = getBucketIndex(string key);
MapNode<V>* head = buckets[bucketIndex];
while (head != NULL) {
if (head->key == key) {
head->value = value;
return;
}
head = head->next;
}
head = buckets[bucketIndex];
MapNode<V>* node = new MapNode<V>(key, value);
node->next = head;
buckets[bucketIndex] = node;
count++;
}

};

You might also like