67 lines
1.4 KiB
C
67 lines
1.4 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
typedef struct maillon_s {
|
|
struct maillon_s* suivant;
|
|
int value;
|
|
struct maillon_s* premier;
|
|
|
|
} maillon;
|
|
|
|
int Empty(maillon* premier) {
|
|
if (premier == NULL) {
|
|
return 1;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
void Push(int value, maillon* premier) {
|
|
maillon* p = malloc(sizeof(maillon));
|
|
if (!Empty(premier)) {
|
|
p->value = premier->value;
|
|
p->suivant = premier->suivant;
|
|
premier->suivant = p;
|
|
premier->value = value;
|
|
} else {
|
|
premier->value = value;
|
|
}
|
|
}
|
|
|
|
int Pop(maillon* premier) {
|
|
maillon* p;
|
|
int value;
|
|
if (Empty(premier)) {
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
value = premier->value;
|
|
if (premier->suivant==NULL) {
|
|
premier = NULL;
|
|
} else {
|
|
p = premier->suivant;
|
|
premier->value = p->value;
|
|
premier->suivant = p->suivant;
|
|
free(p);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
void AfficherListe(maillon* premier) {
|
|
maillon* p;
|
|
for (p=premier ; p != NULL; p = p->premier) {
|
|
printf("%u %u %u %u %u", p->value);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
int main(int argc, char const argv[]) {
|
|
int entier;
|
|
int i;
|
|
maillon* premier = malloc(sizeof(maillon));
|
|
for(i=1;i<=5;i++) {
|
|
printf("Entier n°%d : ",i);
|
|
scanf("%d", &entier);
|
|
Push(entier, premier);
|
|
}
|
|
AfficherListe(premier);
|
|
return EXIT_SUCCESS;
|
|
} |