-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
105 lines (99 loc) · 1.6 KB
/
list.c
File metadata and controls
105 lines (99 loc) · 1.6 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
#include"list.h"
List *init_list()
{
List *list = malloc(sizeof(List));
if(list == NULL)
{
printf("Erreur lors de l'allocation de memoire\n");
exit(1);
}
list->first = NULL;
list->nb_symb = 0;
return(list);
}
void add_char(char c, List *list)
{
if(list->first == NULL)
{
Element el = malloc(sizeof(element));
el->carac = c;
el->nb = 1;
el->nxt = NULL;
list->first = el;
list->nb_symb = 1;
}
else
{
Element tmp = list->first;
while(tmp->nxt)
{
if(tmp->carac == c)
{
tmp->nb++;
return;
}
tmp = tmp->nxt;
}
if(tmp->carac == c)
{
tmp->nb++;
return;
}
else
{
Element new_list = malloc(sizeof(element));
new_list->carac = c;
new_list->nb = 1;
new_list->nxt = NULL;
tmp->nxt = new_list;
list->nb_symb++;
}
}
}
void add_freq(char c, int nb, List *list)
{
if(list->first == NULL)
{
Element el = malloc(sizeof(element));
el->carac = c;
el->nb = nb;
el->nxt = NULL;
list->first = el;
list->nb_symb = 1;
}
else
{
Element tmp = list->first;
while(tmp->nxt)
tmp = tmp->nxt;
Element new_list = malloc(sizeof(element));
new_list->carac = c;
new_list->nb = nb;
new_list->nxt = NULL;
tmp->nxt = new_list;
list->nb_symb++;
}
}
void print_list(List list)
{
printf("Affichage d'une liste de symboles\n");
Element tmp = list.first;
while(tmp)
{
printf("Caractere: %c\n", tmp->carac);
printf("Nb d'occurrences: %d\n\n", tmp->nb);
tmp = tmp->nxt;
}
}
void free_element(Element el)
{
if(el == NULL)
return;
free_element(el->nxt);
free(el);
}
void free_list(List *list)
{
free_element(list->first);
free(list);
}