-
Notifications
You must be signed in to change notification settings - Fork 0
/
strutture.c
115 lines (101 loc) · 1.88 KB
/
strutture.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
108
109
110
111
112
113
114
115
/*
* File: strutture.c
* Author: nikko31
*
* Created on May 30, 2014, 11:35 PM
*/
#include "strutture.h"
/*--------PRIMITIVE------------------*/
list emptylist()
{
return NULL;
}
boolean empty(list l)
{
return (l==NULL);
}
element head(list l)
{
if (empty(l)) abort();
else
return l->value;
}
list tail(list l)
{
if (empty(l)) abort();
else
return l->next;
}
list cons(element e, list l)
{
list t;
t = (list) malloc(sizeof(item));
t->value=copy(e);
t->next=l;
return t;
}
//---------------------------
element copy(element e)
{
element el;
el=e;
return el;
}
boolean isEqual(element e1,element e2)
{
return (e1 == e2);
}
boolean isLess(element e1, element e2)
{
return (e1 < e2);
}
void printelement(element el)
{
printf("%d", el);
}
void showlist(list l)
{
printf("[");
while (!empty(l)) {
printelement(head(l));
l = tail(l);
if (!empty(l))
printf(", ");
}
printf("]\n");
}
list insord(element e, list l)
{
list t;
if(empty(l) || !isLess(head(l), e)){
t = cons(e, l); //se vuoto o se head<e
//t->next = l;
return t;
}
return cons(head(l), insord(e, tail(l)));//ricostruisco con head + insord(tail)
}
list insord2(element e, list l)
{
list l1 = NULL, root = l;
list t;
if(empty(l) || !isLess(head(l), e)) //se vuoto o head>e inserisci in testa con cons
{
t = cons(e, l);
return t;
}
t = cons(e, NULL);
while(!empty(l) && isLess(head(l), e))//finchè è pieno e finchè head>e faccio il ciclo
{
l1=l; //l1 diventa l l____l1======|
l = tail(l);//tolgo la testa
}
l1->next = t; //quando esco avro che l è finita oppure ho trovato head>
t->next = l;
return root;
}
element getElement()
{
element el;
scanf("%d", &el);
return el;
}