-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllist.c
More file actions
175 lines (175 loc) · 4.62 KB
/
llist.c
File metadata and controls
175 lines (175 loc) · 4.62 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Exercício de lista ligada do livro Low Level programming
* Niet
* 0.1v
*/
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
#include "llist.h"
// Estrutura padrão do node.
struct No
{ // Node
int number;
struct No* next; // Node seguinte
};
struct No* list_create(const int n){
struct No* newl;
newl = malloc( sizeof(struct No) );
newl->number = n;
newl->next = NULL;
return newl;
}
// Adiciona um elemento em primeiro lugar
void list_add_front(const int n, struct No** list){
struct No* newl;
newl = list_create(n);
newl->next = *list;
*list = newl;
}
// adiciona um elemento em ultimo lugar
void list_add_back(const int n, struct No* list){
struct No* newl;
size_t lsize;
struct No* last_l;
newl = list_create(n);
lsize = list_length(list);
last_l = list_node_at(lsize-1, list); // retorna o ultimo elemento
last_l->next = newl;
}
// Retorna um size_t com o tamanho total de elementos da lista passado
size_t list_length(struct No* list) {
size_t lsize;
struct No *node;
for(node=list, lsize=0; node != NULL; node=node->next, lsize++);
return lsize;
}
//Soma todos os elementos da lista passada
int list_sum(struct No* list){
struct No *node;
int sum = 0;
for(node=list; node != NULL ; node=node->next)
sum += node->number;
return sum;
}
// retorna o node pelo seu indice na lista
struct No* list_node_at(const unsigned int index, struct No* list){
size_t length, lsize;
struct No* node;
length = list_length(list);
if(index >= length)
return NULL;
for(node=list, lsize=0; lsize<index; node=node->next, lsize++);
return node;
}
// retorna o elemento(node.number) pelo seu indice
int list_get(const unsigned int index, struct No* list){
struct No* node;
int r_number;
node = list_node_at(index, list);
if(node == NULL)
return 0;
r_number = node->number;
return r_number;
}
void list_free(struct No* list){
struct No* node;
struct No* n_node;
for(node=list; node!=NULL; node=n_node){
//printf("free: %p - %d ", (void *) node, node->number);
n_node = node->next;
free(node);
}
}
//others
// Executa a função *func em cada elemento da lista
void foreach(struct No* list, void (*func)(int)) {
struct No* node;
for(node=list; node != NULL ; node=node->next)
func(node->number);
}
// Retorna uma nova *lista com o resultado de *func aplicado aos elementos
// da lista original
struct No* map(struct No* list, int (*func) (int) ) {
struct No* cpnode = list_create(0);
struct No* node;
struct No* renode;
for(node=list; node != NULL ; node=node->next)
list_add_back(func(node->number), cpnode);
renode = cpnode->next;
free(cpnode);
return renode;
}
// Altera os elementos da lista original pelo resultado da função *func aplicado
// neles mesmos.
void map_mut(struct No* list, int (*func)(int)){
struct No* node;
for(node=list; node != NULL ; node=node->next)
node->number = func(node->number);
}
// f(x,a) = x * a
int foldl(struct No* list, int a, int (*func)(int, int)){
struct No* node;
for(node=list; node != NULL; node=node->next)
a = func(a, node->number);
return a;
}
struct No* iterate(int s, size_t n, int (*func) (int) ) {
struct No* nwl = list_create(s);
size_t count;
for(count=0; count<n; count++){
s = func(s);
list_add_back(s, nwl);
}
return nwl;
}
// salva os elementos de uma lista em modo texto(txt)
bool save(struct No* list, const char* filename){
struct No* node;
FILE *fd;
fd = fopen(filename, "w");
if( fd == NULL ) return false;
for(node=list; node != NULL ; node=node->next)
fprintf(fd, "%d ", node->number);
fclose(fd);
return true;
}
// carrega os elementos de uma lista de um txt
bool load(struct No** list, const char* filename){
FILE *fd;
int element;
fd = fopen(filename, "r");
if( fd == NULL ) return false;
while(1){
fscanf(fd, "%i", &element);
if( feof(fd) ) break;
list_add_back(element, *list);
}
return true;
}
// salva os elementos de uma lista em modo binario
bool serialize(struct No* list, const char* filename){
struct No* node;
FILE *fd;
fd = fopen(filename, "wb");
if( fd == NULL ) return false;
for(node=list; node != NULL ; node=node->next){
fwrite(&node->number, sizeof(int), 1, fd);
fwrite(" ", sizeof(char), 1, fd);
}
fclose(fd);
return true;
}
// carrega elementos da lsita de um arquivo binario
bool deserialize(struct No** list, const char* filename){
FILE *fd;
int element;
fd = fopen(filename, "rb");
if( fd == NULL ) return false;
while(1){
fread(&element, sizeof(int), 1, fd);
getc(fd); // spaces
if( feof(fd) ) break;
list_add_back(element, *list);
}
return true;
}