peterzig

[AISD] Lista Jednokierunkowa 2

Sep 13th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <conio.h>
  3. #include <malloc.h>
  4.  
  5. struct Lista
  6. {
  7.     Lista *next;
  8.     float data;
  9. };
  10.  
  11. Lista *Dodaj(Lista *L, const float wartosc)
  12. {
  13.     if (L == NULL)
  14.     {
  15.         L = (Lista*)malloc(sizeof(Lista));
  16.         L->data = wartosc;
  17.         L->next = 0;
  18.     }
  19.     else
  20.     {
  21.         Lista *nl = (Lista*)malloc(sizeof(Lista));
  22.         nl->data = wartosc;
  23.         nl->next = 0;
  24.  
  25.         while (L->next != 0)
  26.             L = L->next;
  27.         L->next = nl;
  28.         return nl;
  29.     }
  30.     return L;
  31. }
  32.  
  33. int LicznikEl(Lista *L)
  34. {
  35.     int licznik = 1;
  36.     while (L->next != 0)
  37.     {
  38.         licznik++;
  39.         L = L->next;
  40.     }
  41.     return licznik;
  42. }
  43.  
  44. Lista *Nastepny(Lista *L)
  45. {
  46.     return L->next;
  47. }
  48.  
  49. float Odczytaj(Lista *L)
  50. {
  51.     return L->data;
  52. }
  53.  
  54. void ZwolnijListe(Lista *L)
  55. {
  56.     if (L != NULL)
  57.     {
  58.         while (L->next != 0)
  59.         {
  60.             Lista *dL = L;
  61.             L = L->next;
  62.             free(dL);
  63.         }
  64.         free(L);
  65.     }
  66. }
  67.  
  68. int main()
  69. {
  70.     printf("Lista:\n");
  71.  
  72.     Lista *X, *L = Dodaj(0, 11.0f);
  73.     Dodaj(L, 22.0f);
  74.     Dodaj(L, 33.0f);
  75.     Dodaj(L, 44.0f);
  76.  
  77.     printf("Powinno byc 4, jest %d elementow listy.\n", LicznikEl(L));
  78.  
  79.     for (X = L; X != 0; X = Nastepny(X))
  80.         printf("%.2f\n", Odczytaj(X));
  81.  
  82.     printf(".....................\n");
  83.     Dodaj(L, 100.0f);
  84.  
  85.     printf("Jest %d elementow listy.\n", LicznikEl(L));
  86.  
  87.     for (X = L; X != 0; X = Nastepny(X))
  88.         printf("%.2f\n", Odczytaj(X));
  89.  
  90.     ZwolnijListe(L);
  91.     _getch();
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment