#include #include #include typedef struct Golfer { char fName[80]; char lName[80]; int handicap; } Golfer; typedef struct ListNode { Golfer data; struct ListNode *next; } ListNode; ListNode * findBestGolfer(ListNode *golfers); ListNode * deletePlayer(ListNode *golfers, ListNode *player); void printBackward(ListNode *golfers); void insertInOrder(ListNode **golfers, ListNode *temp); void printRoster(ListNode *golfers); //void sortPlayers(Golfer golfers[], int numGolfers); int main() { printf("Hello World!\n"); Golfer g; strcpy(g.fName,"Roger"); strcpy(g.lName,"Priebe"); g.handicap = 17; printf("%s, %s - %d\n",g.lName, g.fName, g.handicap); ListNode *head = NULL; ListNode *tail = NULL; FILE *fptr; fptr = fopen("golfers.dat", "r"); if (fptr == NULL) { printf("Cannot open file!\n"); exit(0); } else { printf("found it!\n"); char fName[80]; char lName[80]; int handicap; while (fscanf(fptr, "%s %s %d",fName, lName, &handicap) != EOF) { ListNode *temp = (ListNode *) malloc(sizeof(ListNode)); strcpy(temp->data.fName,fName); strcpy(temp->data.lName, lName); temp->data.handicap = handicap; temp->next = NULL; insertInOrder(&head, temp); /* //O(1) add at end if (head == NULL) { head = temp; tail = temp; } else { tail->next = temp; tail = temp; } //O(n) solution to adding at end if (head == NULL){ //list is empty? head = temp; } else { ListNode *p = head; while (p->next != NULL) { p = p->next; } p->next = temp; } */ } printBackward(head); printf("---------------------\n"); printRoster(head); ListNode *bestGolfer = findBestGolfer(head); printf("the best golfer is %s\n", bestGolfer->data.fName); head = deletePlayer(head, bestGolfer); printRoster(head); printf("----------------------------\n"); head = deletePlayer(head, head); printRoster(head); //WARNING MEMORY LEAK head = NULL; head = deletePlayer(head, head); printRoster(head); //sortPlayers(golfers, numGolfers); //printRoster(golfers, numGolfers); } } void printBackward(ListNode *golfers) { if (golfers != NULL) { printBackward(golfers->next); printf("%s, %s - %d\n", golfers->data.lName, golfers->data.fName, golfers->data.handicap); } } //does not account for the tail pointer void insertInOrder(ListNode **golfers, ListNode *temp) { ListNode *p = *golfers; ListNode *prev = NULL; if (*golfers == NULL) { *golfers = temp; } else { while ((p != NULL) && (p->data.handicap < temp->data.handicap)) { prev = p; p = p->next; } if (prev == NULL) { temp->next = *golfers; *golfers = temp; } else { temp->next = p; prev->next = temp; } } } ListNode * deletePlayer(ListNode *golfers, ListNode *player) { if (golfers == NULL) { return NULL; } else { ListNode *p = golfers; ListNode *prev = NULL; while((p != NULL) && (p != player)) { prev = p; p = p->next; } if (prev == NULL) { //removing first node in list golfers = p->next; free(p); } else if (p == player) { prev->next = p->next; free(p); } } return golfers; } void printRoster(ListNode *golfers) { for (ListNode *p = golfers; p != NULL; p = p->next) { printf("%s, %s - %d\n", p->data.lName, p->data.fName, p->data.handicap); } } ListNode * findBestGolfer(ListNode *golfers) { if (golfers == NULL) { return NULL; } else { ListNode *best = golfers; ListNode *p = golfers->next; while (p != NULL) { if (p->data.handicap < best->data.handicap) { best = p; } p = p->next; } return best; } }