Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- using namespace std;
- template<class T>
- class Lista
- {
- struct element
- {
- element *nast;
- T dana;
- element(T& d):dana(d)
- {nast=NULL;}
- };
- element* przod;
- element* znajdzW(T);
- public:
- Lista();
- ~Lista();
- bool wstaw(T);
- bool znajdz(T d){return znajdzW(d);}
- bool usun(T);
- int UsunDuplikaty();
- bool DaneZPrzedzialuDoPliku(string, int, int);
- bool Zawiera(Lista<T>&);
- void wyswietl()const;
- };
- template<class T>
- Lista<T>::Lista()
- {przod=NULL;}
- template<class T>
- bool Lista<T>::wstaw(T d)
- {
- try
- {
- element *pom=new element(d);
- if(przod==NULL)
- {
- przod=pom;
- return true;
- }
- if(d<przod->dana)
- {
- pom->nast=przod;
- przod=pom;
- return true;
- }
- element *gdzie=przod;
- while(gdzie->nast!=NULL && gdzie->nast->dana < d)
- { gdzie=gdzie->nast; }
- pom->nast=gdzie->nast;
- gdzie->nast=pom;
- }
- catch(...)
- {
- return false;
- }
- return false;
- }
- template<class T>
- void Lista<T>::wyswietl()const
- {
- element *co=przod;
- while(co!=NULL)
- {
- cout << co->dana << " ";
- co=co->nast;
- }
- }
- template<class T>
- Lista<T>::element* Lista<T>::znajdzW(T d)
- {
- element *co=przod;
- while(co && co->dana<d)
- {
- co=co->nast;
- }
- if(co) return co; // gdy jest NULL
- else if(co->dana==d) return co;
- else return NULL;
- }
- template<class T>
- bool Lista<T>::usun(T d)
- {
- element *co=znajdzW(d);
- if(co)// jesli istnieje
- {
- if(co==przod)
- {
- element *pom=przod->nast;
- delete przod;
- przod=pom;
- return true;
- }
- else if(co->nast==NULL)// ostatni
- {
- delete co;
- return true;
- }
- else // jest w srodku
- {
- co->dana=co->nast->dana;
- co->nast=co->nast->nast;
- delete co->nast;
- return true;
- }
- }
- return false;
- }
- template<class T>
- int Lista<T>::UsunDuplikaty()
- {
- element *co=przod;
- if(co==NULL) return -1;
- int ile=0;
- while(co && co->nast!=NULL)
- {
- if(co->dana==co->nast->dana)
- {
- element *rob=co->nast;
- co->nast=rob->nast;
- delete rob;
- ile++;
- }
- co=co->nast;
- }
- return ile;
- }
- template<class T>
- bool Lista<T>::DaneZPrzedzialuDoPliku(string nazwa , int p, int k)
- {
- if(przod==NULL || przod->dana > k) return false; // pusta i poczoatek
- element *co=przod;
- while(co && co->dana < p)
- { co=co->nast; }
- if(co==NULL || co->dana > k) return false;
- fstream plik(nazwa.c_str(), ios::out);
- if(plik.good())
- {
- while(co && co->dana <= k)
- {
- if(co->dana>=p)
- plik << co->dana << " ";
- co=co->nast;
- }
- plik.close();
- return true;
- }
- else
- {
- plik.close();
- return false;
- }
- }
- template<class T>
- bool Lista<T>::Zawiera(Lista &L)
- {
- }
- template<class T>
- Lista<T>::~Lista()
- {
- while(przod!=NULL)
- {
- element *pom=przod->nast;
- delete przod;
- przod=pom;
- }
- }
- int main()
- {
- Lista<int> t;
- t.wstaw(10);
- t.wstaw(20);
- t.wstaw(30);
- t.DaneZPrzedzialuDoPliku("1.txt",15,17);
- t.wyswietl(); cout << endl;
- cout << t.UsunDuplikaty() << endl;
- t.wyswietl(); cout << endl;
- t.DaneZPrzedzialuDoPliku("2.txt",15,21);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement