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

Implemented remove function but...

... it must be tested when we'll be able to insert at a specific position. Some minor fixes for main and other functions.
parent d2b106fe
......@@ -25,7 +25,6 @@ int main()
lTest->tail = e2;
display(lTest);
if (DEBUG) printf("Initialization of an instance of List...\n\n");
List* default_list;
initialize(&default_list);
......@@ -81,7 +80,7 @@ int main()
break;
case TOKEN_RMX_LI_ALL:
destruct(default_list);
destruct(&default_list);
break;
}
......
......@@ -19,7 +19,7 @@ void initialize(List** list){
void insert_empty_list(List** list, char* str){
if (DEBUG) printf("INSERT_EMPTY_LIST\n");
if(*list) printf("cool\n");
if ((*list)->head || (*list)->tail){
printf("List is not empty... use another insert function.\n");
return;
......@@ -34,10 +34,8 @@ void insert_empty_list(List** list, char* str){
// Le brancher
int length = strlen(str), total = 0, i;
printf("str length : %d\n", length);
while(length > 0){
if (DEBUG) printf("WHILE\n");
i = 0;
Element* e = malloc(sizeof(Element));
e->data = malloc(sizeof(char)*N);
......@@ -56,26 +54,13 @@ void insert_empty_list(List** list, char* str){
else
(*list)->tail->next = e;
(*list)->tail = e;
if (DEBUG) printf("element while : %s\n", e->data);
}
if (DEBUG) printf("FLAG 2\n");
Element* e_vide = malloc(sizeof(Element));
e_vide->data = NULL;
e_vide->next = NULL;
(*list)->tail->next = e_vide;
if (DEBUG) printf("FLAG 3\n");
/*Element* e = malloc(sizeof(Element));
char tmp[N];
strncpy(tmp, str, N);
e->data = malloc(sizeof(char)*N);
e->data = tmp;
e->next = NULL;
list->head = e;
list->tail = e;*/
if (DEBUG) printf("Data inserted : %s\n", (*list)->head->data);
}
......@@ -123,6 +108,59 @@ int insert_after_position(List* list, char* str, int p){
int remove_element(List* list, int p){
if (DEBUG) printf("REMOVE_ELEMENT\n");
if (!*list){
printf("List pointer is null.\n");
return -1;
}
if(p <= 0){
printf("The provided value for the position is incorrect.\n");
return -1;
}
int n = 1; // position du parcours
Element* e = list->head, suppr;
/*
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
do{
e = e->next;
}while(e->data != NULL); // on parcoure la liste jusqu'au prochain nombre
suppr = e->next;
n++;
}
/* à ce niveau, on en est là :
P
| ##### | ##### | ¤¤¤¤¤ | ##### | ##### | ¤¤¤¤¤ |
^ ^
e suppr
*/
do{
e->next = suppr->next;
free(suppr->data);
free(suppr);
suppr = e->next;
}while(suppr->data != NULL);
if (suppr == list->tail){ // si c'était le dernier nombre de la liste
free(list->tail->data);
list->tail = e;
e->next = NULL;
free(suppr);
}
else{ // sinon on a supprimé au milieu de la liste
e->next = suppr->next;
free(suppr->data);
free(suppr);
}
return 0;
}
int compare(char* str1, char* str2){
......@@ -172,6 +210,7 @@ void destruct(List** list){
if (!*list){
printf("List is already destroyed...\n");
return;
}
while((*list)->head != (*list)->tail){
......
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