Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lista.h
- #ifndef LISTA_H
- #define LISTA_H
- #include "Head.h"
- class Lista
- {
- public:
- Lista(); // konstruktor domyœlny
- ~Lista();
- void wczytaj_z_pliku();
- void dodaj(string, string, string);
- void dodawanie();
- void wypisz_liste();
- void kasuj_ostatni();
- private:
- Student* head;
- };
- #endif // LISTA_H
- // Lista.cpp
- #include <fstream>
- #include "Lista.h"
- using namespace std;
- /*class Lista
- {
- public:
- Lista(); // konstruktor domyœlny
- ~Lista();
- void wczytaj_z_pliku();
- void dodaj(string, string, string);
- void dodawanie();
- void wypisz_liste();
- void kasuj_ostatni();
- private:
- Student* head;
- };*/
- Lista::Lista() : head(nullptr)
- {
- }
- Lista::~Lista()
- {
- while(head)
- kasuj_ostatni();
- cout << "Lista usunieta\n";
- }
- void Lista::kasuj_ostatni()
- {
- Student* temp = head;
- if(temp)
- {
- if(temp->next)
- {
- while(temp->next->next )
- temp = temp->next;
- delete temp->next;
- temp->next = nullptr;
- }
- else
- {
- delete temp;
- head = nullptr;
- }
- }
- }
- void Lista::wczytaj_z_pliku()
- {
- ifstream plik;
- string imie, nazwisko, pesel;
- plik.open("lista.txt");
- if(plik.fail())
- {
- cout << "Nie udalo sie wczytac pliku.\n";
- return;
- }
- while(!plik.eof())
- {
- plik >> imie >> nazwisko >> pesel;
- dodaj(imie, nazwisko, pesel);
- }
- cout << "Lista wczytana prawidlowo\n";
- plik.close();
- }
- void Lista::dodawanie()
- {
- cout << "Podaj dane studenta (imie, nazwisko, pesel): ";
- string imie, nazwisko, pesel;
- cin >> imie >> nazwisko >> pesel;
- dodaj(imie, nazwisko, pesel);
- }
- void Lista::dodaj(string imie, string nazwisko, string pesel)
- {
- Student* nowy = new Student;
- nowy->Name = imie;
- nowy->LastName = nazwisko;
- nowy->dane = pesel;
- if(head == nullptr)
- head = nowy;
- else
- {
- Student* temp = head;
- while(temp->next)
- temp = temp->next;
- temp->next = nowy;
- nowy->next = nullptr;
- }
- }
- void Lista::wypisz_liste()
- {
- Student* temp = head;
- while(temp != nullptr)
- {
- cout << temp->Name << " " << temp->LastName << " " << temp->dane << endl;
- temp = temp->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment