Commit 5282aaec authored by William Sha's avatar William Sha
Browse files

Merge remote-tracking branch 'origin/master'

parents 29f997d4 8db795f2
...@@ -7,14 +7,14 @@ int main() ...@@ -7,14 +7,14 @@ int main()
if (DEBUG) printf("Initialization of ChainedLists...\n\n"); if (DEBUG) printf("Initialization of ChainedLists...\n\n");
int action = 0; // represents the choice of the user int action = 0; // represents the choice of the user
char str_number[5]; char str_number[100];
char test[5]; //char test[5];
printf("*****CHAINED LISTS*****\n"); printf("*****CHAINED LISTS*****\n");
printf("**UTC - November 2017**\n"); printf("**UTC - November 2017**\n");
printf("*****W.Sha - Y.Ly******\n\n"); printf("*****W.Sha - Y.Ly******\n\n");
List *lTest = malloc(sizeof(List)); /*List *lTest = malloc(sizeof(List));
Element *e1 = malloc(sizeof(Element)); Element *e1 = malloc(sizeof(Element));
Element *e2 = malloc(sizeof(Element)); Element *e2 = malloc(sizeof(Element));
e1->data = "yaya"; e1->data = "yaya";
...@@ -23,7 +23,7 @@ int main() ...@@ -23,7 +23,7 @@ int main()
e2->next = NULL; e2->next = NULL;
lTest->head = e1; lTest->head = e1;
lTest->tail = e2; lTest->tail = e2;
display(lTest); display(lTest);*/
if (DEBUG) printf("Initialization of an instance of List...\n\n"); if (DEBUG) printf("Initialization of an instance of List...\n\n");
List* default_list; List* default_list;
...@@ -53,14 +53,14 @@ int main() ...@@ -53,14 +53,14 @@ int main()
case TOKEN_ADD_NB_BEG: case TOKEN_ADD_NB_BEG:
printf("What do you want to add at the beginning ? "); printf("What do you want to add at the beginning ? ");
scanf("%s", str_number); scanf("%s", str_number);
insert_beginning_list(default_list, str_number); insert_beginning_list(&default_list, str_number);
if (DEBUG) printf("Data inserted case : %s\n", default_list->head->data); if (DEBUG) printf("Data inserted case : %s\n", default_list->head->data);
break; break;
case TOKEN_ADD_NB_END: case TOKEN_ADD_NB_END:
printf("What do you want to add at the end ? "); printf("What do you want to add at the end ? ");
scanf("%s", str_number); scanf("%s", str_number);
insert_end_list(default_list, str_number); insert_end_list(&default_list, str_number);
break; break;
case TOKEN_ADD_NB_SPC: case TOKEN_ADD_NB_SPC:
...@@ -81,6 +81,7 @@ int main() ...@@ -81,6 +81,7 @@ int main()
case TOKEN_RMX_LI_ALL: case TOKEN_RMX_LI_ALL:
destruct(&default_list); destruct(&default_list);
initialize(&default_list);
break; break;
} }
......
...@@ -60,46 +60,43 @@ void insert_empty_list(List** list, char* str){ ...@@ -60,46 +60,43 @@ void insert_empty_list(List** list, char* str){
e_vide->data = NULL; e_vide->data = NULL;
e_vide->next = NULL; e_vide->next = NULL;
(*list)->tail->next = e_vide; (*list)->tail->next = e_vide;
(*list)->tail = e_vide;//tail doit pointer sur le dernier element
if (DEBUG) printf("Data inserted : %s\n", (*list)->head->data); if (DEBUG) printf("Data inserted : %s\n", (*list)->head->data);
} }
void insert_beginning_list(List* list, char* str){ void insert_beginning_list(List** list, char* str){
if (DEBUG) printf("INSERT_BEGINNING_LIST(%p, %s)\n", list, str); if (DEBUG) printf("INSERT_BEGINNING_LIST(%p, %s)\n", list, str);
// empty list // empty list
if (list->head == NULL && list->tail == NULL){ if ((*list)->head == NULL && (*list)->tail == NULL){
if (DEBUG) printf("The list is empty...\n"); if (DEBUG) printf("The list is empty...\n");
insert_empty_list(&list, str); insert_empty_list(list, str);
return; return;
} }
Element* e = malloc(sizeof(Element)); List *tempList = malloc(sizeof(List));
if (DEBUG) printf("Trying to put input into a new element...\n"); insert_empty_list(&tempList, str);
tempList->tail->next = (*list)->head;
(*list)->head = tempList->head;
free(tempList);
char tmp[N];
if (DEBUG) printf("Copy of string...\n");
strncpy(tmp, str, N);
e->data = tmp;
// changes the chain to plug the new element
if (DEBUG) printf("Reorganizing the list...\n");
e->next = list->head;
list->head = e;
} }
void insert_end_list(List* list, char* str){ void insert_end_list(List** list, char* str){
if (DEBUG) printf("INSERT_END_LIST\n"); if (DEBUG) printf("INSERT_END_LIST\n");
Element *e = malloc(sizeof(Element)); if ((*list)->head == NULL && (*list)->tail == NULL){
char tmp[N]; if (DEBUG) printf("The list is empty...\n");
strncpy(tmp, str, N); insert_empty_list(list, str);
e->data = tmp; return;
e->next = NULL; }
list->tail->next = e;
list->tail = e; List *tempList = malloc(sizeof(List));
if (DEBUG) printf("Data inserted : %s\n", list->tail->data); insert_empty_list(&tempList, str);
if (DEBUG) printf("Data head : %s\n", list->head->data); (*list)->tail->next = tempList->head;
(*list)->tail = tempList->tail;
free(tempList);
} }
int insert_after_position(List* list, char* str, int p){ int insert_after_position(List* list, char* str, int p){
...@@ -113,7 +110,7 @@ int remove_element(List* list, int p){ ...@@ -113,7 +110,7 @@ int remove_element(List* list, int p){
printf("List pointer is null.\n"); printf("List pointer is null.\n");
return -1; return -1;
} }
/*
if(p <= 0){ if(p <= 0){
printf("The provided value for the position is incorrect.\n"); printf("The provided value for the position is incorrect.\n");
return -1; return -1;
...@@ -129,7 +126,7 @@ int remove_element(List* list, int p){ ...@@ -129,7 +126,7 @@ int remove_element(List* list, int p){
e sera utilisé pour pointer la bonne position, suppr sera utilisé pour "manger le nombre" (désallocation) 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" 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 while(n < p){ // tant que l'on est pas arrivé au Pème chiffre
if (DEBUG) printf("WHILE\n"); if (DEBUG) printf("WHILE\n");
do{ do{
...@@ -137,15 +134,8 @@ int remove_element(List* list, int p){ ...@@ -137,15 +134,8 @@ int remove_element(List* list, int p){
}while(e->data != NULL); // on parcoure la liste jusqu'au prochain nombre }while(e->data != NULL); // on parcoure la liste jusqu'au prochain nombre
suppr = e->next; suppr = e->next;
n++; n++;
} }*/
/*
/* à ce niveau, on en est là :
P
| ##### | ##### | ¤¤¤¤¤ | ##### | ##### | ¤¤¤¤¤ |
^ ^
e suppr
*/
do{ do{
if (DEBUG) printf("DO2\n"); if (DEBUG) printf("DO2\n");
e->next = suppr->next; e->next = suppr->next;
...@@ -166,8 +156,8 @@ int remove_element(List* list, int p){ ...@@ -166,8 +156,8 @@ int remove_element(List* list, int p){
free(suppr); free(suppr);
} }
return 0; return 0;*/
} }
int compare(char* str1, char* str2){ int compare(char* str1, char* str2){
if (DEBUG) printf("COMPARE\n"); if (DEBUG) printf("COMPARE\n");
...@@ -219,7 +209,7 @@ void destruct(List** list){ ...@@ -219,7 +209,7 @@ void destruct(List** list){
return; return;
} }
while((*list)->head != (*list)->tail){ while((*list)->head->next != NULL){/*cetait pas la bonne condition*/
printf("while\n"); printf("while\n");
Element* e = (*list)->head; Element* e = (*list)->head;
printf("pointeurs : list head : %p, e : %p\n", (*list)->head, e); printf("pointeurs : list head : %p, e : %p\n", (*list)->head, e);
...@@ -230,7 +220,9 @@ void destruct(List** list){ ...@@ -230,7 +220,9 @@ void destruct(List** list){
printf("free\n"); printf("free\n");
printf("pointeurs : list head : %p, e : %p\n", (*list)->head, e); printf("pointeurs : list head : %p, e : %p\n", (*list)->head, e);
free(e->data); free(e->data);
printf("free2\n");
free(e); free(e);
printf("free3\n");
} }
free(*list); free(*list);
...@@ -250,5 +242,6 @@ void display_menu(){ ...@@ -250,5 +242,6 @@ void display_menu(){
printf("7 - Destroy the list\n"); printf("7 - Destroy the list\n");
printf("8 - Quit\n"); printf("8 - Quit\n");
printf("**************************************\n\n"); printf("**************************************\n\n");
} }
...@@ -21,8 +21,8 @@ typedef struct list{ ...@@ -21,8 +21,8 @@ typedef struct list{
void initialize(List** list); void initialize(List** list);
void insert_empty_list(List** list, char* str); void insert_empty_list(List** list, char* str);
void insert_beginning_list(List* list, char* str); void insert_beginning_list(List** list, char* str);
void insert_end_list(List* list, char* str); void insert_end_list(List** list, char* str);
int insert_after_position(List* list, char* str, int p); int insert_after_position(List* list, char* str, int p);
int remove_element(List* list, int p); int remove_element(List* list, int p);
int compare(char* str1, char* str2); int compare(char* str1, char* str2);
......
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