Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Lista {
- Lista(int i=0, Lista *ptr=NULL)
- {
- value = i;
- next = ptr;
- }
- ~Lista()
- {
- if (next != NULL)
- {
- cout << "Usuwam wartosc: " << next->value << "\n";
- delete(next);
- next = NULL;
- }
- }
- int value;
- Lista *next;
- };
- int main()
- {
- Lista *lista = new Lista;
- lista->value = 5;
- lista->next = new Lista(6);
- Lista *ptr = lista->next;
- ptr->next = new Lista(10);
- // wyświetlamy przed wstawieniem
- ptr = lista;
- while (ptr != NULL)
- {
- cout << ptr->value << " ";
- ptr = ptr->next;
- }
- // wstawiamy na odpowiednią pozycję wartość
- int a = 8;
- ptr = lista;
- cout << "Przed wstawieniem\n";
- while (ptr->next != NULL && ptr->next->value < a)
- {
- ptr = ptr->next;
- }
- Lista *tmp = ptr->next;
- ptr->next = new Lista(a, tmp);
- // wyświetlamy po wstawieniu
- cout << "Po wstawieniu\n\n";
- ptr = lista;
- while (ptr != NULL)
- {
- cout << ptr->value << " ";
- ptr = ptr->next;
- }
- cout << endl;
- // usuwamy całą listę
- delete(lista);
- system("PAUSE");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement