Advertisement
Guest User

1

a guest
Oct 14th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include<time.h> //time
  3. #include<fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct lista {
  8.     int a;
  9.     double b;
  10.     char c;
  11.     lista* next;
  12. };
  13.  
  14. void dodaj(lista*& head, int wart)
  15. {
  16.     lista* p = new lista;
  17.  
  18.     p->a = wart;    // inicjujemy element typu int
  19.     p->b = (rand() % 100);;    // inicjujemy element typu double
  20.     p->c = 'T';    // inicjujemy element typu char
  21.     p->next = head;
  22.     head = p;
  23. }
  24. void dodajx(lista*& head, int x) {
  25.     lista* p = new lista;
  26.     for (int i = 0; i < x; i++)
  27.     {
  28.         p->a = (rand() % 99900) + 99;    // inicjujemy element typu int
  29.         p->b = (rand()% 100);    // inicjujemy element typu double
  30.         p->c = 'T';    // inicjujemy element typu char
  31.         p->next = head;
  32.         head = p;
  33.     }
  34. }
  35. void znajdz(lista*& head, int x)
  36. {
  37.     lista* p = head;
  38.     while (p && p->a != x) p = p->next;
  39.     if (p->a = x)
  40.     {
  41.         cout << "Element istnieje" << endl;
  42.     }
  43.     else
  44.     {
  45.         cout << "Element nie istnieje" << endl;
  46.     }
  47. }
  48. /*void usun(lista*& head, lista* e)
  49. {
  50.     lista* p;
  51.  
  52.     if (head == e) dodaj(head);
  53.     else
  54.     {
  55.         p = head;
  56.         while (p->next != e) p = p->next;
  57.         p->next = e->next;
  58.         delete e;
  59.     }
  60. }
  61. */
  62. int dlugosc_listy(lista*& head)
  63. {
  64.     int d = 0;
  65.     lista* p = head;
  66.     while (p != NULL)
  67.     {
  68.         d++;
  69.         p = p->next;
  70.     }
  71.     return d;
  72. }
  73.  
  74. void wypisz(lista*& head)
  75. {
  76.     lista* p = head;
  77.  
  78.     if (p == NULL)
  79.     {
  80.         cout << "Lista jest pusta" << endl;
  81.     }
  82.     else
  83.     {
  84.         for (int i = 1; p; p = p->next) {
  85.             cout << "Element #" << i++ << "  data = " << endl << p->a << endl << p->b << endl << p->c << endl;
  86.            
  87.             cout << endl;
  88.         }
  89.     }
  90. }
  91.  
  92. int main()
  93. {
  94.     srand(time(NULL));
  95.    
  96.     clock_t begin, end;
  97.     int a2;
  98.     double b2;
  99.     char c2;
  100.     double time_spent;
  101.     /* tutaj zaczyna sie czas */
  102.     begin = clock();
  103. /*
  104.     fstream plik;
  105.     plik.open("inlab02.txt", ios::in | ios::out);
  106.     if (plik.good() == true) //jeśli tak można operować na plikach
  107.     {
  108.         plik >> a2; //int
  109.         plik >> b2;  //double
  110.         plik >> c2;
  111.     }
  112.     else cout << "Brak dostępu do pliku!" << endl;
  113.     */
  114.    
  115.     /* tutaj jest koniec czasu */
  116.     end = clock();
  117.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  118.     cout << "Czas wykonania: " << time_spent;
  119.     lista* L = NULL; // zawiera adres początku listy
  120.     lista* e;
  121.     int x;
  122.     for (int i = 1; i <= 6; i++)
  123.     {
  124.         cout << endl;
  125.         cin >> x;
  126.         dodaj(L, x);
  127.     }
  128.     wypisz(L);
  129.  
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement