I have this code:
Code:typedef struct _node{ double total; struct _node *next; struct _node *prev; } NODE; NODE delete_current(NODE *current) { current->prev->next = current->next; current->next->prev = current->prev; free(current); } NODE sort(NODE *new_list, NODE *old_list) { NODE *current = (NODE *)malloc(sizeof(NODE)); NODE *max = (NODE *)malloc(sizeof(NODE)); while(old_list != NULL) { max = old_list->next; for(current = old_list->next; current != old_list; old_list= old_list->next) { if(max->total < old_list->total) { max = old_list; } } new_list = new_list->next; new_list = (NODE *)malloc(sizeof(NODE)); new_list->total = max->total; delete_current(old_list); } } NODE add(NODE *sent, double i) { NODE *new_node; new_node = (NODE *)malloc(sizeof(NODE)); sent->prev->next = new_node; new_node->prev = sent->prev; new_node->next = sent; sent->prev = new_node; new_node->total = i; } NODE print_list(NODE *list) { NODE *current; for(current = list->next; current != list; current = current->next){ printf("%lf\n",current->total); } } int main(int argc, char **argv) { NODE *new_list = (NODE *)malloc(sizeof(NODE)); NODE *old_list = (NODE *)malloc(sizeof(NODE)); double i; old_list->next = old_list; old_list->prev = old_list; for( i =1; i<=10 ; i++) { add(old_list, i); } print_list(old_list); sort(new_list, old_list); print_list(new_list); }
Im having trouble on my sort() function.. I know print_list works and add works..
The whole for loop setting the total in each node is just me making a test list.. its not my actual list.. The sort function is supposed to be sorting the list from greatest to least.



LinkBack URL
About LinkBacks




.. but now im in an infinite loop? help