#include<stdio.h>
#include<string.h>
#include<stdlib.h>

//Structure
typedef struct _Personne{
    char nom[40];
    char prenom[60];
    char passwd[10];
    struct _Personne* pnext;
}Personne;

//Prototypages
void afficherListeChainee(Personne *pdebut2);
int tailleListe(Personne *pDebut);
void triBulle(Personne* pDebut);
Personne* creerTete();
void ajouterPersonne(Personne* pdebut);
int verifierDoublons(Personne* pdebut,char* nom, char* prenom);
void splitt(Personne* pdebut, Personne* un, Personne* deux);
void ajouterPersonneDansListe(Personne* liste,char* nom,char* prenom,char* passwd);
void menu(Personne* pinit, Personne* pinit1, Personne* pinit2);

//Main
int main(){
    Personne *pinit;
    Personne *pinit1 = (Personne*)malloc(sizeof(Personne));
    Personne *pinit2 = (Personne*)malloc(sizeof(Personne));

    menu(pinit,pinit1,pinit2);

}

void menu(Personne* pinit, Personne* pinit1, Personne* pinit2){
    int choix;
    Personne* pdebut = pinit;
    Personne* pdebut1 = pinit1;
    Personne* pdebut2 = pinit2;

    while (choix != 7){
        printf("*** MENU ***\n");
        printf("0 - Creer une liste\n");
        printf("1 - Saisir une personne\n");
        printf("2 - Afficher la Liste \n");
        printf("3 - tri a bulle\n");
        printf("4 - Taille de la liste\n");
        printf("5 - Split de la liste\n");
        printf("6 - Quitter\n");
        printf("\nFaites votre selection : \n");


		scanf("%d",&choix);

		switch (choix){
			case 0 :
				 pdebut = creerTete();
				 break;

            case 1 :
                 ajouterPersonne(pdebut);
				 break;

			case 2 :
				 afficherListeChainee(pdebut);
				 break;

            case 3:
                triBulle(pdebut);
                break;

            case 4:
                printf("La liste contient %d personnes \n", tailleListe(pdebut));
                break;

			case 5 :
                splitt(pdebut, pdebut1, pdebut2);
                printf("Liste 1:\n");
                afficherListeChainee(pdebut1);
                printf("\n\nListe 2:\n");
                afficherListeChainee(pdebut2);
                break;

			case 6 :
				 exit(0);
				 break;

			default :
				  printf("Mauvais choix \n");

		}
	}
}

Personne* creerTete(){
    Personne* pdebut;
    char nom[40];
    char prenom[60];
    char passwd[10];

    printf("\nEntrez votre nom : ");
    scanf("%s",&nom);

    if(strcmp(nom,"FIN") == 0){
        return 0;
    }else{
        printf("\nEntrez votre prenom : ");
        scanf("%s",&prenom);

        printf("\nEntrez votre passwd : ");
        scanf("%s",&passwd);

        pdebut = (Personne*)malloc(sizeof(Personne));
        strcpy(pdebut->nom,nom);
        strcpy(pdebut->prenom,prenom);
        strcpy(pdebut->passwd,passwd);
        pdebut->pnext = NULL;
        return pdebut;
    }
}

void ajouterPersonneDansListe(Personne* liste,char* nom,char* prenom,char* passwd){
        Personne* temp = (Personne*)malloc(sizeof(Personne));
        strcpy(temp->nom,nom);
        strcpy(temp->prenom,prenom);
        strcpy(temp->passwd,passwd);
        liste->pnext = temp;
        liste = liste->pnext;
}

void ajouterPersonne(Personne* pdebut){
    Personne* pcourant = pdebut;
    char nom[40];
    char prenom[60];
    char passwd[10];
    int boolean = 0;

    do{
        printf("\nEntrez votre nom : ");
        scanf("%s",&nom);

        if(strcmp(nom,"FIN") == 0){
            boolean = 1;
        }else{
            printf("\nEntrez votre prenom : ");
            scanf("%s",&prenom);

            printf("\nEntrez votre passwd : ");
            scanf("%s",&passwd);

            if(verifierDoublons(pdebut,nom,prenom)==1){
                pcourant->pnext = (Personne*)malloc(sizeof(Personne));
                strcpy(pcourant->pnext->nom,nom);
                strcpy(pcourant->pnext->prenom,prenom);
                strcpy(pcourant->pnext->passwd,passwd);
                pcourant->pnext->pnext = NULL;
                pcourant=pcourant->pnext;
                boolean = 0;
            }
        }
    }while(boolean != 1);
}

int verifierDoublons(Personne* pdebut,char* nom, char* prenom){
    Personne* pcourant = pdebut;

    do{
        if(strcmp(pcourant->nom,nom)==0){
            if (strcmp(pcourant->prenom,prenom)==0){
                return 0;
            }
        }
        pcourant = pcourant->pnext;
    }while(pcourant!= NULL);
    return 1;
}

void afficherListeChainee(Personne *pdebut){
    Personne *pcourant=pdebut;
    int boolean4=0;
    printf("\n");
    while(boolean4!=1){
        if(pcourant!=NULL){
            printf("%s - %s - %s -----> ",pcourant->nom,pcourant->prenom,pcourant->passwd);
            pcourant=pcourant->pnext;
        }else{
            boolean4=1;
        }
    }
}

void splitt(Personne* pdebut, Personne* un, Personne* deux){

    Personne* pcourant = pdebut;

    int compteur = 0;

    do{
        if(compteur % 2 == 0){
            ajouterPersonneDansListe(un,pcourant->nom,pcourant->prenom,pcourant->passwd);
            printf("un\n");
        }else{
            ajouterPersonneDansListe(deux,pcourant->nom,pcourant->prenom,pcourant->passwd);
            printf("deux\n");
        }
        compteur++;
        pcourant = pcourant->pnext;
    }while(pcourant!= NULL);

}

int tailleListe(Personne *pDebut){
    Personne *pcourant=pDebut;
    int boolean4 = 0;
    int compteur = 0;
    while(boolean4!=1){
        if(pcourant!=NULL){
            compteur++;
            pcourant=pcourant->pnext;
        }else{
            boolean4=1;
        }
    }
    return compteur;
}

void triBulle(Personne* pDebut){
     Personne* p = pDebut;

     Personne* temp = (Personne*)malloc(sizeof(Personne));
     int inversion;

     do{
        p = pDebut;
        inversion=0;
        do{
            if (strcmp(p->nom,p->pnext->nom)>0){
                strcpy(temp->nom,p->nom);
                strcpy(temp->prenom,p->prenom);
                strcpy(temp->passwd,p->passwd);
                strcpy(p->nom,p->pnext->nom);
                strcpy(p->prenom,p->pnext->prenom);
                strcpy(p->passwd,p->pnext->passwd);
                strcpy(p->pnext->nom,temp->nom);
                strcpy(p->pnext->prenom,temp->prenom);
                strcpy(p->pnext->passwd,temp->passwd);
                inversion=1;
            }else{
                if(strcmp(p->nom,p->pnext->nom)==0){
                    if (strcmp(p->prenom,p->pnext->prenom)>0){
                        strcpy(temp->nom,p->nom);
                        strcpy(temp->prenom,p->prenom);
                        strcpy(temp->passwd,p->passwd);
                        strcpy(p->nom,p->pnext->nom);
                        strcpy(p->prenom,p->pnext->prenom);
                        strcpy(p->passwd,p->pnext->passwd);
                        strcpy(p->pnext->nom,temp->nom);
                        strcpy(p->pnext->prenom,temp->prenom);
                        strcpy(p->pnext->passwd,temp->passwd);
                        inversion=1;
                    }
                }
            }
            p = p->pnext;

        }while(p->pnext != NULL);

    }while(inversion == 1);
}
