-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash Table - Addressing Open (DOUBLE).c
More file actions
147 lines (128 loc) · 3.12 KB
/
Hash Table - Addressing Open (DOUBLE).c
File metadata and controls
147 lines (128 loc) · 3.12 KB
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
140
141
142
143
144
145
146
147
#include<stdio.h>
#include<stdlib.h>
#define N 11
int hash[N];
int insercao(int x){
int pos, i=1, h;
pos = (2*x+5)% N;
if(hash[pos] == -1 || hash[pos] == -2){
hash[pos] = x;
return(1);
}else{
while(hash[pos] != -1 && hash[pos] != -2){
if(i== 1){
h = 7 - (x % 7);
}
pos = ((2*x+5)% N + (i * h)) % N;
if(hash[pos] == -1 || hash[pos] == -2){
hash[pos] = x;
return (1);
}
i++;
}
}
return(0);
}
int remover(int x){
int pos, i = 1, h;
pos = (2*x+5)% N;
if(hash[pos] == x){
hash[pos] = -2;
return(1);
}else{
while(hash[pos] != -1 && hash[pos] !=-2){
if(i== 1)
h = 7 - (x % 7);
pos = ((2*x+5)% N + (i * h)) % N;
if(hash[pos] == x){
hash[pos] = -2;
return(1);
}
i++;
}
}
return (0);
}
int busca(int x){
int pos, i = 1, h;
pos = (2*x+5)% N;
if(hash[pos] == x){
return (pos);
}else{
while(hash[pos] != -1 && hash[pos] !=-2){
if(i== 1)
h = 7 - (x % 7);
pos = ((2*x+5)% N + (i * h)) % N;
if(hash[pos] == x){
return(pos);
}
i++;
}
}
return(-1);
}
exibir(){
int i;
for(i =0; i<N; i++){
printf("hash[%d]: %d\n", i, hash[i]);
}
}
int main(){
int i, opc, chave, n=0, j;
float fchave, x, y;
for(i=0; i<N; i++){
hash[i] = -1;
}
do{
printf("\n\n1- Inserir\n2- Remover\n3- Buscar\n4- Exibir\n5- Sair\n");
printf("\nOpcao: ");
scanf("%d", &opc);
switch(opc){
case 1:
printf("Numero: ");
scanf("%d", &chave);
if(n!=N){
j = insercao(chave);
if(j==1){
n++;
printf("Inserido\n");
x = n; y = N; fchave = x/y;
printf("\nFator Chave: %.2f", fchave);
}
else
printf("\nNao Inserido");
}else
printf("\nTabela cheia\n");
break;
case 2:
printf("Numero: ");
scanf("%d", &chave);
j = remover(chave);
if(j == 1){
printf("Removeu\n");
n--;
x = n; y = N; fchave = x/y;
printf("\nFator Chave: %.2f", fchave);
}else printf("\nNao Encontrado");
break;
case 3:
printf("Numero: ");
scanf("%d", &chave);
j = busca(chave);
if(j != -1){
printf("\nAchou na Posicao: %d", j);
}else{
printf("\nNao Encontrado");
}
break;
case 4:
exibir();
break;
case 5:
printf ("\nSaindo...\n");
break;
default: printf ("\nOpcao invalida\n");
}
}while (opc!=5);
return 0;
}