int main(int argc, char * * argv) { //Verify that the correct number of arguments is given if(argc != 2) 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); } //Print adjacency relationships tmpitr = vertex; EdgeList * itrEdge = NULL; while(tmpitr != NULL) { printf("%d: ", tmpitr->value); itrEdge = tmpitr->edges; while(itrEdge != NULL) { printf("%d ", itrEdge->connectedto); itrEdge = itrEdge->next; } printf("\n"); tmpitr = tmpitr->next; } return EXIT_SUCCESS; }
void fifoQueueEnqueue(FifoQueue *queue, PcbStr* pcb) { if (pcb == NULL) { printf("pcb is null"); return; } Node * newNode = nodeConstructor(); newNode->content = pcb; if (queue->head == NULL) { queue->head = newNode; queue->back = newNode; } else if (queue->head == queue->back) { nodeSetNext(queue->head, newNode); queue->back = newNode; } else { nodeSetNext(queue->back, newNode); queue->back = queue->back->next; } (queue->size)++; }
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; }