-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistaligada.c
82 lines (73 loc) · 2.09 KB
/
listaligada.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
#include "listaligada.h"
void sortedInsert(struct listaLigada** head_ref, struct listaLigada* new_node)
{
struct listaLigada* atual;
if (*head_ref == NULL || (comparaDatas((*head_ref)->datay, new_node->datay) == 1))
{
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
/* Encontrar o nodo antes de inserir */
atual = *head_ref;
while (atual->next != NULL &&
(comparaDatas(atual->next->datay, new_node->datay) != 1))
{
atual = atual->next;
}
new_node->next = atual->next;
atual->next = new_node;
}
}
/*Função que cria um novo nodo de listaligada */
struct listaLigada *novoNodo(struct data d, char * n, char * q, char * f){
struct listaLigada * new = (struct listaLigada *) malloc(sizeof(struct listaLigada));
new->datay = d;
new->nome = n;
new->quem = q;
new->fato = f;
new->next = NULL;
return new;
}
/* Função que compara duas datas, retorna 1 se a data1 for mais recente **/
int comparaDatas(struct data data1, struct data data2){
if(data1.ano > data2.ano)return 1;
else if(data1.ano < data2.ano) return -1;
else {
if(data1.mes > data2.mes)return 1;
else if(data1.mes < data2.mes)return -1;
else {
if(data1.dia > data2.dia)return 1;
else if(data1.dia <= data2.dia) return -1;
}
}
return -1;
}
/* Função para utilização do qsort */
int compare (const void * a, const void * b)
{
return strcmp (*(const char **) a, *(const char **) b);
}
/* Função que retira os nomes da lista ligada, ordena e guarda num array de strings */
char ** getNames(struct listaLigada* l, int k){
int maior = 0;
int i,h;
struct listaLigada * n = l;
while(n != NULL){
if(strlen(n->quem) > maior) maior = strlen(n->quem);
n = n->next;
}
char ** new;
new = malloc(k * maior * sizeof(char*));
for(i = 0; i < k; i++) {
new[i] = malloc(maior* sizeof(l->quem));
strcpy(new[i],l->quem);
if(new[i][0] == ' ') {
for(h = 0; h < strlen(new[i]); h++) new[i][h] = new[i][h+1];
}
l = l->next;
}
qsort(new, k, sizeof(const char *), compare);
return new;
}