Bonjour,
Débutant en C, je souhaiterai savoir comment changer ce programme initiale (liste chainée) qui demande à l'utilisateur de choisir les options selon l'affichage :
1 : ajout dans la liste
2 : suppression dans liste
3 : trie de la liste
4 : affiche la liste
0 : quit programme
Je voudrai que celui-ci lance le programme suivi de l'option puis les paramêtres (selon option choisi).
Par explemple, ./monprog -a elementAajouter (qui correspond à l'option 1)
./monprog -b elementAsupp (qui correspond à l'option 2)
./monprog -c (option 3)
./monprog -d (option 4)
./monprog -q (option 0)
Avez vous des conseils, des exemples, des idées ?
Merci
Voici le code :
#include<stdio.h>
#include<malloc.h>
#define Faux 0
#define Vrai 1
typedef struct Element{
int val;
struct Element *next;
}Element;
int estVide(Element* L){
if(L == NULL){
return Vrai;
}
else{
return Faux;
}
}
void ajouter(Element **L, int v){
Element *tmp = *L;
Element *nouveau = (Element*)malloc(sizeof(Element));
if(nouveau == NULL){
exit(1);
}
nouveau->val = v;
nouveau->next = NULL;
if(estVide(*L)){
*L = nouveau;
}
else{
tmp = *L;
while(tmp->next != NULL){
tmp = tmp->next;
}
tmp->next = nouveau;
}
}
void Trier(Element **L){
Element *tmp1, *tmp2;
Element *Min;
int t;
tmp1 = *L;
while(tmp1 != NULL){
Min = tmp1;
tmp2 = tmp1;
while(tmp2 != NULL){
if(Min->val > tmp2->val) Min = tmp2;
tmp2 = tmp2->next;
}
t = tmp1->val;
tmp1->val = Min->val;
Min->val = t;
tmp1=tmp1->next;
}
}
void Supprimer(Element **L, int v){
Element *tmp;
int d;
if(!estVide(*L)){
tmp = *L;
d = 0;
if(tmp->val == v){
*L = (*L)->next;
}
else{
while( d == 0 && tmp->next != NULL){
if(tmp->next->val == v) d = 1;
else tmp = tmp->next;
}
if(d == 1) tmp->next = tmp->next->next;
else printf("!cette valeur n'est pas trouvable!\n");
}
}
else{
printf("!La liste est vide. Impossible de faire la suppression!\n");
}
}
int Longueur(Element *L){
int l=0;
Element *tmp = L;
while(tmp != NULL){
l++;
tmp = tmp->next;
}
return l;
}
void afficher(Element *L){
Element *tmp;
if(estVide(L)){
printf("!la liste est vide!\n");
}
else{
tmp = L;
while(tmp != NULL){
printf("%d ",tmp->val);
tmp = tmp->next;
}
printf(" - longueur : %d\n",Longueur(L));
}
}
int main(){
Element *L = NULL;
int choix;
char *fichier;
int v;
do{
printf("1-Ajouter Element.\n");
printf("2-Supprimer Element.\n");
printf("3-Trier la Liste.\n");
printf("4-Afficher.\n");
printf("0-Quitter.\n");
printf(" -->choix(0-4):");scanf("%d",&choix);
switch(choix){
case 1:printf(" - Entrer une valeur : "); scanf("%d",&v);
ajouter(&L, v);
break;
case 2:printf(" - Entrer une valeur : "); scanf("%d",&v);
Supprimer(&L,v);
break;
case 3:Trier(&L);
break;
case 4:afficher(L);
break;
}
}while(choix != 0);
return 0;
}