#include #include #include typedef struct Golfer { char fName[80]; char lName[80]; int handicap; } Golfer; typedef struct ListNode { Golfer data; struct ListNode *next; } ListNode; void printRoster(ListNode *golfers); void printBackwards(ListNode *golfers); ListNode * findBestGolfer(ListNode *golfers); //return the head of the list ListNode * deleteGolfer(ListNode *target, ListNode *head, ListNode *tail); void insertInOrder(ListNode **start, ListNode **tail, ListNode *golfer); int main (int argc, char *argv[]) { Golfer g; strcpy(g.fName, "Roger"); strcpy(g.lName, "Priebe"); g.handicap = 18; printf("not in data%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("not open!!\n"); exit(-1); } else { char fName[80]; char lName[80]; int handicap; while (fscanf(fptr, "%s %s %d", fName, lName, &handicap) != EOF) { //create new node and set up to be insterted later 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, &tail, temp); //insert at head //temp->next = head; //head = temp; /* //insert at end O(1) because of tail pointer if (head == NULL) { head = temp; tail = temp; } else { tail->next = temp; tail = temp; } */ } } printRoster(head); printf("\n\n"); printBackwards(head); ListNode *best = findBestGolfer(head); printf("The best golfer is %s.\n", best->data.fName); printf("delete best\n\n"); head = deleteGolfer(best, head, tail); printRoster(head); printf("delete first\n\n"); head = deleteGolfer(head, head, tail); printRoster(head); printf("delete last\n\n"); head = deleteGolfer(tail, head, tail); printRoster(head); return 0; } 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; } } //return the head of the list ListNode * deleteGolfer(ListNode *target, ListNode *head, ListNode *tail) { ListNode *p = head; if (head == NULL) { //empty list return NULL; } else if (head == target) { //remove first node if (head == tail) { //only one element head = NULL; tail = NULL; } else { head = p->next; } } else { ListNode *prev = NULL; while (p != target) { prev = p; p = p->next; } printf("prev %d, p %d\n", prev->data.handicap, p->data.handicap); if (tail == target) { tail = prev; printf("delete at tail\n"); } prev->next = p->next; } free(target); //or free(p); return head; } void insertInOrder(ListNode **start, ListNode **tail, ListNode *golfer) { ListNode *p = *start; ListNode *prev = NULL; if (p == NULL) { //list is empty *start = golfer; *tail = golfer; } else { while ((p != NULL) && (p->data.handicap < golfer->data.handicap)) { prev = p; p = p->next; } if (prev == NULL) { //inserting at head of list golfer->next = p; *start = golfer; } else { golfer->next = p; prev->next = golfer; if (*tail == prev) { *tail = golfer; } } } } void printBackwards(ListNode *golfers) { if (golfers != NULL) { printBackwards(golfers->next); printf("BACK %s %s - %d\n", golfers->data.fName, golfers->data.lName, golfers->data.handicap); } }