help needed in hash map stl program


#include <iostream>
#include <string.h>

using namespace std;

class Data {
public:
string key;
string value;
//constructors
Data()
{ key = ""; value = ""; }
Data(string k, string v)
{ key = k; value = v; }
};

class DataSlot {
public:
string index;
long size;
//constructors
DataSlot()
{ index = ""; size = 0; }
DataSlot(string k)
{ index = k; size = 0; }
};

class Element {
public:
Data d;
Element* prev;
Element* next;
//constructors
Element()
{ d.key = ""; d.value = ""; prev = NULL; next = NULL; }
Element(Data dat)
{ d = dat; prev = NULL; next = NULL; }
};

class Slot {
public:
DataSlot d;
Slot* prev;
Slot* next;
Element* head;

//constructors
Slot()
{ d.index = ""; d.size = 0; prev = NULL; next = NULL; head = NULL; }
Slot(DataSlot dat)
{ d = dat; prev = NULL; next = NULL; head = NULL; }
//insert element pointed by x at the beginning of list of elements
void insert(Element* x)
{
x->next = this->head;
this->head = x;
if (x->next != NULL) //if there were other elements
x->next->prev = x;
d.size++;
}
//remove element pointed by x from list of elements
void remove(Element* x)
{
if (x->next != NULL) //if not the last one
x->next->prev = x->prev;
if (x->prev != NULL) //if not the first one
x->prev->next = x->next;
else //if first one
this->head = x->next;
delete x; //deallocate x
d.size--;
}
//destructor
~Slot()
{ while (this->head != NULL) remove(this->head); }
};

class HashMap {
public:
long size;
Slot* head;
//constructor
HashMap()
{ size = 0; head = NULL; }
//insert slot pointed by s at the begining of hash map
void insert(Slot* s)
{
s->next = this->head;
this->head = s;
if (s->next != NULL) { //if there were other slots
s->next->prev = s;
}
size++;
}

//insert element pointed by x in slot pointed by s
void insert(Slot* s, Element* x)
{ s->insert(x); }

//remove slot pointed by s from hash map
void remove(Slot* s)
{
if (s->next != NULL) //if not the last one
s->next->prev = s->prev;
if (s->prev != NULL) //if not the first one
s->prev->next = s->next;
else //if first one
this->head = s->next;
while (s->head != NULL) //delete all elements from slot s
s->remove(s->head);
delete s; //deallocate s
size--;
}

//remove element pointed by x from slot pointed by s
void remove(Slot* s, Element* x) {
s->remove(x);
}
//display HashMap content
void display() const
{
Slot* p;
Element* q;
p = this->head;
while (p != NULL) {
cout << p->d.index << "," << p->d.size << endl;
q = p->head;
while (q != NULL) {
cout << " " << q->d.key << "," << q->d.value << endl;
q = q->next;
}
p = p->next;
}
}
//destructor
~HashMap()
{
while (this->head != NULL)
this->remove(this->head);
}
};

int main()
{
DataSlot ds1("slot1");
DataSlot ds2("slot2");
Data d1("key1","info1");
Data d2("key2","info2");
Data d3("key3","info3");
Slot* s1 = new Slot(ds1);
Slot* s2 = new Slot(ds2);
Element* e1 = new Element(d1);
Element* e2 = new Element(d2);
Element* e3 = new Element(d3);

HashMap* hm = new HashMap;
cout << "================================" << endl;
cout << "Start test..." << endl;
cout << "________________________________" << endl;
cout << "Insert " << s1->d.index << endl;
hm->insert(s1);
hm->display();
cout << "________________________________" << endl;
cout << "Insert " << e1->d.key << "," << e1->d.value << " in " << s1->d.index << endl;
hm->insert(s1,e1);
hm->display();
cout << "________________________________" << endl;
cout << "Insert " << s2->d.index << endl;
hm->insert(s2);
hm->display();
cout << "________________________________" << endl;
cout << "Insert " << e2->d.key << "," << e2->d.value << " in " << s2->d.index << endl;
hm->insert(s2,e2);
hm->display();
cout << "________________________________" << endl;
cout << "Insert " << e3->d.key << "," << e3->d.value << " in " << s2->d.index << endl;
hm->insert(s2,e3);
hm->display();

cout << "================================" << endl;
cout << "Delete HashMap..." << endl;
cout << "________________________________" << endl;
cout << "Remove " << s1->d.index << " and all elements" << endl;
hm->remove(s1);
hm->display();
cout << "________________________________" << endl;
cout << "remove " << e2->d.key << " from " << s2->d.index << endl;
hm->remove(s2,e2);
hm->display();
cout << "________________________________" << endl;
cout << "remove " << s2->d.index << " and all remaining elements." << endl;
hm->remove(s2);
hm->display();
cout << "================================" << endl;

delete hm;

return 0;
}

I need yo complete my project in time and i am still getting confused about the
following questions,

1. How to adapt HashMap to include attribute data templates. Templates should allow for instantiation of objects of any data type (although eventually the data type will be DataSlot and Data);

2. How to adapt HashMap to handle exceptions to bad allocation of slots and elements (and possibly to other exceptions you may find critical);

3. How to adapt DataSlot and Data classes to contain attributes from the real-world data .


Please use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

You need to ask a more specific question. What exactly is the first thing you're confused with?
Topic archived. No new replies allowed.