Advertisement
Guest User

Listy etc

a guest
Jan 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //1.usuwa srodek listy jesli nie parzysta
  4. //2. liczy srednia elementow dodatnich listy
  5. //3. funkcja przyjmujaca 2 listy, wyswietlajaca ich sume??
  6. //4. rekurencyjnie z 10 na 6 i na 7
  7. //5. najwuieksza wartosc niepierwsza z przedzialu, reku.
  8. typedef struct Node* lista;
  9. struct Node {
  10.     int wartosc;
  11.     lista *next;
  12.  
  13. };
  14.  
  15. void pokazListe(lista start){
  16.     lista wypisz = start;
  17.     do{
  18.         printf("Wezel nr: %d\n", wypisz->wartosc);
  19.         wypisz=wypisz->next;
  20.     }
  21.     while(wypisz!=NULL);
  22.  
  23. }
  24. int policz(lista start){
  25.         lista temp=start;
  26.         int zlicz=0;
  27.         while(temp!=NULL)
  28.         {
  29.             temp=temp->next;
  30.             zlicz++;
  31.         }
  32.         return zlicz;
  33. }
  34. void deleteMid(lista start){
  35.         lista temp=start;
  36.         lista przechowaj;
  37.         if((policz(temp)%2)==0)
  38.         {
  39.             printf("Lista jest parzysta");
  40.         }
  41.         else
  42.         {
  43.             for(int i=0;i<policz(temp)/2;i++)
  44.             {
  45.                 przechowaj=temp;
  46.                 temp = temp->next;
  47.  
  48.  
  49.             }
  50.             przechowaj->next = temp->next;
  51.             free(temp);
  52.             temp=NULL;
  53.  
  54.         }
  55.  
  56.  
  57. }
  58.  
  59. void srednia(lista start){
  60.     lista temp=start;
  61.     int suma=0,zlicz=0;
  62.  
  63.     while(temp!=NULL){
  64.         if(temp->wartosc>0)
  65.         {
  66.         suma+=temp->wartosc;
  67.         zlicz++;
  68.         }
  69.         temp=temp->next;
  70.     }
  71.     float srednia=(float)suma/(float)zlicz;
  72.     printf("\nSrednia listy: %f", srednia);
  73. }
  74. /*
  75. ŹLE NAPISANE NIE DZIALA
  76. lista suma(lista pierwsza, lista druga)
  77. {
  78.     lista temp1=pierwsza;
  79.     lista temp2=druga;
  80.     for(int i=0;i<policz(temp1);i++)
  81.     {
  82.         for(int j=0;j<policz(temp2);j++)
  83.         {
  84.             if(temp1->wartosc==temp2->wartosc)
  85.                 {
  86.  
  87.                 }
  88.         }
  89.     }
  90.  
  91. }
  92. */
  93.  
  94. //konwersja na system 7 i 6 z 10
  95. int naSiedem(int liczba)
  96. {
  97.     if(liczba==0) return 0;
  98.     naSiedem(liczba/7);
  99.     return printf("%d",liczba%7);
  100. }
  101. int naSzesc(int liczba)
  102. {
  103.     if(liczba==0)return 0;
  104.     naSzesc(liczba/6);
  105.     return printf("%d",liczba%6);
  106.  
  107. }
  108. int czy_pierwsza(int a){
  109.     if(a<2)return 0;
  110.     for(int i=2;i*i<a;i++)
  111.     {
  112.         if(a%i==0)return 0;
  113.  
  114.     }
  115.     return 1;
  116.  
  117. }
  118.  
  119. //Nierekurencyjnuie
  120. int przedzial(int a,int b)
  121. {
  122.     int max=0;
  123.     for(;a<=b;a++)
  124.     {
  125.         if(czy_pierwsza(a)==0)
  126.         {
  127.             if(max<a) max=a;
  128.  
  129.         }
  130.  
  131.     }
  132.     return max;
  133. }
  134. //Rekurencyjnie
  135. int przedzialReku(int a, int b)
  136. {
  137.     if(a>b)return -1;
  138.     else {
  139.         if(czy_pierwsza(b)==0)return b;
  140.             else return przedzialReku(a,b-1);
  141.     }
  142. }
  143.  
  144. int main()
  145. {
  146.      printf("Na sys 7:");
  147.     naSiedem(55);
  148.  
  149.     printf("\nNa sys 7:");
  150.     naSzesc(55);
  151.     printf("\nCzy pierwsza?:");
  152.     if(czy_pierwsza(17)==1)
  153.             {
  154.             printf("Pierwsza");
  155.             }
  156.         else {
  157.             printf("Nie Pierwsza");
  158.         }
  159.     printf("\nNajwieksza niepierwsza z przedzialu?:%d",przedzialReku(5,11));
  160.  
  161.  
  162.     lista n1 = (lista)malloc(sizeof(struct Node));
  163.     lista n2 = (lista)malloc(sizeof(struct Node));
  164.     lista n3 = (lista)malloc(sizeof(struct Node));
  165.     lista n4 = (lista)malloc(sizeof(struct Node));
  166.     lista n5 = (lista)malloc(sizeof(struct Node));
  167.  
  168.     n1->wartosc=1;
  169.     n1->next=n2;
  170.  
  171.     n2->wartosc=2;
  172.     n2->next=n3;
  173.  
  174.     n3->wartosc=-3;
  175.     n3->next=n4;
  176.  
  177.     n4->wartosc=-4;
  178.     n4->next=n5;
  179.     n5->wartosc=5;
  180.     n5->next=NULL;
  181.  
  182.  
  183.     lista a1 = (lista)malloc(sizeof(struct Node));
  184.     lista a2 = (lista)malloc(sizeof(struct Node));
  185.     lista a3 = (lista)malloc(sizeof(struct Node));
  186.     lista a4 = (lista)malloc(sizeof(struct Node));
  187.  
  188.     a1->wartosc=1;
  189.     a1->next=a2;
  190.  
  191.     a2->wartosc=2;
  192.     a2->next=a3;
  193.  
  194.     a3->wartosc=3;
  195.     a3->next=a4;
  196.  
  197.     a4->wartosc=4;
  198.     a4->next=NULL;
  199.  
  200.  
  201.     //Wypisywanie;
  202.     printf("\nLista 1:\n");
  203.     pokazListe(n1);
  204.     printf("\nLista 2:\n");
  205.     pokazListe(a1);
  206.     printf("\nPO :\n");
  207.     srednia(n1);
  208.     deleteMid(n1);
  209.     printf("\nLista po usunieciu el srodkowego:\n");
  210.     pokazListe(n1);
  211.  
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement