50 lines
1.1 KiB
C
50 lines
1.1 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
struct liste {
|
|
struct liste* suivant;
|
|
int valeur;
|
|
};
|
|
typedef struct liste liste_t;
|
|
|
|
void free_liste(liste_t* liste) {
|
|
liste_t* cur = liste;
|
|
while(cur != NULL) {
|
|
liste_t* prev = cur;
|
|
cur = cur->suivant;
|
|
free(prev);
|
|
}
|
|
}
|
|
|
|
// Alloue une nouvelle cellule de
|
|
// valeur `val`
|
|
liste_t* alloc_cell(int val) {
|
|
liste_t* cell =
|
|
malloc(sizeof(liste_t));
|
|
cell->valeur = val;
|
|
cell->suivant = NULL;
|
|
return cell;
|
|
}
|
|
|
|
// Affiche les `taille` 1ères valeurs de `liste`
|
|
void afficher_liste(liste_t* liste, int taille) {
|
|
liste_t* cur = liste;
|
|
for(int pos=0; pos < taille; ++pos) {
|
|
printf("%d\n", cur->valeur);
|
|
cur = cur->suivant;
|
|
}
|
|
}
|
|
int main(int argc, char** argv) {
|
|
if(argc < 2) {
|
|
fprintf(stderr, "Missing argument: number of elements to print\n");
|
|
exit(1);
|
|
}
|
|
int n = atoi(argv[1]); // premier argument
|
|
liste_t* tete = alloc_cell(1);
|
|
tete->suivant = alloc_cell(2);
|
|
afficher_liste(tete, n);
|
|
|
|
free_liste(tete);
|
|
return 0;
|
|
}
|