0% ont trouvé ce document utile (0 vote)
28 vues5 pages

Gestion de file d'attente en C

Transféré par

namex
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats TXT, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
28 vues5 pages

Gestion de file d'attente en C

Transféré par

namex
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats TXT, PDF, TXT ou lisez en ligne sur Scribd

#include <stdio.

h>
#include <stdlib.h>

typedef struct noeud {


int individu;
int nbOccurences;
struct noeud* suivant;
} Element;

typedef Element* File;

Element* allouerNoeud(int individu, int nbOcc) {


Element* noeud = (Element*)malloc(sizeof(Element));
if (noeud != NULL) {
noeud->individu = individu;
noeud->nbOccurences = nbOcc;
noeud->suivant = NULL;
}
return noeud;
}

Element* existe(Element* tete, int individu) {


Element* courant = tete;
while (courant != NULL) {
if (courant->individu == individu) {
return courant;
}
courant = courant->suivant;
}
return NULL;
}

void enfiler(Element** tete, Element** queue, int individu, int nbOcc) {


Element* noeudExiste = existe(*tete, individu);
if (noeudExiste != NULL) {
noeudExiste->nbOccurences += nbOcc;
} else {
Element* nouveau = allouerNoeud(individu, nbOcc);
if (*queue != NULL) {
(*queue)->suivant = nouveau;
}
*queue = nouveau;
if (*tete == NULL) {
*tete = nouveau;
}
}
}

int defiler(Element** tete, Element** queue) {


if (*tete == NULL) {
return -1;
}
int individu = (*tete)->individu;
if ((*tete)->nbOccurences > 1) {
(*tete)->nbOccurences--;
} else {
Element* temp = *tete;
*tete = (*tete)->suivant;
if (*tete == NULL) {
*queue = NULL;
}
free(temp);
}
return individu;
}

int defilerSpecifique(Element** tete, Element** queue, int individu, int nbOcc) {


Element* courant = *tete;
Element* precedent = NULL;

while (courant != NULL && courant->individu != individu) {


precedent = courant;
courant = courant->suivant;
}

if (courant == NULL) {
return -1; // Individu non trouvé
}

if (courant->nbOccurences > nbOcc) {


courant->nbOccurences -= nbOcc;
} else {
if (precedent == NULL) {
*tete = courant->suivant;
} else {
precedent->suivant = courant->suivant;
}
if (courant == *queue) {
*queue = precedent;
}
free(courant);
}
return individu;
}

void affichage(Element* tete) {


Element* courant = tete;
while (courant != NULL) {
printf("Individu : %d, Occurrences : %d\n", courant->individu, courant-
>nbOccurences);
courant = courant->suivant;
}
}

int calculNombreIndividus(Element* tete) {


int total = 0;
Element* courant = tete;
while (courant != NULL) {
total += courant->nbOccurences;
courant = courant->suivant;
}
return total;
}

Element* plusGrandGroupe(Element* tete) {


if (tete == NULL) {
return NULL;
}
Element* maxNoeud = tete;
Element* courant = tete->suivant;
while (courant != NULL) {
if (courant->nbOccurences > maxNoeud->nbOccurences) {
maxNoeud = courant;
}
courant = courant->suivant;
}
return maxNoeud;
}

Element* insererTrie(Element* tete, Element* nouvelElement) {


if (tete == NULL || tete->nbOccurences < nouvelElement->nbOccurences) {
nouvelElement->suivant = tete;
return nouvelElement;
}

Element* courant = tete;


while (courant->suivant != NULL && courant->suivant->nbOccurences >=
nouvelElement->nbOccurences) {
courant = courant->suivant;
}
nouvelElement->suivant = courant->suivant;
courant->suivant = nouvelElement;

return tete;
}

Element* trierParOccurrence(Element* tete) {


Element* nouvelleTete = NULL;

Element* courant = tete;


while (courant != NULL) {
Element* suivant = courant->suivant;
courant->suivant = NULL;
nouvelleTete = insererTrie(nouvelleTete, courant);
courant = suivant;
}

return nouvelleTete;
}

int main() {
File tete = NULL;
File queue = NULL;
int choix;

do {
printf("\nMenu:\n");
printf("1. Enfiler un individu\n");
printf("2. Defiler l'individu en tete\n");
printf("3. Defiler un individu specifique\n");
printf("4. Afficher la file d'attente\n");
printf("5. Calculer le nombre d'individus\n");
printf("6. Trouver le plus grand groupe\n");
printf("7. Trier par occurrences\n");
printf("8. Quitter\n");
printf("Votre choix: ");
scanf("%d", &choix);
switch (choix) {
case 1: {
int individu;
int nbOcc;
printf("Entrez l'individu: ");
scanf("%d", &individu);
printf("Entrez le nombre d'occurrences: ");
scanf("%d", &nbOcc);
enfiler(&tete, &queue, individu, nbOcc);
break;
}
case 2: {
int individuDefile = defiler(&tete, &queue);
if (individuDefile != -1) {
printf("Individu defile: %d\n", individuDefile);
} else {
printf("La file est vide\n");
}
break;
}
case 3: {
int individu, nbOcc;
printf("Entrez l'individu a defiler: ");
scanf("%d", &individu);
printf("Entrez le nombre d'occurrences a enlever: ");
scanf("%d", &nbOcc);
int resultat = defilerSpecifique(&tete, &queue, individu, nbOcc);
if (resultat != -1) {
printf("Individu %d defiler avec succes\n", individu);
} else {
printf("Individu %d non trouve dans la file\n", individu);
}
break;
}
case 4:
affichage(tete);
break;
case 5:
printf("Nombre total d'individus: %d\n",
calculNombreIndividus(tete));
break;
case 6: {
Element* plusGrand = plusGrandGroupe(tete);
if (plusGrand != NULL) {
printf("Le groupe avec le plus grand nombre d'occurrences
est :\n");
printf("Individu : %d, Occurrences : %d\n", plusGrand-
>individu, plusGrand->nbOccurences);
} else {
printf("La file est vide\n");
}
break;
}
case 7:
tete = trierParOccurrence(tete);
printf("File triee par occurrences.\n");
affichage(tete);
break;
case 8:
printf("Au revoir\n");
break;
default:
printf("Choix invalide\n");
}
} while (choix != 8);

return 0;
}

Vous aimerez peut-être aussi