#include #include #include typedef struct Golfer { char fname[80]; char lname[80]; int handicap; } Golfer; typedef struct ListNode { struct Golfer player; struct ListNode *next; } ListNode; //int findBestGolfer(Golfer golfers[], int numGolfers); ListNode * findBestGolfer(ListNode * head); 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"); char buf[BUFSIZ]; if (fptr == NULL) { printf("Cannot open file!\n"); exit(0); } else { printf("found it!\n"); char fname[25]; char lname[25]; int handicap; while (fscanf(fptr, "%s %s %d", fname, lname, &handicap) != EOF) { printf("ppp %s\n", fname); ListNode *temp = (ListNode *) malloc(sizeof(ListNode)); temp->next = NULL; strcpy(temp->player.fname,fname); strcpy(temp->player.lname,lname); temp->player.handicap = handicap; /* O(n) insert at end if (head == NULL) { //list is empty head = temp; } else { ListNode *p = head; ListNode *trail = NULL; while (p != NULL) { trail = p; p = p->next; } trail->next = temp; */ if (head == NULL) { //list is empty head = temp; tail = temp; } else { tail->next = temp; tail = temp; } } } ListNode *p = head; while(p != NULL) { printf("%s %s %d\n",p->player.fname, p->player.lname, p->player.handicap); p = p->next; } p = findBestGolfer(head); printf("best is %s\n", p->player.fname); /* for (int i = 0; i < numGolfers; i++) printf("%s, %s - %d\n",golfers[i].lname, golfers[i].fname, golfers[i].handicap); int best = findBestGolfer(golfers, numGolfers); printf("the best is %s %s\n", golfers[best].fname, golfers[best].lname); */ } ListNode * findBestGolfer(ListNode * head) { if (head == NULL) return NULL; else { ListNode *best = head; ListNode *p = best->next; while (p != NULL) { if (best->player.handicap > p->player.handicap) best = p; p = p->next; } return best; } } /* int findBestGolfer(Golfer golfers[], int numGolfers) { int best = 0; for (int i = 0; i < numGolfers; i++) { if (golfers[i].handicap < golfers[best].handicap) best = i; } return best; } */