//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);
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 .