-
Notifications
You must be signed in to change notification settings - Fork 2
/
ListaEncadeadaOrdenada.c
63 lines (53 loc) 路 1.1 KB
/
ListaEncadeadaOrdenada.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int v;
struct node *p;
} node;
node *criarNo(int v) {
node *novo = (node *) malloc(sizeof(node));
if (novo) {
novo->v = v;
novo->p = 0;
}
return novo;
}
void inserir(node **lista, int v) {
node *novo = criarNo(v);
if (!novo) return;
if (v < (*lista)->v) {
novo->p = *lista;
*lista = novo;
return;
}
node *aux = *lista;
while (aux->p && aux->p->v < v)
aux = aux->p;
novo->p = aux->p;
aux->p = novo;
}
void exibir(node *lista) {
if (lista) {
printf("%d ", lista->v);
exibir(lista->p);
}
else printf("\n");
}
void liberar(node *lista) {
if (lista) {
liberar(lista->p);
free(lista);
}
}
int main(void) {
node *lista = criarNo(13);
if (!lista) return 1;
int valores[] = {27, 18, 14, 28, 27, 0, 30, 36, 19, 1, 19, 27, 22, 11,
30, 22, 17, 11, 14, 45, 39, 3, 50, 44, 25, 49, 31, 24, 30};
int tamanho = sizeof(valores) / sizeof(int);
for (int i = 0; i < tamanho; ++i)
inserir(&lista, valores[i]);
exibir(lista);
liberar(lista);
return 0;
}