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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct e {
char *name;
int nbr;
}t_elem;
typedef struct node {
t_elem data;
struct node *prev;
struct node *next;
}t_node;
//Une fonction pour remplir la liste, mais pas d'interret pour toi
void add_elem(t_node **list, char *name) {
t_elem e;
t_node *elem;
t_node *ptr_debut;
ptr_debut = *list;
e.name = name;
elem = malloc(sizeof(t_node));
elem->data = e;
if (*list == NULL) {
*list = elem;
elem->next = NULL;
elem->prev = NULL;
} else {
while ((*list)->next != NULL) {
*list = (*list)->next;
}
(*list)->next = elem;
elem->prev = *list;
elem->next = NULL;
*list = ptr_debut;
}
}
//La fonction pour tester si tout a bien marché
void afficher_list(t_node *l) {
while (l != NULL) {
printf("%s", l->data.name);
l = l->next;
}
}
int nb_elem_a_copier(t_node *l) {
int i = 0;
while (l != NULL) {
++i;
l = l->next;
}
return i;
}
//La fonction interessante
void copy_part(t_node *list, t_elem source) {
t_node *ptr_debut = list;
//on parcours notre liste
while (list != NULL) {
//Si l'element match
if (strcmp(list->data.name, source.name) == 0) {
//On doit forement enregistrer le nombre d'element
//car plus on va en ajouter, plus la liste va s'agrandir
int nb = nb_elem_a_copier(list);
while (nb != 0) {
add_elem(&ptr_debut, list->data.name);
list = list->next;
nb--;
}
return;
}
list = list->next;
}
}
int main(void) {
t_node *list = NULL;
add_elem(&list, "a");
add_elem(&list, "z");
add_elem(&list, "e");
add_elem(&list, "r");
add_elem(&list, "t");
add_elem(&list, "y");
afficher_list(list);
t_elem e;
e.name = "t";
copy_part(list, e);
printf("\n");
afficher_list(list);
printf("\n");
return EXIT_SUCCESS;
} |
Partager