src/ListCreation.c
/*
 * FUNCTION NAME: addUniqueCitiesToTheList
 *
 * DESCRIPTION:
 * Unique cities from DataBase.txt file are being added to unique_cities list.
 */
FUNCTION NAME: addUniqueCitiesToTheList
DESCRIPTION:
Unique cities from DataBase.txt file are being added to unique_cities list.
FUNCTION NAME : addUniqueCitiesToTheList DESCRIPTION : Unique cities from DataBase . txt file are being added to unique_cities list .
void addUniqueCitiesToTheList(ConnectionsList_t* connections_list, size_t number_of_connections, UniqueCitiesList_t* unique_cities)
{
    size_t i, j;
    char** tab_all = malloc(2 * number_of_connections * sizeof(char*));
    checkMemory(tab_all);
    for (i = 0; i < 2 * number_of_connections; ++i) {
        tab_all[i] = (char*)malloc(G_CITYS_NAME_LENGTH * sizeof(char));
        checkMemory(tab_all[i]);
    }
    ConnectionsListNode_t* city_node;
    j = 0;
    for (city_node = connections_list->head; city_node != NULL; city_node = city_node->next) {
        strcpy(tab_all[j++], city_node->from);
        strcpy(tab_all[j++], city_node->to);
    }
    qsort(tab_all, 2 * number_of_connections, sizeof(char*), stringCmp);
    addUniqueCityToTheList(unique_cities, tab_all[0]);
    for (i = 0; i < 2 * number_of_connections - 1; ++i) {
        if (0 != strcmp(tab_all[i], tab_all[i + 1])) {
            addUniqueCityToTheList(unique_cities, tab_all[i + 1]);
        }
    }
    for (i = 0; i < 2 * number_of_connections; ++i) {
        free(tab_all[i]);
    }
    free(tab_all);
}
void addUniqueCitiesToTheList ( ConnectionsList_t * connections_list , size_t number_of_connections , UniqueCitiesList_t * unique_cities ) { size_t i , j ; char * * tab_all = malloc ( 2 * number_of_connections * sizeof ( char * ) ) ; checkMemory ( tab_all ) ; for ( i = 0 ; i < 2 * number_of_connections ; ++ i ) { tab_all [ i ] = ( char * ) malloc ( G_CITYS_NAME_LENGTH * sizeof ( char ) ) ; checkMemory ( tab_all [ i ] ) ; } ConnectionsListNode_t * city_node ; j = 0 ; for ( city_node = connections_list -> head ; city_node != NULL ; city_node = city_node -> next ) { strcpy ( tab_all [ j ++ ] , city_node -> from ) ; strcpy ( tab_all [ j ++ ] , city_node -> to ) ; } qsort ( tab_all , 2 * number_of_connections , sizeof ( char * ) , stringCmp ) ; addUniqueCityToTheList ( unique_cities , tab_all [ 0 ] ) ; for ( i = 0 ; i < 2 * number_of_connections - 1 ; ++ i ) { if ( 0 != strcmp ( tab_all [ i ] , tab_all [ i + 1 ] ) ) { addUniqueCityToTheList ( unique_cities , tab_all [ i + 1 ] ) ; } } for ( i = 0 ; i < 2 * number_of_connections ; ++ i ) { free ( tab_all [ i ] ) ; } free ( tab_all ) ; }
FUNCTION NAME: addUniqueCitiesToTheList
 DESCRIPTION:
 Unique cities from DataBase.txt file are being added to unique_cities list.
FUNCTION NAME : addUniqueCitiesToTheList DESCRIPTION : Unique cities from DataBase . txt file are being added to unique_cities list .
/* Variables used in loop. */ /* Since finding unique strings in a singly linked
     * list in a C language is a pain in the ass, let's make it
     * in a simpler way...
     * First, create arrays for cities from DataBase.txt file.
     * IF CITY'S NAME IS MULTIPLE TIMES IN A FILE, IT WILL
     * BE ADDED MULTIPLE TIMES TO THE ARRAY.
     * Dynamics arrays are needed, because strCmp function won't
     * work otherwise. */ /* Array that will contain name of every city
     * that is in DataBase.txt file. */ /* Copy cities' names to tab_all array. */ /* Now sort the array in descending alphabetical order.
     * The reason for descending order is the way how we add nodes
     * to the list - function addUniqueCityToTheList adds head to the list, not tail.
     * So the first element from tab_all will end as the last one
     * on the list.
     * Sorting is being done by built-in qsort function. */ /* Now add cities to the list.
     * First, add city from tab[0] and then search for
     * unique cities. If one is found, then add it to the list. */ /* Remove dynamic arrays from the memory. */