#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdarg.h>
//auteur : Raphael De Oliveira
struct livre{
char* titre;
char* auteur;
short int age;
struct livre * suivant;
}__attribute__((packed));
void increment_element(struct livre* elem){
*elem=*elem->suivant;
}
static struct livre* ajout(char * t, char* a, int y){
static struct livre* premier;
static struct livre* fin;
struct livre* s;
s = malloc(sizeof(struct livre));
s->titre = malloc(sizeof(char*));
s->titre = t;
s->auteur = malloc(sizeof(char*));
s->auteur = a;
s->age = malloc(sizeof(int));
s->age = y;
s->suivant = malloc(NULL);
s->suivant = NULL;
if(premier==NULL){
premier=s;
fin = premier;
return premier;
}
fin->suivant = s;
fin = s;
}
void affichage_bibliotheque(struct livre * elem){
while(elem->suivant!=NULL){
printf("%s | %s\n",elem->titre,elem->auteur);
//elem=elem->suivant;
increment_element(elem);
}
printf("%s| %s\n",elem->titre,elem->auteur);
}
static struct livre* initialisation(){
struct livre* elem = malloc(sizeof(struct livre));
elem = ajout("À la recherche du temps perdu","Albert Camus",10);
return elem;
}
void clear(struct livre* elem){
struct livre *transi;
transi = elem;
while(transi->suivant!=NULL){
free(transi->age);
free(transi->auteur);
free(transi->titre);
free(transi);
transi=transi->suivant;
}
free(elem);
printf("-----------------CLEAR FINI AVEC SUCCES----------------\n");
}
void supp_premier_element(struct livre* elem){
struct livre* temp;
temp = elem;
temp=temp->suivant;
elem->auteur=temp->auteur;
elem->titre=temp->titre;
elem->suivant=temp->suivant;
}
void trier(struct livre* prem){// ici on va ranger les elemants dans l'ordre croisssant avant de les afficher...
struct livre* temp;
struct livre* temp3;
struct livre* temp1;
char* min;
temp3=malloc(sizeof(struct livre));
temp1=malloc(sizeof(struct livre));
for(temp=prem ; temp!=NULL ; temp=temp->suivant)
{
temp3=temp;
min=temp->auteur;
for(temp1=temp->suivant ; temp1!=NULL ; temp1=temp1->suivant)
{
if(min[0] > temp1->auteur[0])
{
temp3=temp1; // le 3è temporaire est l'adresse de l'élement où se trouve le minimum
min=temp3->auteur;
}
}
temp3->auteur=temp->auteur; //echange des 2 elements...
temp->auteur=min;
}
return;
}
void main(int argc, char *argv[]){
struct livre* elem = initialisation();
ajout("Demande à la poussière", "John Fante", 12);
ajout("Le bruit et la fureur", "William Faulkner", 2);
ajout("Madame Bovary", "Gustave Flaubert", 200);
ajout("La Divine Comédie","Léon Tolstoï",30);
trier(elem);
supp_premier_element(elem);
printf("Voici la liste chainée trié par ordre alphabétique par noms d'auteurs des livres de la bibliotheque :\n\n");
affichage_bibliotheque(elem);
clear(elem);
return;
}
Add a code snippet to your website: www.paste.org