/
lista.c
107 lines (89 loc) · 1.73 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
#include <stdio.h>
#include <stdlib.h>
#include "lista.h"
/**
* Inicia uma lista
* @param l elemento do tipo t_lista
*/
void initList(t_lista *l)
{
l->prim = NULL;
l->ult = NULL;
}
/**
* Verifica se a lista está vazia
* @param l elemento do tipo t_lista
* @return bool
*/
int isEmptyList(t_lista *l)
{
return l->prim == NULL;
}
/**
* Insere elemento na lista
* @param l elemento do tipo lista
*/
void insertList(t_lista *l)
{
t_apont_no novo, aux;
novo = createNo(1);
aux = l->prim;
if (! novo) {
printf("\nErro ao alocar.\n");
return;
}
if (isEmptyList(l)) {
l->prim = novo;
l->ult = novo;
return;
}
while (aux->prox) aux = aux->prox;
aux->prox = novo;
l->ult = novo;
}
/**
* Remove item da lista
* @param l elemento do tipo t_lista
*/
void removeList(t_lista *l)
{
t_apont_no aux;
if (! isEmptyList(l)) {
aux = l->prim;
l->prim = aux->prox;
free(aux);
}
}
/**
* Conta quantos parametros há na lista
* @param l elemento do tipo t_lista
* @return int números de elementos
*/
int countLista(t_lista *l)
{
t_apont_no aux;
int cont = 0;
aux = l->prim;
while (aux){
cont = cont + aux->info;
aux = aux->prox;
}
return cont;
}
/**
* Cria números aleatóriamente
* @param l elemento do tipo t_lista
* @param n número de interações
*/
void createTest(t_lista *l, int n)
{
int i, v[n];
for (i = 0; i <= n; i++) v[i] = rand() % 2;
for (i = 0; i < n; i++) printf("%d", v[i]);
printf("\n");
for (i = 0; i < n; i++) if (v[i] == 1) {
insertList(l);
} else {
if (! isEmptyList(l)) removeList(l);
}
}