Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #define N 100 //Número de valores a serem inseridos na hash
- using namespace std;
- //Insere um elemento na hash table.
- //Parâmetros: referência para um ponteiro da hash, chave a ser inserida, tamanho, função de hash
- int hashInsert(int ** T, int k, int m, int * h(int, int)){
- int i=0;
- do{
- int j = *h(k, i);
- if((*T)[j]==NULL){
- (*T)[j]=k;
- return j;
- }else{
- i++;
- }
- }while(!(i==m));
- }
- //Remove um elemento da hash table.
- //Parâmetros: referência para um ponteiro da hash, chave a ser inserida, tamanho, função de hash
- int hashSearch(int ** T, int k, int m, int * h(int, int)){
- int i=0;
- int j;
- do{
- j=*h(k, i);
- if((*T)[j]==k){
- return j;
- }
- i++;
- }while(!((*T)[j]==NULL || i==m));
- return NULL;
- }
- //Função de hash de módulo simples
- int moduloHash(int key, int size){
- return key%size;
- }
- //Main
- int main(int argc, char * argv[]){
- int i;
- if(argc>1){
- int size = atoi(argv[1]);
- int hash[size];
- for(i=0;i<N;i++){
- hashInsert(&hash, i, size, moduloHash);
- }
- }else{
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement