-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlista.c
140 lines (124 loc) · 2.25 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
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lista.h"
struct celula
{
void* info;
Celula* prox;
};
struct lista
{
struct celula *prim;
struct celula *ult;
};
Lista *cria_lista(){
Lista *n = (Lista *)malloc(sizeof(Lista));
n->prim = n->ult = NULL;
return n;
}
int vazia_lista(Lista* l){
if(l->prim == NULL){
return 1;
}
return 0;
}
//Insere na ordem crescente
void insere_crescente_lista(Lista *l,void* info,int (*cb) (void*)){
Celula* n = (Celula *)malloc(sizeof(Celula));
n->info = info;
n->prox = NULL;
Celula* aux = l->prim;
Celula* ant = NULL;
while(aux!=NULL && cb(info)>cb(aux->info)){
ant = aux;
aux = aux->prox;
}
if(l->prim == NULL){//Insere no início(Lista ainda não possuia elementos)
l->prim = l->ult = n;
return;
}
if(aux == NULL){//Insere na última posição
ant->prox = n;
l->ult = n;
return;
}
if(l->prim == aux){//Insere no início
n->prox = l->prim;
l->prim = n;
}else{//No meio da Lista
ant->prox = n;
n->prox = aux;
}
}
void insere_lista(Lista* l,void* info){
Celula* n = (Celula *)malloc(sizeof(Celula));
n->info = info;
n->prox = NULL;
if(vazia_lista(l)){
l->prim = l->ult = n;
return;
}
l->ult->prox = n;
l->ult = n;
}
//Retira sempre a primeira
void* retira_lista(Lista* l){
if(vazia_lista(l)){
return NULL;
}
Celula* aux = l->prim;
l->prim = l->prim->prox;
void* aux2 = aux->info;
free(aux);
return aux2;
}
void libera_lista(Lista* l,void (*cb) (void*)){
Celula* ant = NULL;
Celula* aux = l->prim;
while (aux != NULL) {
ant = aux;
aux = aux->prox;
cb(ant->info);
free (ant);
}
free (l);
}
void* retorna_info(Celula* aux){
if(aux!=NULL){
return aux->info;
}
return NULL;
}
Celula* retorna_primeiro(Lista* l){
if(l!=NULL){
return l->prim;
}
return NULL;
}
Celula* retorna_ultimo(Lista* l){
if(l!=NULL){
return l->ult;
}
return NULL;
}
Celula* retorna_proximo(Celula* aux){
if(aux!=NULL){
return aux->prox;
}
return NULL;
}
void preenche_vetor(Lista* l,void (*cb) (void*, char**), char** vetor){
Celula* aux = l->prim;
while (aux!=NULL){
cb(aux->info,vetor);
aux = aux->prox;
}
}
void imprime_lista(Lista* l,void (*cb) (void*)){
Celula* aux = l->prim;
while (aux!=NULL){
cb(aux->info);
aux = aux->prox;
}
}