I am working on an insertion sort that takes the nodes from one list and places them, sorted, into a new list. The problems seem to be coming into play in my For loop initialization, and the final list is never actually sorted correctly. When I try to pass the sortedlist to my print function, valgrind is showing an 'Invalid read of size 8'. Any help on making sure every node is being passed through and sorted correctly would be greatly appreciated.
1stTry-DLL.c
Code:#include <stdio.h> #include <stdlib.h> typedef struct DLNode Node; typedef struct DLNode { int ID; char *Fname; char *Lname; char *Dept; float GPA; Node * prev; Node * next; } Node; typedef struct DLList { int count; Node * first; Node * last; } List; void DLList_init(List * list) { list->first = NULL; list->last = NULL; list->count = 0; } void DLList_add_node(List * list, int ID, char *Fname, char *Lname, char *Dept, float GPA) { Node * newNode; newNode = malloc(sizeof(Node)); if(!newNode) { printf("Malloc failed, new node not created."); exit(EXIT_FAILURE); } newNode->ID = ID; newNode->Fname = Fname; newNode->Lname = Lname; newNode->Dept = Dept; newNode->GPA = GPA; if(list->last) { list->last->next = newNode; newNode->prev = list->last; list->last = newNode; } else { list->first = newNode; list->last = newNode; } list->count++; } void DLList_print(List * sortedlist, FILE *fout) { Node * node; for(node = sortedlist->first; node != NULL; node = node->next) { fprintf(fout, "%d,%s,%s,%s,%f\n", node->ID, node->Fname, node->Lname, node->Dept, node->GPA); free(node->Dept); free(node->Lname); free(node->Fname); free(node); } } void Insertion_sort(List * list, List * sortedlist, FILE *fout) { Node * indexNode; Node * nextNode; Node * firstNode; indexNode = list->first; nextNode = indexNode->next; for(indexNode = list->first; indexNode->next != NULL; indexNode = indexNode->next) { nextNode = indexNode->next; if(indexNode->ID < nextNode->ID) { if(sortedlist->first == NULL) { firstNode = indexNode; sortedlist->first = firstNode; sortedlist->last = firstNode; } else { firstNode->next = indexNode; indexNode->prev = firstNode; sortedlist->last = indexNode; } } else { nextNode->prev = indexNode; indexNode->next = nextNode; sortedlist->last = nextNode; } } DLList_print(sortedlist, fout); } int main(int argc, char *argv[]) { FILE *fin, *fout; int ID; char *Fname; char *Lname; char *Dept; float GPA; char *fin_path; char *fout_path; List sortedlist; DLList_init(& sortedlist); List list; //Create list DLList_init(& list); //and initialize NULL pointers if(argv[1] && argv[2]) { fin_path = argv[1]; fout_path = argv[2]; } else { printf("Input and output file names were not supplied!\n"); return 1; } fin = fopen(fin_path, "r"); if(fin == NULL) { printf("Can't open input file!\n"); return 1; } while (fscanf(fin, "%d %ms %ms %ms %f", &ID, &Fname, &Lname, &Dept, &GPA) == 5) { DLList_add_node(& list, ID, Fname, Lname, Dept, GPA); } fout = fopen(fout_path, "w"); if(fout == NULL) { printf("Can't open output file!"); return 1; } Insertion_sort(& list, & sortedlist, fout); //DLList_print(& list, fout); fclose(fin); fclose(fout); return 0; }