-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashing-lista-encadeada.c
169 lines (142 loc) · 3.9 KB
/
hashing-lista-encadeada.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Implemente um TAD para hashing estático aberto, onde as colisões devem ser resolvidas por meio de listas encadeadas ordenadas.
A função hash deve ser resto de divisão, ou seja, h(k) = k % B, onde k é uma chave e B é o tamanho da tabela hash.
Input Format
Na primeira linha deve ser lido o tamanho da tabela hash. Na segunda linha deve ser lida a quantidade de chaves que serão alocadas.
Por fim, devem ser lidas as chaves que serão alocadas na tabela hash.
Constraints
Output Format
Imprimir as posições onde as chaves foram colocadas na tabela. Em seguida, imprimir a tabela hash inteira.
Para isso, deve ser considerado o seguinte formato: "linha_i: -> item_0 -> item_1 -> ... -> item_n"
Caso a i-ésima linha da matriz esparsa seja nula, basta imprimir "linha_i -> ".
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef struct Cell Cell;
typedef struct ListaE ListaE;
struct Cell {
int item;
Cell *next;
};
struct ListaE {
Cell *head;
};
Cell* criar_celula(int key) {
Cell *c = (Cell*) malloc(sizeof(Cell));
c->item = key;
c->next = NULL;
return c;
}
ListaE* criar_listaE() {
ListaE* l = (ListaE*) malloc(sizeof(ListaE));
l->head = NULL;
return l;
}
int listaE_vazia(ListaE *l) {
return (l == NULL) || (l->head == NULL);
}
void inserir_primeiro(int key, ListaE *l) {
Cell *aux;
if (l == NULL)
l = criar_listaE();
aux = criar_celula(key);
aux->next = l->head;
l->head = aux;
}
void imprimir_lista(ListaE *l) {
Cell *aux;
if (!listaE_vazia(l)) {
aux = l->head;
while (aux != NULL) {
printf(" -> %d", aux->item);
aux = aux->next;
}
printf("\n");
} else {
printf(" ->\n");
}
}
void inserir_ordenado(int key, ListaE *l) {
Cell *auxA, *auxP, *nova;
if (l == NULL)
l = criar_listaE();
if (listaE_vazia(l))
inserir_primeiro(key, l);
else {
nova = criar_celula(key);
if ((l->head == NULL) || (l->head->item <= key)) {
nova->next = l->head;
l->head = nova;
} else {
auxP = auxA = l->head;
while((auxA != NULL) && (key > auxA->item)) {
auxP = auxA;
auxA = auxA->next;
}
auxP->next = nova;
nova->next = auxA;
}
}
}
typedef struct {
unsigned int tam;
ListaE **buckets;
int *posicao;
int numPos;
} HashT;
HashT *criar(unsigned int tam) {
HashT *t = malloc(sizeof(HashT));
int i;
t->tam = tam;
t->buckets = (ListaE**) malloc(tam * sizeof(ListaE*));
t->posicao = (int*) malloc(sizeof(int));
t->numPos = 0;
for (i = 0; i < tam; i++)
t->buckets[i] = criar_listaE();
return t;
}
static int hashingF(int k, int B) {
return k % B;
}
int inserir(int key, HashT *t) {
int x;
if ((t != NULL) && (key > 0)) {
x = hashingF(key, t->tam);
inserir_ordenado(key, t->buckets[x]);
t->posicao = realloc(t->posicao, (t->numPos + 1) * sizeof(int));
t->posicao[t->numPos] = x;
t->numPos++;
return 1;
}
return 0;
}
void imprimir(HashT *t) {
int i;
if (t != NULL)
for (i = 0; i < t->tam; i++) {
printf("%d:", i);
imprimir_lista(t->buckets[i]);
}
}
void imprimir_posicao_inserida(HashT *t) {
int i;
if(t != NULL)
for (i = 0; i < t->numPos; i++)
printf("%d\n", t->posicao[i]);
}
int main(void) {
int n, end, key, i;
scanf("%d", &n);
scanf("%d", &end);
HashT *t = criar(n);
for(i = 0; i < end; i++) {
scanf("%d", &key);
inserir(key, t);
}
imprimir_posicao_inserida(t);
printf("\n");
imprimir(t);
return 0;
}