-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
84 lines (73 loc) · 1.21 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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
list newlist(int x, list r) {
list l = malloc(sizeof(struct intlist));
l -> value = x;
l -> rest= r;
return l;
}
int head(list l){
return (l->value);
}
list tail(list l){
return (l->rest);
}
void printlist (list p){
list l = p;
printf("%d",l->value);
l = tail(l);
while(l!=NULL){
printf("%d",l->value);
l = tail(l);
}
printf("\n");
}
int elem(int pos, list l){
list p= l;
for(int i=1;i<pos;i++)
p = tail(l);
return head(p);
}
int length(list l) {
list p = l;
int i=0;
while(p!=NULL){
i++;
p = tail(p);
}
return i;
}
list append(list l1, list l2){
list p1=l1;
list p2=l2;
if(p1==NULL)
return p2;
while(l1->rest!=NULL){
l1 = tail(l1);
}
l1->rest = p2;
return p1;
}
list add1(int x,list l,int pos){
if(pos>length(l)){
printf("Erro: Posição excede o tamanho da lista\n");
exit(1);
}
list p = l;
for(int i=1;i<pos-1;i++){
l= tail(l);
}
list aux = newlist(x,tail(l));
l->rest = aux;
return p;
}
list reverse(list l){
list nova = NULL;
list old = l;
while(old!=NULL){
nova = append(newlist(head(old),NULL),nova);
old = tail(old);
}
return nova;
}