-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlista.c
More file actions
193 lines (180 loc) · 2.49 KB
/
lista.c
File metadata and controls
193 lines (180 loc) · 2.49 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "lista.h"
lista ricerca (lista l, data el)
{
if(l==NULL || l->el == el)
{
return l;
}
else
{
return ricerca(l->next, el);
}
}
lista ricercaIter (lista l, data el)
{
if(l==NULL || l->el == el)
{
return l;
}
else
{
while(l->next!=NULL)
{
if(l->el == el)
return l;
else
l=l->next;
}
return NULL;
}
}
lista inserisci_testa (lista l, data el)
{
struct nodo* temp = malloc(sizeof(struct nodo));
temp->el = el;
temp->next = l;
return temp;
}
lista inserisci_coda (lista l, data el)
{
if(l==NULL)
return inserisci_testa(l,el);
else
{
l->next = inserisci_coda(l->next,el);
return l;
}
}
lista inserisci_codaIter (lista l, data el)
{
if(l==NULL)
return inserisci_testa(l,el);
else
{
struct nodo* coda = malloc(sizeof(struct nodo));
coda->el=el;
coda->next=NULL;
lista temp=l;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=coda;
return l;
}
}
lista rimuovi_testa (lista l)
{
if(l!=NULL)
{
lista temp = l;
l = l->next;
free(temp);
}
return l;
}
lista rimuovi_coda (lista l)
{
if(l!=NULL)
{
if(l->next == NULL)
return rimuovi_testa(l);
else
{
l->next = rimuovi_coda(l->next);
return l;
}
}
else
return NULL;
}
lista rimuovi_codaIter (lista l) //TODO
{
if(l!=NULL)
{
if(l->next == NULL)
return rimuovi_testa(l);
else
{
lista temp=l;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp=rimuovi_testa(temp);
free(temp);
return l;
}
}
}
int lunghezza (lista l)
{
if(l==NULL)
return 0;
else
return 1+lunghezza(l->next);
}
int lunghezzaIter (lista l)
{
int conta=0;
if(l==NULL)
return 0;
else
while(l->next!=NULL)
{
conta++;
l=l->next;
}
}
lista rimuovi(lista l, data el)
{
if(l==NULL)
return l;
if(l->el == el)
return rimuovi_testa(l);
else
{
l->next = rimuovi(l->next,el);
return l;
}
}
lista rimuoviIter(lista l, data el)
{
if(l==NULL)
return l;
if(l->el == el)
return rimuovi_testa(l);
else
{
lista temp=l;
while(temp->next!=NULL && temp->el !=el )
{
temp=temp->next;
}
if(temp->next!=NULL)
return rimuovi_testa(temp->next);
else
return l;
}
}
void stampa(lista l)
{
if (l==NULL)
printf("END\n");
else {
printf("%d -> ",l->el);
stampa(l->next);
}
}
void stampaIter (lista l)
{
int pos=1;
if(l!=NULL)
printf("il valore in posizione 0 = %d\n",l->el);
while(l->next!=NULL)
{
printf("il valore in posizione %d = %d\n",pos,l->next->el);
l=l->next;
pos++;
}
}