Commit ba3027cb authored by William Sha's avatar William Sha
Browse files

Fixed issue on remove

Now the first number can be deleted. It was an issue regarding the lost of the head attribute of the list.
parent ce75d2d1
......@@ -14,17 +14,6 @@ int main()
printf("**UTC - November 2017**\n");
printf("*****W.Sha - Y.Ly******\n\n");
/*List *lTest = malloc(sizeof(List));
Element *e1 = malloc(sizeof(Element));
Element *e2 = malloc(sizeof(Element));
e1->data = "yaya";
e1->next = e2;
e2->data = "Ly";
e2->next = NULL;
lTest->head = e1;
lTest->tail = e2;
display(lTest);*/
if (DEBUG) printf("Initialization of an instance of List...\n\n");
List* default_list;
initialize(&default_list);
......@@ -46,7 +35,6 @@ int main()
scanf("%d", &action);
}
if (DEBUG) printf("{action = %d}\n", action);
fflush(stdin);
switch(action){
......@@ -54,7 +42,6 @@ int main()
printf("What do you want to add at the beginning ? ");
scanf("%s", str_number);
insert_beginning_list(&default_list, str_number);
if (DEBUG) printf("Data inserted case : %s\n", default_list->head->data);
break;
case TOKEN_ADD_NB_END:
......@@ -68,7 +55,14 @@ int main()
break;
case TOKEN_DEL_NB_SPC:
remove_element(default_list, 2);
printf("What number do you want to remove ? ");
scanf("%s", str_number);
while(atoi(str_number) <= 0){
fflush(stdin);
printf("Wrong input. Please enter a positive position : ");
scanf("%s", str_number);
}
remove_element(default_list, atoi(str_number));
break;
case TOKEN_SRT_LI_ASC:
......
......@@ -120,17 +120,13 @@ int remove_element(List* list, int p){
int n = 1; // position du parcours
Element* e = list->head;
Element* suppr;
if (DEBUG) printf("FLAG 1\n");
Element* suppr = list->head;
/*
e sera utilisé pour pointer la bonne position, suppr sera utilisé pour "manger le nombre" (désallocation)
e will be used to focus on the right place, suppr will be used to "eat the number"
*/
while(n < p){ // tant que l'on est pas arrivé au Pème chiffre
if (DEBUG) printf("WHILE\n");
do{
e = e->next;
}while(e->data != NULL); // on parcoure la liste jusqu'au prochain nombre
......@@ -145,8 +141,14 @@ int remove_element(List* list, int p){
e suppr
*/
if (p == 1){ // c'est le premier élément de la liste qu'il faut supprimer
e = malloc(sizeof(Element));
e->data = NULL;
e->next = suppr;
list->head = suppr->next;
}
do{
if (DEBUG) printf("DO2\n");
e->next = suppr->next;
free(suppr->data);
free(suppr);
......@@ -159,8 +161,13 @@ int remove_element(List* list, int p){
e->next = NULL;
free(suppr);
}
else{ // sinon on a supprimé au milieu de la liste
e->next = suppr->next;
else{
if (p == 1){
list->head = suppr->next; // sinon on a supprimé au début de la liste
free(e);
}
else
e->next = suppr->next; // sinon on a supprimé au milieu de la liste
free(suppr->data);
free(suppr);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment