Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Lista {
  6.     Lista(int i=0, Lista *ptr=NULL)
  7.     {
  8.         value = i;
  9.         next = ptr;
  10.     }
  11.     ~Lista()
  12.     {
  13.         if (next != NULL)
  14.         {
  15.             cout << "Usuwam wartosc: " << next->value << "\n";
  16.             delete(next);
  17.  
  18.             next = NULL;
  19.         }
  20.     }
  21.    
  22.     int value;
  23.     Lista *next;
  24. };
  25.  
  26.  
  27. int main()
  28. {
  29.     Lista *lista = new Lista;
  30.     lista->value = 5;
  31.     lista->next = new Lista(6);
  32.    
  33.     Lista *ptr = lista->next;
  34.     ptr->next = new Lista(10);
  35.  
  36.     // wyświetlamy przed wstawieniem
  37.     ptr = lista;
  38.     while (ptr != NULL)
  39.     {
  40.         cout << ptr->value << " ";
  41.         ptr = ptr->next;
  42.     }
  43.  
  44.     // wstawiamy na odpowiednią pozycję wartość
  45.     int a = 8;
  46.     ptr = lista;
  47.     cout << "Przed wstawieniem\n";
  48.     while (ptr->next != NULL && ptr->next->value < a)
  49.     {
  50.         ptr = ptr->next;
  51.     }
  52.     Lista *tmp = ptr->next;
  53.     ptr->next = new Lista(a, tmp);
  54.  
  55.     // wyświetlamy po wstawieniu
  56.     cout << "Po wstawieniu\n\n";
  57.     ptr = lista;
  58.     while (ptr != NULL)
  59.     {
  60.         cout << ptr->value << " ";
  61.         ptr = ptr->next;
  62.     }
  63.     cout << endl;
  64.    
  65.     // usuwamy całą listę
  66.     delete(lista);
  67.     system("PAUSE");
  68.     return EXIT_SUCCESS;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement