Advertisement
Evilerus

Untitled

Jan 27th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct el_db
  6. {
  7.     float f;
  8.     int i;
  9.     char c[100];
  10.     struct el_db *next;
  11. } el;
  12.  
  13. int dodaj(float f, int i, char c[], el **element)
  14. {
  15.     el *nowy;
  16.     nowy = malloc(sizeof(el));
  17.     if (nowy!=NULL)
  18.     {
  19.         nowy->f=f;
  20.         nowy->i=i;
  21.         strcpy(nowy->c, c);
  22.         nowy->next=*element;
  23.         *element = nowy;
  24.         return 1;
  25.     }
  26.     else
  27.     {
  28.         printf("Brak pamięci na nowy element listy!\n");
  29.         return 0;
  30.     }
  31. }
  32.  
  33. void usun(el **element)
  34. {
  35.     el *tmp;
  36.     if (*element!=NULL)
  37.     {
  38.        tmp = *element;
  39.        *element=tmp->next;
  40.        free(tmp);
  41.     }
  42. }
  43.  
  44. void wypisz(el *element)
  45. {
  46.     while (element!=NULL)
  47.     {
  48.         printf("f = %f, i = %d, c = %s\n", element->f, element->i, element->c);
  49.         element=element->next;
  50.     }
  51. }
  52.  
  53. int main(int argc, char **argv)
  54. {
  55.     el *start = NULL;
  56.     if (dodaj(100.0, 100, "Damian", &start) && dodaj(200.0, 200, "Krzysztof", &start))
  57.     {
  58.        wypisz(start);
  59.        printf("\n");
  60.        if (dodaj(300.0, 300, "Jaroslaw", &start))
  61.        {
  62.            wypisz(start);
  63.        }
  64.        else
  65.        {
  66.            return 1;
  67.        }
  68.        usun(&start);
  69.        printf("\n");
  70.        wypisz(start);
  71.     }
  72.     else
  73.     {
  74.         return 1;
  75.     }
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement