Saturday, April 24, 2010

HashTable Implementation

// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include
//#include
using namespace std;

#define H_SIZE 79

typedef struct _Element Element;
typedef struct _HashTable HashTable;
typedef enum _COLLISION_RESOLUTION_TYPE CRT;

void collisionResolution(HashTable* origin, int key, Element* pElement, CRT resType);
bool compareNode(Element* node, Element* pData);

enum _COLLISION_RESOLUTION_TYPE {
INVALID_TYPE,
OPEN_HASHING,
CLOSED_HASHING_LINEAR_PROBING,
CLOSED_HASHING_QUADRATIC_PROBING,
CLOSED_HASHING_DOUBLE_HASHING,
REHASHING,
EXTENDIBLE_HASHING
};


struct _Element
{
int dInt;
char pChar[80];
int nonce;
Element* next;
};

struct _HashTable
{
Element* element;
};

class C_HashTable
{
private:
HashTable origin[H_SIZE];
public:
C_HashTable() {int i=0;while( i < H_SIZE ) {origin[i].element = NULL;i++;}}
void insert(Element*);
int hash_function(Element*);
void my_delete(Element*);
void display() {
int i=0;
Element* node;
while( i < H_SIZE ) {
node = origin[i].element;
if (node)
{
cout << "origin[" << i << "]" << " = " << node->dInt << \
" | " << node->pChar << " | " << node->nonce << " --> " ;
node = node->next;
while(node) {
cout << node->dInt << " | " << node->pChar << " | " << node->nonce << " --> " ;
node = node->next;
}
cout << "NULL\n";
}
i++;
}
}
};


int C_HashTable::hash_function(Element* pElement)
{
int key = -1;
if (!pElement) return 0;
key = pElement->dInt;
if (strlen(pElement->pChar) >= 3)
key += pElement->pChar[0] + (27*pElement->pChar[1]) + (729*(pElement->pChar[2]));
key += pElement->nonce;
cout << "Key --> " << key << "\n";
return (key % H_SIZE);
}

void C_HashTable::insert(Element* pElement)
{
int key;
if(!pElement) return;
key = hash_function(pElement);
if (!origin[key].element) {
origin[key].element = (Element*)malloc(sizeof(Element));
origin[key].element->dInt = pElement->dInt;
origin[key].element->nonce = pElement->nonce;
strcpy(origin[key].element->pChar, pElement->pChar);
origin[key].element->next = NULL;
cout << "Inserted --> " << pElement->dInt << " At Position(Key) = " << key <<"\n";
}
else
{
int i;
cout << "COLLISION...COLLISION...COLLISION\n";
cout << "1: Open Hashing\n2: Close Hashing Linear Probing\n";
cin >> i;
switch (i) {
case OPEN_HASHING:
collisionResolution(origin, key, pElement, OPEN_HASHING);
break;
case CLOSED_HASHING_LINEAR_PROBING:
collisionResolution(origin, key, pElement, CLOSED_HASHING_LINEAR_PROBING);
break;

default:
cout << "Not Handled\n";
}
}

}

void collisionResolution(HashTable* origin, int key, Element* pElement, CRT resType)
{
Element *node = NULL;
int itr = -1;
if (!origin || (key < 0) || !pElement) return;
switch (resType) {
case OPEN_HASHING:
node = origin[key].element;
while (node->next) node = node->next;
node->next = (Element*)malloc(sizeof(Element));
node = node->next;
node->dInt = pElement->dInt;
node->nonce = pElement->nonce;
strcpy(node->pChar,pElement->pChar);
node->next = NULL;
cout << "Inserted --> " << pElement->dInt << " At Position(Key) = " << key <<"\n";
break;
case CLOSED_HASHING_LINEAR_PROBING:
itr = key+1;
if (itr > H_SIZE) itr=0;
while (origin[itr].element)
{
itr++;
if (itr > H_SIZE) itr = 0;
if (itr == key) { cout << "ALL CELLS EXHAUSTED\n"; return; }
}
node = origin[itr].element = (Element*)malloc(sizeof(Element));
node->dInt = pElement->dInt;
node->nonce = pElement->nonce;
strcpy(node->pChar,pElement->pChar);
node->next = NULL;
cout << "Inserted --> " << pElement->dInt << " At Position(Key) = " << itr <<"\n";
break;
default:
cout << "NOT IMPLEMENTED\n";
}
}


void C_HashTable::my_delete(Element* pData)
{
int key, itr;
Element *node = NULL;
Element *nextNode = NULL;
if(!pData) return;
itr = key = hash_function(pData);
while (1)
{
node = origin[itr].element;
if (!node) { cout << "No Match Found\n"; return; }
if (compareNode(node,pData) == true)
{
while (node) {
nextNode = node->next;
free(node);
node = nextNode;
}
break;
}
else
{
itr++;
if (itr > H_SIZE) itr = 0;
if (itr == key) break;
}
}
if(origin[key].element)
{
cout << "Deleting --> " << pData->dInt << " At Position(Key) = " << itr <<"\n";
origin[itr].element = NULL;
} else cout << "Nothing to Delete\n";

}

bool compareNode(Element* node, Element* pData)
{
if ((node->dInt == pData->dInt) && (node->nonce == pData->nonce) && (!strcmp(node->pChar,pData->pChar)))
return true;
else
return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
C_HashTable HT;
Element e;
cout << "Enter the dInt, pChar, nonce values " << "\n";
cin >> e.dInt;
while (e.dInt != -1)
{
cin >> e.pChar;
cin >> e.nonce;
HT.insert(&e);
cout << "Enter the dInt, pChar, nonce values " << "\n";
cin >> e.dInt;
}
HT.display();
cout << "\n\nDELETE -> Enter the dInt, pChar, nonce values " << "\n";
cin >> e.dInt;
while (e.dInt != -1)
{
cin >> e.pChar;
cin >> e.nonce;
HT.my_delete(&e);
cout << "Enter the dInt, pChar, nonce values " << "\n";
cin >> e.dInt;
}
cout << "\n\n\n";
HT.display();


int x;
cin >> x;


}

No comments:

Post a Comment