Commit 8147e4ed authored by William Sha's avatar William Sha
Browse files

Merge remote-tracking branch 'origin/master'

parents 764960f6 ef7d18f0
......@@ -8,6 +8,7 @@ int main()
int action = 0; // represents the choice of the user
char str_number[100];
int p = 0;
//char test[5];
printf("*****CHAINED LISTS*****\n");
......@@ -51,18 +52,17 @@ int main()
break;
case TOKEN_ADD_NB_SPC:
insert_after_position(NULL, NULL, 0);
printf("what element do you want to add? ");
scanf("%s", str_number);
printf("\nAt what position do you want to add it? ");
scanf("%d", &p);
insert_after_position(&default_list, str_number, p);
break;
case TOKEN_DEL_NB_SPC:
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));
printf("what element do you want to remove?\n");
scanf("%d", &p);
remove_element(default_list, p);
break;
case TOKEN_SRT_LI_ASC:
......
......@@ -101,8 +101,49 @@ 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){
if (DEBUG) printf("INSERT_AFTER_POSITION\n");
if (!(*list)){
printf("List doesn't exist.\n");
return -1;
}
int length;
length = lengthList(*list);
if((p <= 0) || (p > length)){
printf("The provided value for the position is incorrect.\n");
return -1;
}
if (p == 1 || ((*list)->head == (*list)->tail) & ((*list)->head == NULL)){
insert_beginning_list(list, str);
return 1;
}
if (p == length || ((((*list)->head != NULL) & ((*list)->head == (*list)->tail)))){
insert_end_list(list, str);
return 1;
}
Element* parcour = (*list)->head;
printf("1\n");
int compteur = 0;
while(p != compteur){
parcour = parcour->next;
if(parcour->data == NULL)
compteur++;
}
//a la sortie parcour est à l'element de delimitation de l'element p
printf("2\n");
List *tempList;
initialize(&tempList);
insert_empty_list(&tempList, str);
printf("3\n");
tempList->tail->next = parcour->next;
parcour->next = tempList->head;
free(tempList);
//display(*list);
return 1;
}
int remove_element(List* list, int p){
......@@ -126,7 +167,8 @@ int remove_element(List* list, int p){
e sera utilisé pour pointer la bonne position, suppr sera utilisé pour "manger le nombre" (désallocation)
*/
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");
do{
e = e->next;
}while(e->data != NULL); // on parcoure la liste jusqu'au prochain nombre
......@@ -209,6 +251,12 @@ void display(List* list){
printf("Nothing to display...\n");
return;
}
int c;
c = lengthList(list);
printf("length list: %d\n", c);
Element *p = list->head;
for (p = list->head; p != NULL; p = p->next){
printf("element: %s\n ", p->data);
......@@ -261,3 +309,21 @@ void display_menu(){
}
int lengthList (List *list){
if(!list){
printf("list doesn't exist");
return -1;
}
Element *parcour = list->head;
int compteur = 0;
while(parcour != list->tail){
parcour = parcour->next;
if(parcour->data == NULL)
compteur++;
}
return compteur;
}
......@@ -23,7 +23,7 @@ void initialize(List** list);
void insert_empty_list(List** list, char* str);
void insert_beginning_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 compare(char* str1, char* str2);
int sort(List* list);
......@@ -31,3 +31,4 @@ void display(List* list);
void destruct(List** list);
void display_menu();
int lengthList (List *list);
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