-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.c
107 lines (89 loc) · 2.01 KB
/
arrays.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "sequences.h"
#define INIT_LMAX 10
sequence* nouvelle_sequence (void)
{
sequence* seq = (sequence*) malloc (sizeof(sequence));
assert (seq != NULL);
seq->LMAX = INIT_LMAX;
seq->longueur = 0;
seq->tab = (char*) malloc (INIT_LMAX * sizeof(char));
assert (seq->tab != NULL);
return seq;
}
void libere_sequence(sequence* seq)
{
assert (seq != NULL);
free (seq->tab);
free (seq);
}
/* fonction interne pour double la taille du tableau s'il devient trop petit
* pour la séquence */
void double_taille (sequence* seq)
{
assert (seq != NULL);
char* new_tab;
/* on pourrait utiliser "realloc" ici mais cela "cache" la recopie
* des éléments en mémoire
* */
new_tab = (char*) malloc (seq->LMAX*2 * sizeof(char));
assert (new_tab != NULL);
for (unsigned int i = 0; i<seq->longueur; i++) {
new_tab[i] = seq->tab[i];
}
free (seq->tab);
seq->tab = new_tab;
seq->LMAX = seq->LMAX*2;
}
unsigned int longueur(sequence* seq)
{
assert (seq != NULL);
return seq->longueur;
}
bool est_vide (sequence *seq)
{
return (seq->longueur == 0);
}
void affiche(sequence* seq)
{
unsigned int i;
assert (seq != NULL);
for (i=0; i<seq->longueur; i++) {
printf ("%c", seq->tab[i]);
}
printf ("\n");
}
void ajout_debut (sequence* seq, char c)
{
assert (seq != NULL);
if (seq->longueur == seq->LMAX) {
double_taille (seq);
}
seq->longueur++;
for (int i=seq->longueur-1; i>0; i--) {
seq->tab[i] = seq->tab[i-1];
}
seq->tab[0] = c;
}
void ajout_fin (sequence* seq, char c)
{
assert (seq != NULL);
if (seq->longueur == seq->LMAX) {
double_taille (seq);
}
seq->longueur++;
seq->tab[seq->longueur-1] = c;
}
int recherche (sequence *seq, char c)
{
unsigned int i = 0;
for (; i<seq->longueur; i++) {
if (seq->tab[i] == c) {
return i;
}
}
return -1;
}