Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. template <class T> class elementStosu;
  8. template <class T> class Stos;
  9. template <class T> class Iterator;
  10.  
  11. // ======================================================================
  12. //                    Template Class stos
  13. // ======================================================================
  14. template <class T>
  15. class Stos {
  16. protected:
  17.     elementStosu <T> *first;                // data field
  18.  
  19. public:
  20.     Stos() : first(nullptr) {}      // default constructor
  21.     Stos(const Stos &source);       // copy constructor
  22.     virtual ~Stos() {};                // destructor
  23.  
  24.     virtual void add(T value);      // insert a new item
  25.     virtual void deleteAll();
  26.     T firstElement() const;        // access the first item
  27.     virtual bool includes(T value) const;  // inclusion test
  28.     bool isEmpty() const;
  29.     virtual void removeFirst();
  30.    
  31.     friend class Iterator <T>;
  32. };
  33. // ======================================================================
  34. //                    Template Class element stosu
  35. // ======================================================================
  36. template <class T>
  37. class elementStosu {
  38. private:
  39.     T value;
  40.     elementStosu <T> *next;
  41.  
  42.     elementStosu(T val, elementStosu *ptr) : value(val), next(ptr) { }
  43.    
  44.    
  45. public:
  46.     elementStosu <T> *insert(T value);      // after current Link
  47.  
  48.     friend class Stos <T>;
  49.     friend class Iterator <T>;
  50. };
  51.  
  52. // ======================================================================
  53. //                    Template Class elementStosu - attributes
  54. // ======================================================================
  55. template <class T> elementStosu <T> * elementStosu <T> ::insert(T value) {
  56.     next = new elementStosu <T>(value, next);
  57.     return next;
  58. }
  59.  
  60. // ======================================================================
  61. //                    Template Class stos - attributes
  62. // ======================================================================
  63. template <class T> void Stos <T> ::add(T value)
  64. {
  65.     first = new elementStosu <T>(value, first);
  66. }
  67.  
  68. template <class T> T Stos <T> ::firstElement() const
  69. {
  70.     assert(first != nullptr);
  71.     return first->value;
  72. }
  73.  
  74.  
  75. template <class T> bool Stos <T> ::isEmpty() const
  76. {
  77.     return first == nullptr;
  78. }
  79.  
  80.  
  81. template <class T> bool Stos <T> ::includes(T value) const
  82. {
  83.     for (elementStosu <T> *p = first; p; p = p->next)
  84.         if (value == p->value)return true;
  85.     return false;
  86. }
  87.  
  88. template <class T> void Stos <T> ::removeFirst() {
  89.     assert(first != nullptr);
  90.     elementStosu <T> *ptr = first;  // save pointer to the first item
  91.     first = ptr->next;      // reassign the first node
  92.     delete ptr;
  93. }
  94.  
  95. template <class T> void Stos <T> ::deleteAll() {
  96.     elementStosu <T> *next;
  97.     for (elementStosu <T> *p = first; p; p = next) {
  98.         next = p->next;
  99.         delete p;
  100.     }
  101.     first = nullptr;
  102. }
  103.  
  104. template <class T>
  105. ostream &operator << (ostream &wyjscie, Stos <T> &s)
  106. {
  107.     Stos <T> pom;
  108.    
  109.         while(!s.isEmpty())
  110.         {
  111.             pom.add(s.firstElement());
  112.             s.removeFirst();
  113.         }
  114.        
  115.         while(!pom.isEmpty())
  116.         {
  117.             s.add(pom.firstElement());
  118.             wyjscie << pom.firstElement()<< " ";
  119.             pom.removeFirst();
  120.         }
  121.        
  122.         return wyjscie;
  123. }
  124.  
  125.  
  126. template <class T>
  127. class FIFO
  128. {
  129.   private:
  130.   Stos <T> stos1;
  131.   Stos <T> stos2;
  132.  
  133.   public:
  134.   void add(T wartosc);
  135.   void remove(int wartosc);
  136.   T firsElement();
  137.   void disp();
  138.  
  139. };
  140.  
  141.  
  142. template <class T> void FIFO <T> ::add(T wartosc)
  143. {
  144.     stos1.add(wartosc);
  145. }
  146.  
  147. template <class T> void FIFO <T> ::disp()
  148. {
  149.     cout<<stos1 << stos2;
  150. }
  151.  
  152. template <class T> void FIFO <T> ::remove(int w)            //w-ilosc elementow do usuniecia
  153. {
  154.     if(w>0)
  155.     {
  156.         if(stos2.isEmpty())
  157.         {
  158.             while(!stos1.isEmpty())
  159.             {
  160.                 stos2.add(stos1.firstElement());
  161.                 stos1.removeFirst();
  162.                    
  163.             }
  164.            
  165.             while(stos2.isEmpty()==false && w>0)
  166.             {
  167.              
  168.                 stos2.removeFirst();
  169.                 w--;
  170.             }
  171.            
  172.         }
  173.         else
  174.         {
  175.              while(stos2.isEmpty()==false && w>0)
  176.             {
  177.                
  178.                 stos2.removeFirst();
  179.                 w--;
  180.             }
  181.             if(w>0)
  182.             {
  183.                 while(!stos1.isEmpty())
  184.                 {
  185.                     stos2.add(stos1.firstElement());
  186.                     stos1.removeFirst();
  187.                    
  188.                 }
  189.                
  190.                 while(stos2.isEmpty()==false && w>0)
  191.                 {
  192.                    
  193.                     stos2.removeFirst();
  194.                     w--;
  195.                 }
  196.             }
  197.         }
  198.        
  199.     }
  200.    
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. // ======================================================================
  210. //                            main
  211. // ======================================================================
  212. int main()
  213. {
  214.    
  215.     FIFO <int> Kolejka;
  216.    
  217.     int K;
  218.     cin>>K;
  219.    
  220.     int wartosc;
  221.    
  222.     for(int i=0; i<K; i++)
  223.     {
  224.         cin>>wartosc;
  225.         Kolejka.add(wartosc);
  226.        
  227.     }
  228.    
  229.     int L;
  230.     cin>>L;
  231.    
  232.     Kolejka.remove(L);
  233.    
  234.     int M;
  235.     cin>>M;
  236.    
  237.     int wartosc2;
  238.    
  239.     for(int i=0; i<M; i++)
  240.     {
  241.         cin>>wartosc2;
  242.         Kolejka.add(wartosc2);
  243.        
  244.     }
  245.    
  246.      int N;
  247.     cin>>N;
  248.    
  249.     Kolejka.remove(N);
  250.    
  251.     Kolejka.disp();
  252.    
  253.    
  254.    
  255.    
  256.     return 1;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement