bool Sort::sort_by_time(int num_hops) { bool any_change = false; if(num_hops == route->get_current()->get_num_flights()) { any_change = true; } if(route->get_next()) { RouteList *old_head, *new_head, *cur_node, *max_node; long cur_max, temp_dur; //initializes both heads to new head old_head = route; new_head = route; //while the old head is not null while(old_head) { RouteListManipulator rlm; //initializes the current node and max node to the old head cur_node = old_head; max_node = old_head; //sets max to the current node's duration of travel cur_max = get_dur(cur_node->get_current()->get_flights()); //until it reaches the end of the list while(cur_node) { //gets the current nodes duration temp_dur = get_dur(cur_node->get_current()->get_flights()); //if the curent node's duration is greater than the max duration if(temp_dur >= cur_max && num_hops == cur_node->get_current()->get_num_flights()) { //sets max to current cur_max = temp_dur; //sets max node to current node max_node = cur_node; any_change = true; } //advances the node tracker cur_node = cur_node->get_next(); } if(!any_change) { return any_change; } //if the max node is the old head if(max_node == old_head) { //advnaces the old head old_head = old_head->get_next(); } //moves the max node to the head rlm.set_to_head(new_head, max_node); //sets the new head to max node new_head = max_node; } //changes route head to new head route = new_head; } return any_change; }
int main(int argc,char * argv []) { double *bbrk, in_dur; FILE *fp; int bbrksize, mode; if(argc==2 && (strcmp(argv[1],"--version") == 0)) { fprintf(stdout,"%s\n",cdp_version); fflush(stdout); return 0; } if(argc!=4) { fprintf(stdout,"ERROR: Bad function call.\n"); fflush(stdout); return(0); } if(sscanf(argv[2],"%d",&mode)!=1) { fprintf(stdout,"ERROR: Failed to read mode.\n"); fflush(stdout); return(0); } if(sscanf(argv[3],"%lf",&in_dur)!=1) { fprintf(stdout,"ERROR: Failed to read input file duration.\n"); fflush(stdout); return(0); } if((fp = fopen(argv[1],"r"))==NULL) { fprintf(stdout,"ERROR: Failed to open file %s\n",argv[1]); fflush(stdout); return(0); } if((bbrksize = read_brkdata(&bbrk,fp,mode))<=0) return(0); get_dur(bbrk,bbrksize,in_dur,mode); return(1); }