I am trying to build ADT map using List and iterator. I got everything working but erase(key) function. I am writing erase(int key) function where I try to delete particular (key, value) from the list with erase(key) function. Following is my ease(key) function. What is the best way to approach this?
void Hash::erase(int key) {
int index = hashFunction(key);
list<Pair>::iterator itr;
bucketArrays[index].erase(bucketArrays[index].begin(), bucketArrays[index].end());
return;
}
Map.h
#ifndef MAP_H_
#define MAP_H_
#include<iostream>
#include<list>
#include<math.h>
using namespace std;
struct Pair {
int key;
int value;
Pair(int k, int v) : key(k), value(v){}
};
class Hash {
private:
int buckets;
list<Pair>* bucketArrays;
public:
Hash();
int hashFunction(int);
void put(int, int);
void find(int);
void erase(int);
void print();
};
Hash::Hash() {
buckets = 19;
bucketArrays = new list<Pair>[buckets];
}
int Hash::hashFunction(int k) {
return (abs((3 * k) + 2) % 19);
}
void Hash::put(int key, int value) {
int index = hashFunction(key);
bucketArrays[index].push_back(Pair(key, value));
return;
}
void Hash::find(int key) {
int count = 0;
int index = hashFunction(key);
list<Pair>::iterator itr;
for (itr = bucketArrays[index].begin(); itr != bucketArrays[index].end(); itr++) {
if (itr->key == key) {
cout << itr->value << endl;
}
}
}
void Hash::eraseValue(int key) {
int index = hashFunction(key);
list<Pair>::iterator itr;
bucketArrays[index].erase(bucketArrays[index].begin(), bucketArrays[index].end());
return;
}
void Hash::print() {
for (int i = 0; i < 19; i++) {
cout << "\n" << i << " |";
list<Pair>::iterator itr;
for (itr = bucketArrays[i].begin(); itr != bucketArrays[i].end(); itr++) {
int key = itr->key;
int value = itr->value;
if (itr == itr) {
cout << "->";
}
cout << "(" << key << "," << value << ")";
}
}
cout << "\n--------" << endl;
}
#endif // !MAP_H_
#include "Map.h"
#include <iostream>
using namespace std;
int main() {
Hash H;
H.put(24, 17);
H.put(32, 19);
H.put(52, 31);
H.put(62, 18);
H.put(41, 12);
H.put(12, 9);
H.put(17, 2);
H.put(18, 10);
H.put(2, 91);
H.put(13, 8);
H.put(60, 18);
H.put(31, 19);
H.print();
cout << "find(52): ";
H.find(52);
cout << "find(32): ";
H.find(32);
cout << "find(13): ";
H.find(13);
cout << "find(41): ";
H.find(41);
cout << "find(60): ";
H.find(60);
cout << "find(17): ";
H.find(17);
cout << "find(13): ";
H.find(13);
cout << "--------" << endl;
H.erase(2);
H.erase(60);
H.print();
/*
H.find(46);
H.erase(1);
H.find(52);
*/
return 0;
}
Console Display
0 |->(12,9)->(31,19)
1 |
2 |
3 |->(32,19)->(13,8)
4 |
5 |
6 |->(52,31)
7 |
8 |->(2,91)
9 |
10 |
11 |->(41,12)->(60,18)
12 |
13 |
14 |
15 |->(17,2)
16 |
17 |->(24,17)->(62,18)
18 |->(18,10)
--------
find(52): 31
find(32): 19
find(13): 8
find(41): 12
find(60): 18
find(17): 2
find(13): 8
--------
0 |->(12,9)->(31,19)
1 |
2 |
3 |->(32,19)->(13,8)
4 |
5 |
6 |->(52,31)
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |->(17,2)
16 |
17 |->(24,17)->(62,18)
18 |->(18,10)
--------
What else can I do with iterator ?
You don't need to re-implement find in erase. You push to put; you pop to erase. You find/peek to find. Are there no popping functions? You used push_back in put.
isn't erasing anything, just iterating the array "bucketArrays[index].erase" from the indexes specified.