-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlist.c
99 lines (83 loc) · 1.68 KB
/
list.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
#include <stdlib.h>
#include <string.h>
struct node {
void *data;
struct node *next;
};
#include "list.h"
list init_list() {
return NULL;
}
int createNode(pos *p) {
*p = malloc(sizeof(**p));
return *p != NULL;
}
void insert(list *l, void *data){
pos item, aux;
if(createNode(&item)){
item->data = data;
item->next = NULL;
if(*l == NULL){
*l = item;
}else{
for (aux = *l; aux->next != NULL; aux = aux->next); //nos movemos al final de la lista
aux->next = item;
}
}
}
pos first(list l) {
return l;
}
pos next(list l, pos p) {
if(p==NULL) return NULL;
return p->next;
}
int end(list l, pos p) { // estoy en el final
return (p==NULL);
}
void *get(list l, pos p) {
if(p==NULL) return NULL;
return p->data;
}
int numPos(list l){ //numero total de posiciones
int i;
pos p=l;
for(i=0; next(l,p)!= NULL; i++ )
p=next(l,p);
return i;
}
pos findItem(list L, void *d){
pos p;
for(p=L;((p!=NULL)&&(p->data != d));p=p->next);
if(p!=NULL&&(p->data == d))
return p;
else
return NULL;
}
void deleteAtPosition(list* list, pos p, void (*free_data)(void *)) {
pos i;
if(p == *list) {
*list = (*list)->next;
}else if(p->next == NULL) {
for (i = *list; i->next != p; i = i->next);
i->next = NULL;
}else {
i = p->next;
p->data = i->data;
p->next = i->next;
p = i;
}
free_data(p->data);
free(p);
}
void freeList(list *l, void (*free_data)(void *)) {
struct node *n, *aux;
n = *l;
while(n != NULL) {
aux = n;
free_data(n->data);
n = n -> next;
free(aux);
}
*l = NULL;
}