Advertisement
peterzig

[AISD] Lista Jednokierunkowa

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