2022-12-08 16:44:49 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
struct mail {
|
|
|
|
unsigned short int valeur;
|
|
|
|
struct mail* suivant;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct mail maillon;
|
|
|
|
|
|
|
|
void afficher(maillon* premier)
|
|
|
|
{
|
|
|
|
maillon* p;
|
|
|
|
for(p = premier; p != NULL; p = p->suivant)
|
|
|
|
{
|
|
|
|
printf("%d ", p->valeur);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
maillon* ajouter_debut(maillon* premier,unsigned short int nouveau)
|
|
|
|
{
|
|
|
|
maillon* p = (maillon*) malloc(sizeof(maillon));
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
p->suivant = premier;
|
|
|
|
p->valeur = nouveau;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
maillon* ajouter_dernier(maillon* premier,unsigned short int chiffre)
|
|
|
|
{
|
|
|
|
maillon* nouveau = (maillon*) malloc(sizeof(maillon));
|
|
|
|
nouveau -> suivant = NULL;
|
|
|
|
nouveau ->valeur = chiffre;
|
|
|
|
maillon* p = (maillon*) malloc(sizeof(maillon));
|
|
|
|
for(p = premier; p != NULL; p = p->suivant)
|
|
|
|
{
|
|
|
|
if(p->suivant==NULL)
|
|
|
|
{
|
|
|
|
p->suivant=nouveau;
|
|
|
|
return premier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short int retirer_premier(maillon* premier)
|
|
|
|
{
|
|
|
|
maillon* postp = (maillon*) malloc(sizeof(maillon));
|
|
|
|
postp = premier -> suivant;
|
|
|
|
unsigned short int chiffre = premier -> valeur;
|
2022-12-13 11:29:38 +01:00
|
|
|
premier -> valeur = postp -> valeur;
|
|
|
|
premier ->suivant = postp -> suivant;
|
|
|
|
free(postp);
|
2022-12-08 16:44:49 +01:00
|
|
|
return chiffre;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short int retirer_dernier(maillon* premier)
|
|
|
|
{
|
2022-12-13 11:29:38 +01:00
|
|
|
unsigned short int output;
|
2022-12-08 16:44:49 +01:00
|
|
|
maillon* p = (maillon*) malloc(sizeof(maillon));
|
|
|
|
maillon* anteP = (maillon*) malloc(sizeof(maillon));
|
|
|
|
for(p = premier; p != NULL; p = p->suivant)
|
|
|
|
{
|
|
|
|
if(p->suivant==NULL)
|
|
|
|
{
|
2022-12-13 11:29:38 +01:00
|
|
|
anteP ->suivant = NULL;
|
|
|
|
output = p -> valeur;
|
2022-12-08 16:44:49 +01:00
|
|
|
free(p);
|
2022-12-13 11:29:38 +01:00
|
|
|
return output;
|
2022-12-08 16:44:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
anteP = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maillon* circuler(maillon* premier)
|
|
|
|
{
|
|
|
|
premier= ajouter_debut(premier,retirer_dernier(premier));
|
|
|
|
return premier;
|
|
|
|
}
|
|
|
|
|
|
|
|
maillon* circulerAnti(maillon* premier)
|
|
|
|
{
|
|
|
|
premier=ajouter_dernier(premier,retirer_premier(premier));
|
|
|
|
return premier;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char const *argv[])
|
|
|
|
{
|
|
|
|
srand(time(NULL));
|
|
|
|
unsigned short int number = (rand() % (999 - 111 + 1)) + 111;
|
|
|
|
maillon *mail = malloc(sizeof(maillon));
|
|
|
|
mail -> valeur = number;
|
|
|
|
mail -> suivant = NULL;
|
|
|
|
for (int i = 0; i < 9; ++i)
|
|
|
|
{
|
|
|
|
number = (rand() % (999 - 111 + 1)) + 111;
|
|
|
|
mail = ajouter_debut(mail,number);
|
|
|
|
}
|
|
|
|
|
|
|
|
int max = mail -> valeur;
|
|
|
|
for(maillon* p = mail; p != NULL; p = p->suivant)
|
|
|
|
{
|
|
|
|
if (p->valeur>max)
|
|
|
|
{
|
|
|
|
max = p->valeur;
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 11:29:38 +01:00
|
|
|
/*printf("le max est: %d \n", max);
|
2022-12-08 16:44:49 +01:00
|
|
|
printf("la liste sans le dernier nombre:");
|
|
|
|
retirer_dernier(mail);
|
|
|
|
afficher(mail);
|
|
|
|
printf("la liste cyclé: ");
|
|
|
|
mail=circuler(mail);
|
|
|
|
afficher(mail);
|
|
|
|
printf("ajout de 666 à la fin : ");
|
|
|
|
mail=ajouter_dernier(mail,666);
|
|
|
|
afficher(mail);
|
|
|
|
printf("suppresion du premier element: \n");
|
|
|
|
retirer_premier(mail);
|
|
|
|
afficher(mail);
|
2022-12-13 11:29:38 +01:00
|
|
|
printf("cycle mais dans lautre sens:\n");
|
2022-12-08 16:44:49 +01:00
|
|
|
mail=circulerAnti(mail);
|
|
|
|
afficher(mail);*/
|
|
|
|
|
2022-12-13 11:29:38 +01:00
|
|
|
afficher(mail);
|
|
|
|
int rotation;
|
|
|
|
printf("choisissez une rotation: \n");
|
|
|
|
scanf("%d",&rotation);
|
|
|
|
if(rotation > 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < rotation; ++i)
|
|
|
|
{
|
|
|
|
mail=circuler(mail);
|
|
|
|
}
|
|
|
|
afficher(mail);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
if (rotation < 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < (-1*rotation); ++i)
|
|
|
|
{
|
|
|
|
mail=circulerAnti(mail);
|
|
|
|
}
|
|
|
|
afficher(mail);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
afficher(mail);
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 16:44:49 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|