Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define ROZ 20
  5. struct dane
  6. {
  7.     char imie[ROZ];
  8.     char nazwisko[ROZ];
  9.     int rokuro;
  10. };
  11. struct elem
  12. {
  13.     struct dane dane;
  14.     struct elem * next;
  15. };
  16. void wypisanieelementu(struct elem * wsk)
  17. {
  18.     if(wsk != NULL)
  19.         printf("%s%s%i \n",(*wsk).dane.imie,(*wsk).dane.nazwisko,(*wsk).dane.rokuro);
  20. }
  21. struct elem * dodaj_poczatek(struct elem * head, struct dane * wpis)
  22. {
  23.     struct elem * new = malloc(sizeof(struct elem));
  24.     (*new).dane = *wpis;
  25.     (*new).next = head;
  26.     return new;
  27. }
  28. struct elem * dodaj_koniec(struct elem * head, struct dane * wpis)
  29. {
  30.     if(head == NULL)
  31.         return dodaj_poczatek(head, wpis);
  32.        
  33.     struct elem * new = malloc(sizeof(struct elem));
  34.     (*new).dane = *wpis;
  35.    
  36.     struct elem * tmp = head;
  37.     while((*tmp).next != NULL)
  38.         tmp = (*tmp).next;
  39.     (*tmp).next = new;
  40.    
  41.     return head;
  42. }
  43. struct elem * dodaj_n(struct elem * head, struct dane * wpis, int n)
  44. {
  45.     if(n == 0 || head == NULL)
  46.         return dodaj_poczatek(head, wpis);
  47.    
  48.     struct elem * tmp = head;
  49.     while(tmp != NULL && n > 1)
  50.     {
  51.         tmp = (*tmp).next;
  52.         n--;
  53.     }
  54.    
  55.     struct elem * new = malloc(sizeof(struct elem));
  56.     (*new).dane = *wpis;
  57.     (*new).next = (*tmp).next;
  58.     (*tmp).next = new;
  59.    
  60.     return head;
  61. }
  62. int search_rokuro(struct elem * head, int rokuro)
  63. {
  64.     int idx = 0;
  65.     while(head != NULL)
  66.     {
  67.         if((*head).dane.rokuro==rokuro)
  68.             return idx;
  69.         idx++;
  70.         head = (*head).next;
  71.     }
  72.     return -1;
  73. }
  74. int search_nazw(struct elem * head, char * nazwisko)
  75. {
  76.     int idx = 0;
  77.     while(head != NULL)
  78.     {
  79.         if(!strcmp((*head).dane.nazwisko, nazwisko))
  80.             return idx;
  81.         idx++;
  82.         head = (*head).next;
  83.     }
  84.     return -1;
  85. }
  86. // Drugi sposob zwracania glowy
  87. void usun_poczatek(struct elem ** head)
  88. {
  89.     struct elem * newhead = (*(*head)).next;
  90.     free(*head);
  91.     *head = newhead;
  92. }
  93. void usun_koniec(struct elem * head)
  94. {
  95.     struct elem * tmp = head;
  96.     while((*(*tmp).next).next != NULL) // biorę przedostani i w jego nexicie wpisuję null, po czym zwalniam poprzednie ostatni.//
  97.         tmp = (*tmp).next;
  98.     free((*tmp).next);
  99.     (*tmp).next = NULL;
  100. }
  101. int usun_n(struct elem * head, int n)
  102. {
  103.     int i;
  104.     struct elem * tmp = head;
  105.     for(i = 1; i < n ; i++)
  106.     {
  107.         if((*tmp).next == NULL)
  108.             return 0;
  109.         tmp = (*tmp).next;
  110.     }
  111.     struct elem * next = (*(*tmp).next).next;
  112.     free((*tmp).next);
  113.     (*tmp).next = next;
  114.     return 1;
  115. }
  116. void wypisanie(struct elem * head)
  117. {
  118.     while(head != NULL)
  119.     {
  120.         wypisanieelementu(head);
  121.         head = (*head).next;
  122.     }
  123. }
  124. int main()
  125. {
  126.     struct elem * first=NULL;
  127.     int w=-1;
  128.     for(;w!=0;)
  129.     {
  130.         printf("wybierz co chcesz zrobić \n");
  131.         printf("1 - wypisz listę \n");
  132.         printf("2 - dodaj element na poczatek \n");
  133.         printf("3 - dodawanie nowego elementu na końcu listy \n");
  134.         printf("4 - wyszukiwanie elementów wg. roku urodzenia \n");
  135.         printf("5 - dodawanie nowego elementu w dowolnym miejscu listy \n");
  136.         printf("6 - usuwanie elementu z poczatku listy \n");
  137.         printf("7 - usuwanie elementu z końca listy \n");
  138.         printf("8 - usuwanie elementu z dowolnego miejsca listy \n");
  139.         printf("9 - usuwanie całej listy \n");
  140.         printf("0 - koniec \n");
  141.         scanf("%i",&w);
  142.         getchar();
  143.     if(w!=1&&w!=2&&w!=3&&w!=4&&w!=5&&w!=6&&w!=7&&w!=8&&w!=9&&w!=0)
  144.         printf("wybrano zly numer \n");
  145.     else
  146.     {
  147.        if(w==1)
  148.        {
  149.         wypisanie(first);
  150.        }
  151.        if(w==2)
  152.        {
  153.            struct dane wpis;
  154.            printf("wpisz imie ");
  155.             fgets(wpis.imie,ROZ,stdin);
  156.            printf("wpisz nazwisko ");
  157.             fgets(wpis.nazwisko,ROZ,stdin);
  158.            printf("wpisz rok urodzenia ");
  159.             scanf("%i",&wpis.rokuro);
  160.                 getchar();
  161.            first = dodaj_poczatek(first,&wpis);
  162.        }
  163.        if(w==3)
  164.        {
  165.            struct dane wpis;
  166.            printf("wpisz imie ");
  167.             fgets(wpis.imie,ROZ,stdin);
  168.            printf("wpisz nazwisko ");
  169.             fgets(wpis.nazwisko,ROZ,stdin);
  170.            printf("wpisz rok urodzenia ");
  171.             scanf("%i",&wpis.rokuro);
  172.                 getchar();
  173.             first = dodaj_koniec(first,&wpis);
  174.        }
  175.        if(w==4)
  176.        {
  177.             int c;
  178.             printf("wpisz rok urodzenia ");
  179.             scanf("%i",&c);
  180.             getchar();
  181.             int res = search_rokuro(first,c);
  182.             if(res == -1)
  183.                 printf("Nie znaleziono elementu\n");
  184.             else
  185.                 printf("Pierwszy element z danym rokiem urodzenia ma index: %d\n", res);
  186.        }
  187.        if(w==5)
  188.        {
  189.            struct dane wpis;
  190.            printf("wpisz imie ");
  191.             fgets(wpis.imie,ROZ,stdin);
  192.            printf("wpisz nazwisko ");
  193.             fgets(wpis.nazwisko,ROZ,stdin);
  194.            printf("wpisz rok urodzenia ");
  195.             scanf("%i",&wpis.rokuro);
  196.                 getchar();
  197.             printf("podaj ktorym miejscu chcesz dodac nowy \n");
  198.             int n;
  199.             scanf("%i",&n);
  200.             getchar();
  201.             first = dodaj_n(first, &wpis, n);
  202.        }
  203.        if(w==6)
  204.        {
  205.            usun_poczatek(&first);
  206.        }
  207.        if(w==7)
  208.        {
  209.            usun_koniec(first);
  210.        }
  211.        if(w==8)
  212.        {
  213.            printf("podaj ktory element chcesz usunąć \n");
  214.             int o;
  215.             scanf("%i",&o);
  216.             getchar();
  217.            usun_n(first,o);
  218.        }
  219.        if(w==9)
  220.        {
  221.            while(first != NULL){
  222.                usun_poczatek(&first);
  223.            }
  224.        }
  225.        if(w==0)
  226.        {
  227.            break;
  228.        }
  229.     }
  230.     }
  231.     return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement