Bucket Hashing
#include < fstream.h >
#include < iostream.h >
#include < stdlib.h >
#include < stdio.h >
class Object
{
private:
int number;
public:
Object(char *x) { number = atoi(x); }
Object(int x) { number = x; }
int valueOf() { return number; }
Object *copyOf() { return new Object(number); }
void display() { cout << number << " "; }
};
class Cell
{
public:
Cell (Object *obj, Cell *nxt)
{
object = obj;
next = nxt;
}
void setNext(Cell *n) { next = n; }
Cell *getNext() { return next; }
Object *objectOf() { return object; }
Object *copyOf() { return object->copyOf(); }
private:
Object *object;
Cell *next;
};
class List
{
public:
List (int (* cmp)(Object *, char *))
{
head = tail = NULL;
cmpfunc = cmp;
}
void add (Object *obj)
{
if (head == NULL)
{
head = tail = new Cell(obj, NULL);
return;
}
tail->setNext(new Cell(obj, NULL));
tail = tail->getNext();
}
Object *lookup (char *id)
{
for (Cell *p=head ; p != NULL ; p = p->getNext())
if ((cmpfunc)(p->objectOf(), id)) return p->copyOf();
return NULL;
}
Object *pop (char *id)
{
Object *obj;
Cell *ptr;
if ((cmpfunc)(head->objectOf(), id))
{
obj = head->objectOf();
ptr = head;
head = head->getNext();
delete ptr;
return obj;
}
for (Cell *p=head ; p->getNext() != NULL; p = p->getNext())
{
if ((cmpfunc)(p->getNext()->objectOf(), id))
{
obj = p->getNext()->objectOf();
ptr = p->getNext();
p->setNext(p->getNext()->getNext());
delete ptr;
return obj;
}
}
return NULL;
}
void display()
{
for (Cell *p=head ; p != NULL ; p = p->getNext())
p->objectOf()->display();
cout << "\n";
}
private:
Cell *head;
Cell *tail;
int (* cmpfunc)(Object *, char *);
};
class HashTable
{
private:
List **table;
int size;
int (* key)(Object *);
int (* cmp)(Object *, char *);
public:
HashTable(int sz, int (* k)(Object *), int (* c)(Object *, char *))
{
table = new (List *)[sz];
size = sz;
key = k;
cmp = c;
for (int i=0 ; i < size ; i++) table[i] = NULL;
}
int locate(Object *obj) { return (key)(obj); }
Object *add(Object *obj)
{
if (table[locate(obj)] == NULL)
table[locate(obj)] = new List(cmp);
table[locate(obj)]->add(obj);
return obj;
}
Object *lookup (char *id)
{
Object *num = new Object(id);
if (table[locate(num)] == NULL) return NULL;
return table[locate(num)]->lookup(id);
}
Object *pop (char *id)
{
Object *num = new Object(id);
if (table[locate(num)] == NULL) return NULL;
return table[locate(num)]->pop(id);
}
void display()
{
int flag = 0;
int i;
cout << "--Display--\n";
for (i=0 ; i < size ; i++)
{
if (table[i] != NULL)
{
flag=1;
cout << "[" << i << "] ";
table[i]->display();
}
}
if (!flag) cout << "Hash Table Empty\n";
cout << "-----------\n";
}
};
int h1(Object *obj)
{
return obj->valueOf() % 100;
}
int cf(Object *obj, char *s)
{
return (obj->valueOf() == atoi(s));
}
void main()
{
HashTable *h = new HashTable(100, h1, cf);
h->display();
cout << "Add: " << h->add(new Object(12))->valueOf() << "\n";
cout << "Add: " << h->add(new Object(264))->valueOf() << "\n";
h->display();
cout << "Add: " << h->add(new Object(112))->valueOf() << "\n";
h->display();
cout << "Add: " << h->add(new Object(212))->valueOf() << "\n";
cout << "Add: " << h->add(new Object(312))->valueOf() << "\n";
cout << "Add: " << h->add(new Object(835))->valueOf() << "\n";
h->display();
cout << "Pop: " << (h->pop("212"))->valueOf() << "\n";
h->display();
cout << "Pop: " << (h->pop("12"))->valueOf() << "\n";
h->display();
cout << "Lup: " << (h->lookup("835"))->valueOf() << "\n";
h->display();
}