This repository has been archived on 2019-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
s6-pa-tp/TP5/supplementaires.c

169 lines
3.1 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
struct Cellule {
struct Cellule* suiv;
int val;
int mult;
};
typedef struct Cellule Cellule;
void ajout_tete(Cellule** l, int val, int mult) {
Cellule* p;
p = malloc(sizeof(Cellule));
p->val = val;
p->suiv = *l;
p->mult = mult;
*l = p;
}
void imprimer(Cellule* l) {
while (l != NULL) {
printf("%d × %d\n", l->val, l->mult);
l = l->suiv;
}
}
void supprimer_tete(Cellule** l) {
Cellule* p;
p = (*l)->suiv;
free(*l);
*l = p;
}
void inserer(Cellule** p, int val) {
Cellule* l = *p;
if (l == NULL) {
ajout_tete(p, val, 1);
} else if (l->val == val) {
l->mult++;
} else if (l->val > val) {
ajout_tete(p, val, 1);
} else {
inserer(&l->suiv, val);
}
}
bool est_trie(Cellule* l) {
if (l == NULL || l->suiv == NULL) {
return true;
}
if (l->val < l->suiv->val) {
return est_trie(l->suiv);
} else {
return false;
}
}
void desallouer(Cellule* l) {
if (l == NULL) {
return;
}
desallouer(l->suiv);
free(l);
}
int multiplicite(Cellule* l, int val) {
if (l == NULL) {
return 0;
}
if (l->val == val) {
return l->mult;
} else {
return multiplicite(l->suiv, val);
}
}
int cardinal(Cellule* l) {
if (l == NULL) {
return 0;
}
return l->mult + cardinal(l->suiv);
}
// Pour simplifier l'affichage
void details(Cellule *l, char nom) {
printf("\nListe %c :\n", nom);
imprimer(l);
if (est_trie(l)) {
printf("→ Est trié\n");
} else {
printf("→ N'est pas triée\n");
}
}
Cellule* union_me(Cellule* j, Cellule* k) {
Cellule* u;
int i;
if (j == NULL) {
if (k == NULL) {
return NULL;
} else {
u = union_me(NULL, k->suiv);
for (i = 0; i < k->mult; i++) {
inserer(&u, k->val);
}
return u;
}
} else {
u = union_me(j->suiv, k);
for (i = 0; i < j->mult; i++) {
inserer(&u, j->val);
}
return u;
}
}
int min(int a, int b) {
if (a > b) {
return b;
} else {
return a;
}
}
Cellule* intersection_me(Cellule* j, Cellule* k) {
if (j == NULL || k == NULL) {
return NULL;
} else if (j->val == k->val) {
Cellule* i;
i = intersection_me(j->suiv, k->suiv);
inserer(&i, min(j->val, k->val));
return i;
} else if (j->val < k->val) {
return intersection_me(j->suiv, k);
} else {
return intersection_me(j, k->suiv);
}
}
int main() {
Cellule* j = NULL;
inserer(&j, 3);
inserer(&j, 2);
inserer(&j, 3);
inserer(&j, 2);
inserer(&j, 1);
inserer(&j, 3);
details(j, 'j');
Cellule* k = NULL;
inserer(&k, 3);
inserer(&k, 3);
inserer(&k, 2);
inserer(&k, 5);
inserer(&k, 5);
inserer(&k, 5);
details(k, 'k');
Cellule* u = union_me(j, k);
details(u, 'u');
Cellule* i = intersection_me(j, k);
details(i, 'i');
}