#include "hashTable.h"
void HashTable::getData(int *T, int m){
cout<<"Input:[";
input=new int[N];
for(int i=0;i<N;i++){
input[i]=rand()%(2*m);
hashInsert(T,input[i],m);
cout<<input[i];
cout<<",";
}
cout<<"]";
}
void HashTable::hashInsert(int *T,int x,int m){
int i=0;
do{
int hx=((x % m)+i)%m;
if(T[j]==-1)
{T[j]=x;break;}
else i++;
}while(i!=m);
}
int HashTable::hashSearch(int *T,int fnd, int x){
int i=0;
do{
int hx=((x % 13)+i)%13;
int j=hx;
if(T[i]==fnd)
{
cout<<"found";
return j;
}
else
i++;
}while(i<13);
cout<<"not found";
return NULL;
}
void HashTable::print(int *T){
cout<<"+---------+"<<endl;
for(int i=0;i<13;i++){
cout<<i<<" ";
cout<<T[i]<<" "<<endl;
}
}
-------------------.cpp
#include <iostream>
#include "hashTable.h"
using namespace std;
int main(){
HashTable h;
int *T;
int fnd;
int m=13;
//해쉬만들기
T=new int [m];
for(int i=0;i<m;i++){
T[i]=-1;
}
///
//해쉬테이블값입력
h.getData(T,m);
//테이블프린트
h.print(T);
//검색
cin>>fnd;
h.hashSearch(T,fnd,m);
system("PAUSE");
return 0;
}
-------------main.cpp
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <iostream>
using namespace std;
#define N 10
class HashTable{
private:
int *input;
public:
void getData(int *T, int m);
int hashInsert(int *T, int x, int m);
int hashSearch(int *T, int x, int m);
void print(int *T);
};
#endif
----------.h
'Not Using > algorithm' 카테고리의 다른 글
DP (0) | 2009.12.01 |
---|---|
Dynamic programming (0) | 2009.11.17 |
해쉬테이블 완성 (0) | 2009.11.09 |
이런망할이진검색트리 (0) | 2009.10.27 |
알고리즘 --- (0) | 2009.10.13 |