-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlista.c
85 lines (75 loc) · 1.71 KB
/
lista.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
#include <stdio.h>
#include <stdlib.h>
#include "lista.h"
#include "arvore.h"
struct celula {
Arv* arvore;
Celula* prox;
};
struct lista {
Celula* fim;
Celula* ini;
};
Lista* inic_lista() {
Lista* sentinela = (Lista*) malloc(sizeof (Lista));
sentinela->fim = NULL;
sentinela->ini = NULL;
return sentinela;
}
Celula* nova_celula(Arv* arv) {
Celula* cel = (Celula*) malloc(sizeof (Celula));
cel->arvore = arv;
cel->prox = NULL;
return cel;
}
void retira_2_prim(Lista* l){
Celula* p = l->ini;
Celula* q = l->ini->prox;
if(q->prox == NULL)
l->ini = NULL;
else
l->ini = q->prox;
free(p);
free(q);
}
void lista_insere(Lista* l, Celula* nova) {
if (l->ini == NULL) {
l->ini = nova;
l->fim = nova;
} else {
l->fim->prox = nova;
l->fim = nova;
}
}
void ordena_lista(Lista* l) {
Celula *p, *q;
for(p = l->ini;p!=NULL;p = p->prox){
for(q = p->prox;q!=NULL;q = q->prox){
if(arv_freq(q->arvore)<arv_freq(p->arvore))
troca_arv(p, q);
}
}
}
void troca_arv(Celula* p, Celula* q){
Arv* aux = p->arvore;
p->arvore = q->arvore;
q->arvore = aux;
}
int tam_lista(Lista* l){
int aux = 0;
Celula* p = l->ini;
while(p!=NULL){
aux++;
p = p->prox;
}
return aux;
}
void faz_arv_huffman(Lista* lista){
while (tam_lista(lista) != 1) {
Celula* p = lista->ini;
Arv* novo_ramo = cria_arv((arv_freq(p->arvore)+arv_freq(p->prox->arvore)), lista->ini->prox->arvore, lista->ini->arvore);
Celula* nova_cel = nova_celula(novo_ramo);
retira_2_prim(lista);
lista_insere(lista, nova_cel);
}
}