Advertisement
Motocelium

Joc de carti

Nov 14th, 2024 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. // main.c
  2.  
  3. #include "functii.h"
  4.  
  5. int main(){
  6.     int nr_jucatori;
  7.     scanf("%d", &nr_jucatori);
  8.     NOD *jucator;
  9.    
  10.     for(int i=0; i<nr_jucatori; i++){
  11.         jucator = creare_nod(jucator);
  12.     }
  13.     afisare(jucator);
  14.    
  15.     while(jucator->next){
  16.         jucator = citire_carti(jucator);
  17.         jucator = eliminare_jucator(jucator);
  18.         afisare(jucator);
  19.     }
  20.     return 0;  
  21. }
  22.  
  23. // functii.h
  24.  
  25. #include "functii.h"
  26.  
  27. NOD * creare_nod(NOD *head){
  28.     NOD *nou = malloc(sizeof(NOD));
  29.    
  30.     scanf("%s", nou->nume);
  31.     nou->next = NULL;
  32.    
  33.     if(head == NULL)
  34.         head = nou;
  35.     else{
  36.         NOD *curent = head;
  37.         for( ; curent->next; curent=curent->next);
  38.         curent->next = nou;
  39.     }
  40.     return head;
  41. }
  42.  
  43. NOD *citire_carti(NOD *head){
  44.     NOD *curent = head;
  45.     for( ; curent!=NULL; curent=curent->next)
  46.         scanf("%d", &curent->carte);
  47.     return head;
  48. }
  49.  
  50. NOD *stergere_nod(NOD *head, int poz){
  51.     NOD *nod = head;
  52.     if(poz == 0){
  53.         head = head->next;
  54.         free(nod);
  55.     }
  56.     else{
  57.         for(int i = 0; i<poz-1; i++) nod = nod->next;
  58.         NOD *nod_sters = nod->next;
  59.         nod -> next = nod -> next -> next;
  60.         free(nod_sters);
  61.     }
  62.     return head;
  63. }
  64.  
  65. NOD *eliminare_jucator(NOD *head){
  66.     int poz = 0, mini = head->carte;
  67.     NOD *nod = head ->next;
  68.     for(int i = 1; nod; nod = nod->next, i++)
  69.         if(mini > nod->carte)
  70.             mini = nod->carte, poz = i;
  71.     return stergere_nod(head, poz);
  72. }
  73.  
  74. void afisare(NOD *head){
  75.     NOD *nod = head;
  76.    
  77.     for(; nod; nod=nod->next)
  78.         printf("%s\n", nod->nume);
  79. }
  80.  
  81. //functii.c
  82.  
  83. #include "functii.h"
  84.  
  85. NOD * creare_nod(NOD *head){
  86.     NOD *nou = malloc(sizeof(NOD));
  87.    
  88.     scanf("%s", nou->nume);
  89.     nou->next = NULL;
  90.    
  91.     if(head == NULL)
  92.         head = nou;
  93.     else{
  94.         NOD *curent = head;
  95.         for( ; curent->next; curent=curent->next);
  96.         curent->next = nou;
  97.     }
  98.     return head;
  99. }
  100.  
  101. NOD *citire_carti(NOD *head){
  102.     NOD *curent = head;
  103.     for( ; curent!=NULL; curent=curent->next)
  104.         scanf("%d", &curent->carte);
  105.     return head;
  106. }
  107.  
  108. NOD *stergere_nod(NOD *head, int poz){
  109.     NOD *nod = head;
  110.     if(poz == 0){
  111.         head = head->next;
  112.         free(nod);
  113.     }
  114.     else{
  115.         for(int i = 0; i<poz-1; i++) nod = nod->next;
  116.         NOD *nod_sters = nod->next;
  117.         nod -> next = nod -> next -> next;
  118.         free(nod_sters);
  119.     }
  120.     return head;
  121. }
  122.  
  123. NOD *eliminare_jucator(NOD *head){
  124.     int poz = 0, mini = head->carte;
  125.     NOD *nod = head ->next;
  126.     for(int i = 1; nod; nod = nod->next, i++)
  127.         if(mini > nod->carte)
  128.             mini = nod->carte, poz = i;
  129.     return stergere_nod(head, poz);
  130. }
  131.  
  132. void afisare(NOD *head){
  133.     NOD *nod = head;
  134.    
  135.     for(; nod; nod=nod->next)
  136.         printf("%s\n", nod->nume);
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement