-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash Table - Addressing Closed.cpp
More file actions
151 lines (133 loc) · 2.55 KB
/
Copy pathHash Table - Addressing Closed.cpp
File metadata and controls
151 lines (133 loc) · 2.55 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
148
149
150
151
#include<stdio.h>
#include<conio.h>
#include <stdlib.h>
#define N 11
//LISTA DINAMICA
struct no{
int item;
struct no *prox;
};
int chave, n=0, i, r;
struct no *hash[N];
struct no *aloca_no(){
struct no *h;
h= (struct no *)malloc(sizeof(struct no));
return h;
}
void desaloca_no(struct no *h){
free (h);
h=NULL;
}
//FIM
//HASH
int pesquisa(){
struct no *aux;
int pos;
pos=(2*chave+5)%N;
aux = hash[pos];
while(aux!=NULL){
if((aux->item)==chave){
printf("Com sucesso!");
return 1;
}
else{
aux = aux->prox;
}
}
printf("Sem Sucesso");
}
void insercao(){
struct no *aux, *p;
int pos;
pos=(2*chave +5)%N;
aux = hash[pos];
p=aloca_no();
p->item=chave;
p->prox=aux;
hash[pos]=p;
n++;
}
void exibicao(){
struct no *aux;
for(i=0; i<N; i++){
printf("%d: ", i);
aux = hash[i];
while(aux!=NULL){
printf("%d , ",aux->item);
aux=aux->prox;
}
printf("\n\n");
}
}
int remocao(){
int pos;
struct no *aux;
struct no *ant;
pos=(2*chave+5)%N;
ant=NULL;
aux=hash[pos];
while(aux!=NULL){
if(aux->item == chave){
if(ant != NULL){
ant->prox=aux->prox;
}else{
hash[pos]=aux->prox;
}
n--;
desaloca_no(aux);
aux=NULL;
return 1;
}else{
ant=aux;
aux=aux->prox;
return 0;
}
}
}
//main
main(){
int op;
for(i=0; i<N; i++){
hash[i]=NULL;
}
do{
system("cls");
printf("******MENU********* \n1- Insercão \n2- Remocao \n3- Pesquisa \n4- Exibicao \n5- Sair\n");
printf("\nDigite a opcao: ");
scanf("%d", &op);
switch(op){
case 1:
printf("Digite o numero a ser inserido: ");
scanf("%d", &chave);
insercao();
printf("FATOR DE CARGA: %d / %d ", n, N);
getch();
break;
case 2:
printf("Digite o numero a ser Removido: ");
scanf("%d", &chave);
r=remocao();
printf("FATOR DE CARGA: %d / %d ", n, N);
if(r==1){
printf("\nSucesso");
}else{
printf("\nSem sucesso!");
}
getch();
break;
case 3:
printf("Digite o valor a ser buscado: ");
scanf("%d", &chave);
pesquisa();
getch();
break;
case 4:
exibicao();
getch();
break;
default:
printf("Opcao invalida!");
getch();
}
}while(op!=5);
}