src/ManageCities.c
/*
 * FUNCTION NAME: removeCity
 *
 * DESCRIPTION:
 * Function does remove given city from a graph.
 */
FUNCTION NAME: removeCity
DESCRIPTION:
Function does remove given city from a graph.
FUNCTION NAME : removeCity DESCRIPTION : Function does remove given city from a graph .
void removeCity(Graph_t* graph, const char* city)
{
    AdjacencyList_t *upper, *previous_upper;
    AdjacencyListNode_t* lower;
    size_t how_many_connections = 0;
    char** connections_array = NULL;
    size_t i;
    previous_upper = graph->head;
    for (upper = graph->head; upper != NULL; upper = upper->next) {
        if (strcmp(upper->from, city) == 0) {
            for (lower = upper->head; lower != NULL; lower = lower->next) {
                ++how_many_connections;
            }
            connections_array = (char**)malloc(how_many_connections * sizeof(char*));
            checkMemory(connections_array);
            for (i = 0; i < how_many_connections; ++i) {
                connections_array[i] = (char*)malloc(G_CITYS_NAME_LENGTH * sizeof(char));
                checkMemory(connections_array[i]);
            }
            i = 0;
            for (lower = upper->head; lower != NULL; lower = lower->next) {
                strcpy(connections_array[i++], lower->to);
            }
            if (upper == graph->head) {
                graph->head = upper->next;
                removeAdjacencyListNodes(previous_upper->head);
                free(previous_upper->from);
                free(previous_upper);
            }
            else if (upper != graph->head) {
                previous_upper->next = upper->next;
                removeAdjacencyListNodes(upper->head);
                free(upper->from);
                free(upper);
            }
            break;
        }
        previous_upper = upper;
    }
    for (i = 0; i < how_many_connections; ++i) {
        removeConnection(graph, city, connections_array[i]);
    }
    for (i = 0; i < how_many_connections; ++i) {
        free(connections_array[i]);
    }
    free(connections_array);
}
void removeCity ( Graph_t * graph , const char * city ) { AdjacencyList_t * upper , * previous_upper ; AdjacencyListNode_t * lower ; size_t how_many_connections = 0 ; char * * connections_array = NULL ; size_t i ; previous_upper = graph -> head ; for ( upper = graph -> head ; upper != NULL ; upper = upper -> next ) { if ( strcmp ( upper -> from , city ) == 0 ) { for ( lower = upper -> head ; lower != NULL ; lower = lower -> next ) { ++ how_many_connections ; } connections_array = ( char * * ) malloc ( how_many_connections * sizeof ( char * ) ) ; checkMemory ( connections_array ) ; for ( i = 0 ; i < how_many_connections ; ++ i ) { connections_array [ i ] = ( char * ) malloc ( G_CITYS_NAME_LENGTH * sizeof ( char ) ) ; checkMemory ( connections_array [ i ] ) ; } i = 0 ; for ( lower = upper -> head ; lower != NULL ; lower = lower -> next ) { strcpy ( connections_array [ i ++ ] , lower -> to ) ; } if ( upper == graph -> head ) { graph -> head = upper -> next ; removeAdjacencyListNodes ( previous_upper -> head ) ; free ( previous_upper -> from ) ; free ( previous_upper ) ; } else if ( upper != graph -> head ) { previous_upper -> next = upper -> next ; removeAdjacencyListNodes ( upper -> head ) ; free ( upper -> from ) ; free ( upper ) ; } break ; } previous_upper = upper ; } for ( i = 0 ; i < how_many_connections ; ++ i ) { removeConnection ( graph , city , connections_array [ i ] ) ; } for ( i = 0 ; i < how_many_connections ; ++ i ) { free ( connections_array [ i ] ) ; } free ( connections_array ) ; }
FUNCTION NAME: removeCity
 DESCRIPTION:
 Function does remove given city from a graph.
FUNCTION NAME : removeCity DESCRIPTION : Function does remove given city from a graph .
/* Firstly, search for adjacency list that city creates. */ /* When you find it, count how many nodes (i.e. how many connections there
             * is between given city and other cities) are in its adjacency list. */ /* When you have done it, then create an array and copy connected cities
             * to created array. */ /* Now, 2 cases: if given city creates the first adjacency list in the graph... */ /* Remove adjacency list from the graph.  */ /* ... or it does not. */ /* Remove adjacency list from the graph. */ /* Now, remove every connection between given city cities
     * connected to it. */ /* And now free memory after array. */