Dev - C++ - Implementar Funciones Tabla de Hash

 
Vista:

Implementar Funciones Tabla de Hash

Publicado por Alejandrov (1 intervención) el 24/11/2021 20:49:35
Hola a todos un saludo lo que sucede es que estoy aprendiendo estructura de datos, tengo un inconveniente me pidieron diseñar una tabla de Hash la cual diseñe y me pidieron implementar funciones como buscar items en la tabla,modificarlos,agregarlos y borrarlos, ya pudo dimplementar la funcion de busqueda, pero no entiendo como implementar las siguientes funciones, me podrian ayudar porfavoraca esta mi codigo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<bits/stdc++.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<string>
#include<ctype.h>
using namespace std;
int n1=81; //The size of the hash table
int choose_key() {
int v, k;
    printf("Do you want enter new element-1 or to generate it - 2?\n");
cin>>v;
if (v == 1){
    printf("enter your value: ");
    cin>>k;
}
else{
    srand(time(NULL));
    k = rand() % (999 - 100 + 1) + 100;
    printf("generated element: %d\n", k);
}
return k;
 
}
//Funcion Buscar
int find (int* hash){
int k = choose_key();
int flag = -1;
int ind = (k * k) % n1;
if (hash[ind] == 0) {}
else {
int i = 1;
while (hash[ind] != k && i<100) {
ind = (ind + i * i) % n1;
i++;
}
if (hash[ind] == k)
flag = ind;
}
return flag;
}
 
void find2(int* hash) {
int flag = find(hash);
if (flag == -1)
cout<<"The element was not found!\n";
else cout<<"The index of the found element = "<<flag<<endl;
}
 
 
 
 
 
 
 
 
int main(){
 
    int op, res,p;
    int n=47; //number of elements generated
 
    long int mas[47]; //Array of generated elements
    int hash[100]={0}; //hash table
    int adress; //Hash address
    int i;
    int dato;
    char band ='V';
    dato = 200;
    int i0=1; //Sample number
    int k1=0; //Table fill factor = n / n1
    int k2=0; //Total number of samples
int flag=0;
int tmp=0;
int k=0;
system("cls");
srand(time(NULL));
while (k<n) {
 
 
flag=0;
tmp=(100*(1+rand()%9))+(rand()%100);
for (int i=0; i<k; i++) {
if (mas[i]==tmp) {
flag=1;
break;
}
}
if (flag==0) {
mas[k]=tmp;
++k;
}
}
 
cout<<"GENERATION OF RANDOM NUMBERS: "<<endl;
for (int i=1; i<n; i++)
cout<<setw(2)<<i<<setw(7)<<mas[i]<<" ";
cout<<endl;
cout<<endl;
 
cout<<"TABLA DE HASH : "<<endl;
for (int i=0; i<n; i++) {
adress=(mas[i]*mas[i])%n1; //funcion no olvidar conchetumadre
k2++;
int adress0=adress;
if (hash[adress]==0) {
hash[adress]=mas[i];
k1++;
}
else {
while (hash[adress]!=0) {
adress=(adress0+i0*i0)%n1;
i0++;
k2++;
}
hash[adress]=mas[i];
}
i0=1;
}
for (int ht=0; ht<n1; ht++)
 
cout<<setw(2)<<ht<<setw(7)<<hash[ht]<<" ";
 
 
 
cout<<"\n\nTable fill : "<<double(n)/double(n1)<<endl;
 
cout<<"Number of samples :"<<double(k2)/double(n)<<endl;
cout<<endl;
 
 
int a = choose_key ();
cout<<a;
 
 
find2(hash);
 
return 0;
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder