#include #include #include #include #define MAX_NAME 30 struct GolfList{ struct GolfList* prev; char name[MAX_NAME+1]; int score; struct GolfList* next; }; struct GolfList* createNode(char* newName, int newScore){ struct GolfList* newNode = (struct GolfList*)malloc(sizeof(struct GolfList)); if(newNode != NULL){ strcpy(newNode->name, newName); newNode->score = newScore; newNode->prev = NULL; newNode->next = NULL; } return newNode; } void insertDescendingOrder(struct GolfList** head, struct GolfList** tail, struct GolfList* newNode){ // if empty list if(*head == NULL){ *head = newNode; *tail = newNode; return; } // if need to add before the head if(newNode->score > (*head)->score){ newNode->next = *head; (*head)->prev = newNode; *head = newNode; return; } // if need to add after the last if( (*tail)->score > newNode->score){ newNode->prev = *tail; (*tail)->next = newNode; *tail = newNode; return; } struct GolfList* tmp = *head; while(tmp->next != NULL){ if(tmp->next->score < newNode->score){ newNode->next = tmp->next; newNode->prev = tmp; tmp->next->prev = newNode; tmp->next = newNode; break; } else{ tmp = tmp->next; } } //end of the while } void printDescendingList(struct GolfList* head){ struct GolfList* current = head; while(current != NULL){ printf("%s ", current->name); printf("%d\n", current->score); current = current->next; } printf("\n"); } void printAscendingList(struct GolfList* tail){ struct GolfList* current = tail; while(current != NULL){ printf("%s ", current->name); printf("%d\n", current->score); current = current->prev; } printf("\n"); } void printSameScore(struct GolfList* head, char* playerName){ struct GolfList* current = head; //find the player in the list while( strcmp(current->name, playerName) != 0){ current = current->next; //if the player is not in the list if(current == NULL){ printf("Player not found"); return; } } //return to the first player with the same score struct GolfList* tmp = current; while(current->score == tmp->score){ current = current->prev; } current = current->next; //print all the players with the same score while(current->score == tmp->score){ printf("%s ", current->name); printf("%d\n", current->score); current = current->next; } printf("\n"); } int main(){ const char* golfScores[] = { "Albert Einstein;121", "John Ford;230", "Hakito Honda;140", "William Chevrolet;136", "Harry Volkswagen;115", "Gilles Nissan;149", "Cristiano Ronaldo;121", "Barack Obama;130", "George Porsche;142", "Juan Tesla;151", "Satoko Hyundai;123", "Nagamoto Subaru;121", "Elodie Kia;136", "Henry Lexus;129", "Sabine Porsche;146", "Benny Jaguar;121", "Margareth Land-Rover;136", "Klaus Volvo;132", "Satko Mazda;121", "Lapo Fiat;136" }; struct GolfList* head = NULL; struct GolfList* tail = NULL; for(int i=0; i<(sizeof(golfScores)/sizeof(golfScores[0])); i++){ char name[MAX_NAME+1]; int score; char temp[MAX_NAME]; strcpy(name, strtok(golfScores[i], ";")); if(name == NULL){ printf("Error reading tokens from row"); system("pause"); return 1; } strcpy(temp, strtok(NULL," ")); if(temp == NULL){ printf("Error reading tokens from row"); system("pause"); return 1; } else{ score = atoi(temp); } struct GolfList* newNode = createNode(name, score); if(newNode != NULL){ insertDescendingOrder(&head, &tail, newNode); } else{ printf("Error creating newNode"); return 2; } } //end for printf("\nList in ascending order\n"); printAscendingList(tail); printf("\nList in descending order\n"); printDescendingList(head); char person[] = "Nagamoto Subaru"; printf("\nList of all the player that scored the same points of %s\n", person); printSameScore(head, person); system("pause"); }