tp7 exo1
This commit is contained in:
parent
baff61f961
commit
a6ca349c40
46
tp7/exo1.c
Normal file
46
tp7/exo1.c
Normal file
@ -0,0 +1,46 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct t_r_i {
|
||||
int taille;
|
||||
int capacite;
|
||||
int *donnees;
|
||||
};
|
||||
typedef struct t_r_i itr;
|
||||
itr creer_itr(int capacite) {
|
||||
int c = 1;
|
||||
while (capacite) {
|
||||
c *= 2;
|
||||
capacite /= 2;
|
||||
}
|
||||
itr t;
|
||||
t.taille = 0;
|
||||
t.capacite = c;
|
||||
t.donnees = malloc(c * sizeof(int));
|
||||
return t;
|
||||
}
|
||||
int taille_itr(itr t) { return t.taille; }
|
||||
int acces_itr(itr t, int i) { return t.donnees[i]; }
|
||||
void modif_itr(itr t, int i, int x) { t.donnees[i] = x; }
|
||||
void append_itr(itr *t, int x) {
|
||||
if (t->taille + 1 <= t->capacite) {
|
||||
t->donnees[t->taille] = x;
|
||||
} else {
|
||||
int c = t->capacite * 2;
|
||||
int *t2 = malloc(c * sizeof(int));
|
||||
|
||||
for (int i = 0; i < t->taille; i += 1) {
|
||||
t2[i] = t->donnees[i];
|
||||
}
|
||||
t2[t->taille] = x;
|
||||
free(t->donnees);
|
||||
t->donnees = t2;
|
||||
}
|
||||
}
|
||||
int pop_itr(itr *t) {
|
||||
t->taille = t->taille - 1;
|
||||
return t->donnees[t->taille];
|
||||
}
|
||||
// let's goooooooooo maxime primeeeeeeeee
|
||||
Loading…
Reference in New Issue
Block a user