Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Para
- {
- int dana;
- Para *nastepna;
- Para(int wartosc):dana(wartosc),nastepna(nullptr)
- {}
- };
- class LinkedList
- {
- public:
- LinkedList():poczatek(nullptr) {};
- void dodaj(int wartosc)
- {
- Para *nowaPara = new Para(wartosc);
- if (poczatek == nullptr)
- {
- poczatek = nowaPara;
- }
- else
- {
- Para *temp = poczatek;
- while (temp->nastepna != nullptr)
- {
- temp = temp->nastepna;
- }
- temp->nastepna = nowaPara;
- }
- }
- int pobierz(int index)
- {
- Para *temp = poczatek;
- int i = 0;
- while (temp != nullptr)
- {
- if (i == index)
- return temp->dana;
- i++;
- temp = temp->nastepna;
- }
- return 0;
- }
- void wyczysc()
- {
- Para *temp = poczatek;
- while (temp != nullptr)
- {
- Para *next = temp->nastepna;
- delete temp;
- temp = next;
- }
- poczatek = nullptr;
- }
- ~LinkedList()
- {
- wyczysc();
- };
- private:
- Para *poczatek;
- };
- int main()
- {
- LinkedList lista;
- lista.dodaj(4);
- lista.dodaj(8);
- cout << "Pierwszy element listy: " << lista.pobierz(0) << endl;
- cout << "Drugi element listy: " << lista.pobierz(1) << endl;
- lista.wyczysc();
- cout << "Pierwszy element listy: " << lista.pobierz(0) << endl;
- cout << "Drugi element listy: " << lista.pobierz(1) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment