#include #include #include #define MAX_LENGTH 255 #define MAX_RUNDA 20 typedef struct Igrac { char *imePrezime; int id; char *drzava; int runda; int *izabraniBrojevi; } Igrac; struct ListNode { Igrac *igrac; struct ListNode *next; }; void printList(struct ListNode *head, int runda) { printf("--------------------------------------\n"); if(head == NULL) { printf("Nema elemenata\n"); return; } struct ListNode *pom = head; while(pom) { printf("%d | %s | %s | %d |", pom -> igrac -> id, pom -> igrac -> imePrezime, pom -> igrac -> drzava, pom -> igrac -> runda); printf("Brojevi: "); for(int i = 0; i < runda && i < MAX_RUNDA; i++) { printf("%d ", pom -> igrac -> izabraniBrojevi[i]); } printf("\n"); pom = pom -> next; } printf("--------------------------------------\n"); } void freeList(struct ListNode *head) { struct ListNode *pom = head; while (pom) { struct ListNode *temp = pom; pom = pom -> next; free(temp -> igrac -> imePrezime); free(temp -> igrac -> drzava); free(temp -> igrac -> izabraniBrojevi); free(temp -> igrac); free(temp); } } struct ListNode *createNode(Igrac *igrac) { struct ListNode *novi = (struct ListNode*)malloc(sizeof(struct ListNode)); if(novi == NULL) { printf("Greska prilikom alokacije memorije\n"); exit(EXIT_FAILURE); } novi -> igrac = igrac; novi -> next = NULL; return novi; } void addNode(struct ListNode **head, struct ListNode *newNode) { if(*head == NULL) { *head = newNode; return; } struct ListNode *pom = *head; while(pom -> next) { pom = pom -> next; } pom -> next = newNode; } struct ListNode *createList() { struct ListNode *head = NULL; int n, broj; char linija[MAX_LENGTH]; scanf("%d", &n); for(int i = 0; i < n; i++) { Igrac *igrac = (Igrac*)malloc(sizeof(Igrac)); if (igrac == NULL) { printf("Greska prilikom alokacije memorije\n"); exit(EXIT_FAILURE); } // provera alokacije igrac -> id = i+1; igrac -> runda = 1; fgetc(stdin); fgets(linija, MAX_LENGTH, stdin); if(linija[strlen(linija)-1] == '\n') { linija[strlen(linija)-1] = '\0'; } igrac -> imePrezime = (char*)malloc((strlen(linija)+1) * sizeof(char)); if(igrac -> imePrezime == NULL) { printf("Greska prilikom alokacije memorije\n"); exit(EXIT_FAILURE); } strcpy(igrac -> imePrezime, linija); fgets(linija, MAX_LENGTH, stdin); if(linija[strlen(linija)-1] == '\n') { linija[strlen(linija)-1] = '\0'; } igrac -> drzava = (char*)malloc((strlen(linija)+1) * sizeof(char)); if(igrac -> drzava == NULL) { printf("Greska prilikom alokacije memorije\n"); exit(EXIT_FAILURE); } strcpy(igrac -> drzava, linija); igrac -> izabraniBrojevi = (int*)calloc(MAX_RUNDA, sizeof(int)); if(igrac -> izabraniBrojevi == NULL) { printf("Greska prilikom alokacije memorije\n"); exit(EXIT_FAILURE); } scanf("%d", &broj); igrac -> izabraniBrojevi[0] = broj; // dodaj u listu addNode(&head, createNode(igrac)); } return head; } int brojIgraca(struct ListNode *head) { int count = 0; struct ListNode *pom = head; while(pom) { count++; pom = pom -> next; } return count; } // proverava da li postoje duplikati izabranih brojeva u rundi 'runda' // i uklanja duplikate void proveriBrojeveURundi(struct ListNode **head, int runda) { struct ListNode *pom = *head; struct ListNode *pomPrev = NULL; while (pom) { int broj = pom -> igrac -> izabraniBrojevi[runda-1]; int imaDuplikata = 0; struct ListNode *pom1 = pom -> next; struct ListNode *pom1Prev = pom; while(pom1) { struct ListNode *naredni = pom1 ->next; if (pom1 -> igrac -> izabraniBrojevi[runda-1] == broj) { pom1Prev -> next = naredni; struct ListNode *temp = pom1; free(temp -> igrac -> imePrezime); free(temp -> igrac -> drzava); free(temp -> igrac -> izabraniBrojevi); free(temp -> igrac); free(temp); imaDuplikata = 1; } else { pom1Prev = pom1; } pom1 = naredni; } if(imaDuplikata) { if(pomPrev == NULL) { *head = pom -> next; } else { pomPrev -> next = pom -> next; } struct ListNode *temp = pom; pom = pom -> next; free(temp -> igrac -> imePrezime); free(temp -> igrac -> drzava); free(temp -> igrac -> izabraniBrojevi); free(temp -> igrac); free(temp); } else { pomPrev = pom; pom = pom -> next; } } } void azuriraj(struct ListNode *head, int runda) { struct ListNode *temp = head; int broj; while(temp) { scanf("%d", &broj); temp -> igrac -> runda = runda; temp -> igrac -> izabraniBrojevi[runda-1] = broj; temp = temp -> next; } } void igraj(struct ListNode **head) { int runda = 1; while(brojIgraca(*head) > 3) { proveriBrojeveURundi(head, runda); runda++; azuriraj(*head, runda); printList(*head, runda); } } int main(int argc, char **argv) { struct ListNode *head = createList(); printList(head, 1); igraj(&head); printf("POBEDNICI:\n"); printList(head, 3); freeList(head); return 0; }