Browse Source

Fin TP5&6

master
Elias Leinenweber 6 months ago
parent
commit
b0434ed0ae
  1. 60
      TP5and6/main.c

60
TP5and6/main.c

@ -86,19 +86,11 @@ main(int argc, char *argv[])
printf("Test of the \"search\" function finished: evaluate the result.\n");
printf ("**************************************************\n\n");
printf ("\n\n**************************************************\n");
printf("Test of the \"insert\" function ...\n");
Student_t student_z = { "ZZZ", "should_be_last", 42 };
Student_t student_a = { "AAA", "should_be_first", 42 };
Student_t student_o = { "OOO", "should_be_middle", 42 };
Link_t * link_z = new_link(student_z);
Link_t * link_a = new_link(student_a);
Link_t * link_o = new_link(student_o);
printf("Test of the \"insert\" function...\n");
first_link = insert(first_link, link_z);
first_link = insert(first_link, link_a);
first_link = insert(first_link, link_o);
first_link = insert(first_link, new_link((Student_t){ "ZZZ", "should_be_last", 42 }));
first_link = insert(first_link, new_link((Student_t){ "AAA", "should_be_first", 42 }));
first_link = insert(first_link, new_link((Student_t){ "OOO", "should_be_middle", 42 }));
printf ("-------------- Content of the list after insertion -------------- \n");
display_linked_list(first_link);
@ -107,24 +99,17 @@ main(int argc, char *argv[])
printf ("\n\n**************************************************\n");
printf("Test of the \"sort\" function ...\n");
Link_t * sorted_list = sort(first_link);
printf ("-------------- Content of the list after sort -------------- \n");
display_linked_list(sorted_list);
display_linked_list(sort(first_link));
printf("Test of the \"sort\" function finished: evaluate the result.\n");
printf ("**************************************************\n\n");
/***** test free ********/
printf ("\n\n**************************************************\n");
printf("Test of the \"free_list\" function ...\n");
free_list(first_link);
printf("Test of the \"free_list\" function finished.\n");
printf ("**************************************************\n\n");
printf ("\n\n**************************************************\n");
printf ("Tests passed?\n\t NO --> continue ! \n\t YES --> This is the end ... Hold your breath and count to ten ... Next: Makefiles and OS ... \n");
printf ("**************************************************\n\n");
printf ("\n\n**************************************************\n");
printf("Test of the \"free_list\" function ...\n");
free_list(first_link);
printf("Test of the \"free_list\" function finished.\n");
return 0;
return EXIT_SUCCESS;
}
/*
@ -214,10 +199,12 @@ insert(Link_t *beginning, Link_t *new_link)
{
Link_t *list_after_insertion = beginning;
if (!beginning || strcmp(new_link->student.lastname, beginning->student.lastname) < 0)
if (!beginning || strcmp(new_link->student.lastname,
beginning->student.lastname) < 0)
list_after_insertion = chain(beginning, new_link);
else {
while (beginning->next && strcmp(new_link->student.lastname, beginning->next->student.lastname) >= 0)
while (beginning->next && strcmp(new_link->student.lastname,
beginning->next->student.lastname) >= 0)
beginning = beginning->next;
beginning->next = chain(beginning->next, new_link);
}
@ -240,15 +227,18 @@ sort(Link_t *beginning)
return sorted_list;
}
/******************** free_list *******************
* Free the memory used by the linked list.
********************************************************/
void free_list(Link_t * list){
/* TODO: implement this function */
printf("free_list: Not Implemented Yet\n");
exit(-1);
/*
* Free the memory used by the linked list.
*/
void
free_list(Link_t *list)
{
Link_t *next;
while (next = list->next) {
free(list);
list = next;
}
}
void

Loading…
Cancel
Save