Advertisement
Guest User

lista

a guest
May 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3.  
  4. using namespace std;
  5.  
  6. template<class T>
  7. class Lista
  8. {
  9.   struct element
  10.   {
  11.    element *nast;
  12.    T dana;
  13.    element(T& d):dana(d)
  14.    {nast=NULL;}
  15.   };
  16.   element* przod;
  17.   element* znajdzW(T);
  18.  public:
  19.   Lista();
  20.   ~Lista();
  21.   bool wstaw(T);
  22.   bool znajdz(T d){return znajdzW(d);}
  23.   bool usun(T);
  24.   int UsunDuplikaty();
  25.   bool DaneZPrzedzialuDoPliku(string, int, int);
  26.   bool Zawiera(Lista<T>&);
  27.  
  28.  
  29.   void wyswietl()const;
  30.  
  31.  
  32.  
  33. };
  34.  
  35. template<class T>
  36. Lista<T>::Lista()
  37. {przod=NULL;}
  38.  
  39. template<class T>
  40. bool Lista<T>::wstaw(T d)
  41. {
  42.  try
  43.  {
  44.   element *pom=new element(d);
  45.   if(przod==NULL)
  46.   {
  47.    przod=pom;
  48.    return true;
  49.   }
  50.  
  51.   if(d<przod->dana)
  52.   {
  53.    pom->nast=przod;
  54.    przod=pom;
  55.    return true;
  56.   }
  57.  
  58.   element *gdzie=przod;
  59.   while(gdzie->nast!=NULL && gdzie->nast->dana < d)
  60.   { gdzie=gdzie->nast; }
  61.  
  62.   pom->nast=gdzie->nast;
  63.   gdzie->nast=pom;
  64.  }
  65.  catch(...)
  66.  {
  67.   return false;
  68.  }
  69.  return false;
  70. }
  71.  
  72. template<class T>
  73. void Lista<T>::wyswietl()const
  74. {
  75.  element *co=przod;
  76.  while(co!=NULL)
  77.  {
  78.  cout << co->dana << " ";
  79.  co=co->nast;
  80.  }
  81. }
  82.  
  83. template<class T>
  84. Lista<T>::element* Lista<T>::znajdzW(T d)
  85. {
  86.  element *co=przod;
  87.  while(co && co->dana<d)
  88.  {
  89.   co=co->nast;
  90.  }
  91.  if(co) return co; // gdy jest NULL
  92.  else if(co->dana==d) return co;
  93.  else return NULL;
  94.  
  95. }
  96.  
  97. template<class T>
  98. bool Lista<T>::usun(T d)
  99. {
  100.   element *co=znajdzW(d);
  101.   if(co)// jesli istnieje
  102.   {
  103.    if(co==przod)
  104.    {
  105.     element *pom=przod->nast;
  106.     delete przod;
  107.     przod=pom;
  108.     return true;
  109.    }
  110.    else if(co->nast==NULL)// ostatni
  111.    {
  112.     delete co;
  113.     return true;
  114.    }
  115.    else // jest w srodku
  116.    {
  117.     co->dana=co->nast->dana;
  118.     co->nast=co->nast->nast;
  119.     delete co->nast;
  120.     return true;
  121.    }
  122.   }
  123.   return false;
  124. }
  125.  
  126. template<class T>
  127. int Lista<T>::UsunDuplikaty()
  128. {
  129.  element *co=przod;
  130.  if(co==NULL) return -1;
  131.  int ile=0;
  132.  while(co && co->nast!=NULL)
  133.  {
  134.    if(co->dana==co->nast->dana)
  135.    {
  136.      element *rob=co->nast;
  137.      co->nast=rob->nast;
  138.      delete rob;
  139.      ile++;
  140.    }
  141.    co=co->nast;
  142.  }
  143.  return ile;
  144.  
  145. }
  146.  
  147.  
  148.  
  149.  
  150. template<class T>
  151. bool Lista<T>::DaneZPrzedzialuDoPliku(string nazwa , int p, int k)
  152. {
  153.  if(przod==NULL || przod->dana > k) return false; // pusta i poczoatek
  154.  element *co=przod;
  155.  while(co && co->dana < p)
  156.  { co=co->nast; }
  157.  if(co==NULL || co->dana > k) return false;
  158.  fstream plik(nazwa.c_str(), ios::out);
  159.  if(plik.good())
  160.  {
  161.   while(co && co->dana <= k)
  162.   {
  163.    if(co->dana>=p)
  164.     plik << co->dana << " ";
  165.    co=co->nast;
  166.   }
  167.   plik.close();
  168.   return true;
  169.  }
  170.  else
  171.  {
  172.   plik.close();
  173.   return false;
  174.  }
  175.  
  176.  
  177.  
  178. }
  179.  
  180.  
  181.  
  182. template<class T>
  183. bool Lista<T>::Zawiera(Lista &L)
  184. {
  185.  
  186. }
  187.  
  188. template<class T>
  189. Lista<T>::~Lista()
  190. {
  191.  while(przod!=NULL)
  192.  {
  193.   element *pom=przod->nast;
  194.   delete przod;
  195.   przod=pom;
  196.  }
  197. }
  198.  
  199.  
  200. int main()
  201. {
  202.  Lista<int> t;
  203.  
  204.  t.wstaw(10);
  205.  t.wstaw(20);
  206.  t.wstaw(30);
  207.  
  208.  t.DaneZPrzedzialuDoPliku("1.txt",15,17);
  209.  t.wyswietl(); cout << endl;
  210.  cout << t.UsunDuplikaty() << endl;
  211.  t.wyswietl(); cout << endl;
  212.  
  213.  t.DaneZPrzedzialuDoPliku("2.txt",15,21);
  214.  
  215.  
  216.  system("pause");
  217.  return 0;
  218.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement