Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //Structura pentru jucator:
- typedef struct Player
- {
- char *last_name;
- char *first_name;
- int score;
- } Player;
- //Structura pentru tara:
- typedef struct Country
- {
- char *name;
- int nr_players;
- int global_score;
- Player *players;
- } Country;
- struct Elem
- {
- Country Countries;
- struct Elem *next, *prev;
- };
- typedef struct Elem Node;
- void addAtBeginning(Node **head, Country aux)
- {
- Node *newNode = (Node *)malloc(sizeof(Node));
- newNode->Countries = aux;
- newNode->next = newNode;
- newNode->prev = newNode;
- *head = newNode;
- }
- void iend(Node **head, Country aux)
- {
- if (*head == NULL)
- {
- addAtBeginning(&*head, aux);
- return;
- }
- Node *headcopy = *head;
- Node *newNode = (Node *)malloc(sizeof(Node));
- while (headcopy->next != *head)
- headcopy = headcopy->next;
- headcopy->next=newNode;
- newNode->prev=headcopy;
- newNode->Countries=aux;
- newNode->next=*head;
- (*head)->prev=newNode;
- }
- Node *write_list(int n_countries)
- {
- char s[20];
- Node *head=NULL;
- for (int i = 0; i < n_countries; i++)
- {
- Country aux;
- scanf("%d", &aux.nr_players);
- aux.players = malloc(sizeof(Player) * aux.nr_players);
- scanf("%s", s);
- aux.name = malloc((strlen(s) + 1) * sizeof(char));
- strcpy(aux.name, s);
- for (int j = 0; j < aux.nr_players; j++)
- {
- scanf("%s", s);
- aux.players[j].last_name = malloc((strlen(s) + 1) * sizeof(char));
- strcpy(aux.players[j].last_name, s);
- scanf("%s", s);
- aux.players[j].first_name = malloc((strlen(s) + 1) * sizeof(char));
- strcpy(aux.players[j].first_name, s);
- scanf("%d", &aux.players[j].score);
- }
- iend(&head, aux);
- }
- //fac o structura tara noua
- Country sentinel;
- //pun nr de playeri =0 ca sa am un element cu care sa pot identifica santinela
- sentinel.nr_players=0;
- //o adaug in lista
- iend(&head,sentinel);
- return head;
- }
- /*void print(Node *head)
- {
- Node *headcopy = head;
- while (headcopy->next != head)
- {
- printf("%s\n", headcopy->Countries.name);
- for (int i = 0; i < headcopy->Countries.nr_players; i++)
- printf("%s %s %d\n", headcopy->Countries.players[i].last_name, headcopy->Countries.players[i].first_name, headcopy->Countries.players[i].score);
- headcopy = headcopy->next;
- }
- printf("%s\n", headcopy->Countries.name);
- for (int i = 0; i < headcopy->Countries.nr_players; i++)
- printf("%s %s %d\n", headcopy->Countries.players[i].last_name, headcopy->Countries.players[i].first_name, headcopy->Countries.players[i].score);
- }*/
- int eliminare_tari(int n_countries)
- {
- int nr=1;
- while(nr<=n_countries)
- nr=nr*2;
- nr=nr/2;
- return nr;
- // printf("Se vor elimina %d tari.\n",n_countries-nr);
- //*n_countries_list=nr;
- }
- void print(Node *head)
- {
- Node *headcopy = head;
- while (headcopy->Countries.nr_players != 0)
- {
- printf("%s\n", headcopy->Countries.name);
- for (int i = 0; i < headcopy->Countries.nr_players; i++)
- printf("%s %s %d\n", headcopy->Countries.players[i].last_name, headcopy->Countries.players[i].first_name, headcopy->Countries.players[i].score);
- headcopy = headcopy->next;
- }
- }
- float Scor_initial(Node **head)
- {
- Node *headcopy=*head;
- // while(headcopy->Countries.nr_players!=0)
- {
- int s=0;
- float initial_score;
- for(int i=0;i<headcopy->Countries.nr_players;i++)
- s=s+headcopy->Countries.players[i].score;
- initial_score=s/headcopy->Countries.nr_players;
- return initial_score;
- }
- }
- void delete(Node**head, Country aux) /*val este valoarea stocata in nodul de sters*/
- {
- if (*head==NULL) return;
- if (*head==(*head)->next && (*head)->val==val) /*acesta e singurul nod din lista*/
- {
- free(*head);
- *head=NULL; /*lista e acum goala*/
- return;
- }
- Node *headcopy=(*head)->next;
- Node *aux=*head;
- while (headcopy!=*head) /*daca acesta nu e βprimulβ element*/
- {
- if (headcopy->val!=val)
- {
- aux=headcopy;
- headcopy=headcopy->next;
- }
- else
- {
- aux->next=headcopy->next;
- free(headcopy);
- return;
- }
- }
- void delete_minim(Node **head,int *n_countries)
- {
- float min_scor_initial=Scor_initial(&*head);
- Node *headcopy=*head;
- while((*n_countries)>eliminare_tari(n_countries))
- {
- while(headcopy->Countries.nr_players!=0)
- {
- if (min_scor_initial>Scor_initial(&*head)) min_scor_initial=Scor_initial(&*head);
- headcopy=headcopy->next;
- }
- while(headcopy->Countries.nr_players!=0)
- if(Scor_initial(&*head)==min_scor_initial) //sterge head;
- (*n_countries)--;
- }
- }
- int main()
- {
- Node *head;
- int n_countries;
- scanf("%d", &n_countries);
- head = write_list(n_countries);
- printf("Nr tari:%d\n", n_countries);
- print(head);
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement