// golfScores.c : Questo file contiene la funzione 'main', in cui inizia e termina l'esecuzione del programma. // #include #include #include /* Maximum length of names */ #define STRMAX 20 struct dnode { int score; char player[STRMAX+1]; struct dnode* prev; struct dnode* next; }; struct dnode* first; /* pointer to the first list item */ struct dnode* last; /* pointer to the last list item */ struct dnode* createNode(char* playerName, int playerScore) { struct dnode* newNode = (struct dnode*)malloc(sizeof(struct dnode)); if (newNode != NULL) { strcpy(newNode->player, playerName); newNode->score = playerScore; newNode->next = NULL; newNode->prev = NULL; } return(newNode); } void printList(struct dnode* head) { struct dnode* current = head; while (current != NULL) { printf("%s ", current->player); printf("%d\n", current->score); current = current->next; } printf("\n"); } void printPlayersSameScore(struct dnode* pnode) { int playerScore = 0; struct dnode* current = pnode; if (pnode != NULL) { playerScore = pnode->score; } while (current != NULL && current->score == playerScore) { printf("%s ", current->player); printf("%d\n", current->score); current = current->next; } current = pnode->prev; while (current != NULL && current->score == playerScore) { printf("%s ", current->player); printf("%d\n", current->score); current = current->prev; } printf("\n"); } struct dnode* searchItem(struct dnode* head, char* playerToSearch) { while ((head != NULL) && (strcmp(head->player,playerToSearch)!=0)) head = head->next; return(head); } void head_insert(struct dnode** ptr, struct dnode* newNode) { struct dnode* tmp = NULL; tmp = *ptr; *ptr = newNode; if (tmp != NULL) { tmp->prev = newNode; (*ptr)->next = tmp; } else { (*ptr)->next = NULL; (*ptr)->prev = NULL; } } void tail_insert(struct dnode** ptr, struct dnode* newNode) { struct dnode* tmp = NULL; if (*ptr == NULL) { *ptr = newNode; (*ptr)->next = NULL; (*ptr)->prev = NULL; } else { tmp = *ptr; while (tmp->next != NULL) tmp = tmp->next; tmp->next = newNode; newNode->prev = tmp; newNode->next = NULL; } } void insert_x_score(struct dnode** ptr, struct dnode* newNode) { struct dnode* tmp = NULL; if (*ptr == NULL) { *ptr = newNode; (*ptr)->next = NULL; (*ptr)->prev = NULL; } else { if ((*ptr)->score >= newNode->score) { newNode->next = *ptr; newNode->prev = NULL; (*ptr)->prev = newNode; *ptr = newNode; } else { tmp = *ptr; while (tmp->next != NULL) { if ((tmp->score <= newNode->score) && (tmp->next->score >= newNode->score)) { newNode->next = tmp->next; tmp->next->prev = newNode; tmp->next = newNode; newNode->prev = tmp; break; } else { tmp = tmp->next; } } if (tmp->next == NULL) { tmp->next = newNode; newNode->next = NULL; newNode->prev = tmp; } } } } 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 dnode* headList = NULL; struct dnode* headListTail = NULL; struct dnode* headListScore = NULL; for (int i = 0; i < sizeof(golfScores) / sizeof(golfScores[0]); i++) { char player[STRMAX+1]; int score; char row[4 * STRMAX]; char* t; strcpy(row, golfScores[i]); if ((t = strtok(row, ";")) != NULL) { strcpy(player, t); if ((t = strtok(NULL, ";")) != NULL) { score = atoi(t); } } else { printf("Error reading tokens from row"); return(1); } //printf("Player %s - score %d\n", player, score); struct dnode* newNode = createNode(player, score); if (newNode != NULL) { //head_insert(&headList, newNode); //tail_insert(&headListTail, newNode); insert_x_score(&headListScore, newNode); } else { printf("Error creating newNode"); return(2); } } printList(headListScore); printf("\nInsert name to be searched:"); char playerName[STRMAX + 1]; if (gets_s(playerName, STRMAX) == NULL) { printf("Error reading player name\n"); return(1); } else { //playerName[strlen(playerName)] = '\0'; printf("Player to be searched %s\n", playerName); } struct dnode* playerFound = searchItem(headListScore, playerName); if (playerFound != NULL) { printPlayersSameScore(playerFound); } }