الثلاثاء، 1 أبريل 2014
2:01 م

hashtable in c++

// record taqble data base.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#define null -1
using namespace std;
struct emprecord
{
int empcode;
};
class hashtable
{
private:
emprecord *hasharray;
int arraysize;
public:
hashtable(int size)
{
hasharray = new emprecord[size];
arraysize = size;
for (int i = 0; i < arraysize; i++)
hasharray[i].empcode = null;
}
int hashfunc(int key)
{
return key%arraysize;
}
void insert(emprecord item)
{
int key = item.empcode;
int hasval = hashfunc(key);
while (hasharray[hasval].empcode != null)
{
++hasval;
hasval %= arraysize;
}
hasharray[hasval] = item;
}
void display()
{
cout << "key" << "\t" << "hashfunc" << endl;
cout << "---" << "\t" << "--------" << endl;
for (int i = 0; i < arraysize; i++)
cout << hasharray[i].empcode << "\t" << hashfunc(hasharray[i].empcode) << endl;
}

};

void main()
{
int a[] = { 12, 17, 21, 28, 35, 40, 43, 52, 66, 69 };
emprecord reckey[10];
for (int i = 0; i < 10; i++)
reckey[i].empcode=a[i];
hashtable h(10);
for (int i = 0; i < 10; i++)
h.insert(reckey[i]);
h.display();
_getch();

}

0 التعليقات:

إرسال تعليق