Bonsoir à tous,
J'ai un petit problème, j'ai une liste chainée double qui elle, fonctionne bien, j'ai une fonction load_fichier ki charge en mémoire la liste chainée à partir d'un fichier .csv puis une fonction print_list qui l'affiche à l'écran. (jusqu'ici tout va bien)
Ensuite j'ai fait une fonction de tri par insertion et lorsque j'exécute, ça nfait rien, pas de réponse, donc soit le programme tourne en boucle soit je ne sais pas.
Voila ma structure :
Voila ma fonction de tri par insertion:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 struct DATA{ char nom[15]; char prenom[15]; char initial[10]; char email[35]; char num_tel[10]; int taille; char classe[15]; struct DATA *next; struct DATA *previous; }; typedef struct DATA ELEVE;
Ma fonction Main ressemble à ça:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 void tri_inserstion_list(ELEVE *list_debut) { int i,boolean; ELEVE *tmp1,*tmp2,*tmp3,*tmp4; tmp1 = list_debut->next; for(i=1;i<list_debut->taille;i++) { tmp2 = tmp1->next; tmp3 = tmp2->next; do{ boolean = 0; if(tmp1->previous == NULL) { if(tmp1->nom[0]>tmp2->nom[0]) { tmp1->next = NULL; tmp2->next = NULL; tmp2->previous = NULL; tmp3->previous = NULL; tmp1->next = tmp3; tmp3->previous = tmp1; tmp2->next = tmp1; tmp1->previous = tmp2; boolean = 1; } } else{ if(tmp3->next == NULL) { if(tmp2->nom[0]>tmp3->nom[0]) { tmp1->next = NULL; tmp2->next = NULL; tmp2->previous = NULL; tmp3->previous = NULL; tmp1->next = tmp3; tmp3->previous = tmp1; tmp3->next = tmp2; tmp2->previous = tmp3; boolean = 1; } } else{ if(tmp1->nom[0]>tmp2->nom[0]) { tmp1->previous = tmp4; tmp2->next = NULL; tmp2->previous = NULL; tmp3->previous = NULL; tmp1->next = NULL; tmp4->next = NULL; tmp1->previous = NULL; tmp4->next = tmp2; tmp2->previous = tmp4; tmp2->next = tmp1; tmp1->previous = tmp2; tmp2->next = tmp3; tmp3->previous = tmp1; boolean = 1; } } } }while(boolean == 1); tmp1 = tmp1->next; } }
J'ai enfet essayé d'adapter la fonction de tri d'un tableau d'entier à celui d'une liste chainée.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 int main() { ELEVE *list = load_fichier("liste.csv"); print_liste(list); tri_inserstion_list(list); printf("\n\n"); print_liste(list); //save_fichier(list); list_delete(list); return 0; }
Celle du tableau d'entier fonctionne bien :
Pour ma fonction tri de la liste chainée, je ne vérifie pour l'instant que la première lettre du de la chaine "nom" de chaque structure.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 int main() { int i,tmp,cpt; int boolean,n=0; printf("\n entrer la taille du tableau : "); scanf("%d",&n); int *t=malloc(n * sizeof(int)); for(i=0;i<n;i++) { printf("\n la valeur de la case %d : ",i+1); scanf("%d",&t[i]); } for(i=1;i<n;i++) { tmp = t[i]; cpt = i-1; do { boolean = 0; if(t[cpt]>tmp) { t[cpt+1] = t[cpt]; cpt = cpt - 1; boolean = 1; } if(cpt<0) boolean = 0; }while(boolean == 1); t[cpt+1] = tmp; } for(i=0;i<n;i++) printf("\n %d \n",t[i]); return 0; }
Merci de vouloir m'aider...
Partager