void nodeDestroy(Node * node) { if(node == NULL) return; nodeDestroy(node->next); edgelistDestroy(node->edges); free(node); }
void nodeDestroy(Node node) { int i; if (!node) return; for (i = 0; i < ALPHABET_SIZE; i++) { if (node->next[i]) nodeDestroy(node->next[i]); } free(node); }
int LLRemove(linkedListPtr list, char *token){ llnode *curr = list->head; llnode *prev = NULL; /*INFINITE LOOP*/ while (curr != NULL){ if(strcmp(curr->filename, token) == 0){ if(strcmp(curr->filename, list->head->filename) == 0){ list->head = curr->next; list->size--; nodeDestroy(curr); return 1; } else if(curr->next == NULL){ prev->next = NULL; nodeDestroy(curr); list->size--; return 1; } else{ prev->next = curr->next; nodeDestroy(curr); list->size--; return 1; } } prev = curr; curr = curr->next; } return 0; /* FAILED TO REMOVE */ }
///Removes all key and data elements from target map MapResult mapClear(Map map) { if (map == NULL) { return MAP_NULL_ARGUMENT; } NodeResult node_status; Node new_first_pointer= nodeDestroy(map->first_pointer, map->free_data_map, map->free_key_map,&node_status); map->first_pointer=new_first_pointer; if (node_status == NODE_NULL_PTR) { return MAP_NULL_ARGUMENT; } else { return MAP_SUCCESS; } }
void trieDestroy(Trie trie) { if (!trie) return; nodeDestroy(trie->root); free(trie); }
int main(int argc, char * * argv) { //Verify that the correct number of arguments is given if(argc != 3) FATAL("Invalid number of command line arguments!"); //Open the map file for reading FILE * map = fopen(argv[1], "r"); //Read the number of vertices in the file int numVertices = readNumberFromFile(map); //Read the number of edges in the file int numEdges = readNumberFromFile(map); //Read vertice data and assign it to nodal linked list "vertex" int nodenum = readNumberFromFile(map); int nodexcoord = readNumberFromFile(map); int nodeycoord = readNumberFromFile(map); Node * vertex = nodeConstructor(nodenum, nodexcoord, nodeycoord); int i; Node * tmpitr = vertex; for(i = 1; i < numVertices; ++i) { nodenum = readNumberFromFile(map); nodexcoord = readNumberFromFile(map); nodeycoord = readNumberFromFile(map); tmpitr->next = nodeConstructor(nodenum, nodexcoord, nodeycoord); tmpitr = tmpitr->next; } //Read edge data from the file and assign it to respective lists for(i = 0; i < numEdges; ++i) { int firstnode = readNumberFromFile(map); int secondnode = readNumberFromFile(map); insertConnection(vertex, firstnode, secondnode); } fclose(map); //NOTE: At this point, the graph is complete, so begin algorithm //Open the query file for reading FILE * query = fopen(argv[2], "r"); //Read the number of queries int numQueries = readNumberFromFile(query); //Read query data and store it for reference int * queryArr = readQueries(query, numQueries); //Store origin node in list Node * origin = vertex; //Loop through the queries for(i = 0; i < (numQueries * 2); i += 2) { //Find the starting point of the query int startVal = queryArr[i]; int endVal = queryArr[i+1]; //If they're the same, avoid trying to do anything else if(startVal == endVal) { printf("0\n"); printf("%d %d\n", startVal, endVal); } else { Node * start = findVertexByValue(startVal, origin); Node * end = findVertexByValue(endVal, origin); start->weight = 0; while(areAllNotRemoved(origin)) { //Set the weight check to find minimum node Node * itrNode = findLowestNode(origin); //Make sure we're not done if(itrNode->value == endVal) break; //Update weights for adjacent nodes updateAdjacentWeights(itrNode, origin); } if(end->weight == PSEUDOINF) FATAL("Final node weight was not updated... possibly unconnected?"); //Produce the result list ResultList * results = reslistConstructor(end->value, end->weight); Node * before = findVertexByValue(end->prev, origin); while(before->value != startVal) { ResultList * pinonfront = reslistConstructor(before->value, before->weight); pinonfront->next = results; results = pinonfront; before = findVertexByValue(before->prev, origin); } //Make sure the first number in the list is origin if(results->value != startVal) { ResultList * pinonfront2 = reslistConstructor(startVal, start->weight); pinonfront2->next = results; results = pinonfront2; } //Verify weights and assign the end weight //end->weight = verifyWeightsAndAssign(results, origin); //Print the final results printf("%ld\n", end->weight); ResultList * printitr = results; while(printitr != NULL) { printf("%d ", printitr->value); printitr = printitr->next; } printf("\n"); reslistDestroy(results); resetGraph(origin); } } //Free used memory fclose(query); free(queryArr); nodeDestroy(origin); return EXIT_SUCCESS; }