Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- #include <string>
- using namespace std;
- template <class T> class elementStosu;
- template <class T> class Stos;
- template <class T> class Iterator;
- // ======================================================================
- // Template Class stos
- // ======================================================================
- template <class T>
- class Stos {
- protected:
- elementStosu <T> *first; // data field
- public:
- Stos() : first(nullptr) {} // default constructor
- Stos(const Stos &source); // copy constructor
- virtual ~Stos() {}; // destructor
- virtual void add(T value); // insert a new item
- virtual void deleteAll();
- T firstElement() const; // access the first item
- virtual bool includes(T value) const; // inclusion test
- bool isEmpty() const;
- virtual void removeFirst();
- friend class Iterator <T>;
- };
- // ======================================================================
- // Template Class element stosu
- // ======================================================================
- template <class T>
- class elementStosu {
- private:
- T value;
- elementStosu <T> *next;
- elementStosu(T val, elementStosu *ptr) : value(val), next(ptr) { }
- public:
- elementStosu <T> *insert(T value); // after current Link
- friend class Stos <T>;
- friend class Iterator <T>;
- };
- // ======================================================================
- // Template Class elementStosu - attributes
- // ======================================================================
- template <class T> elementStosu <T> * elementStosu <T> ::insert(T value) {
- next = new elementStosu <T>(value, next);
- return next;
- }
- // ======================================================================
- // Template Class stos - attributes
- // ======================================================================
- template <class T> void Stos <T> ::add(T value)
- {
- first = new elementStosu <T>(value, first);
- }
- template <class T> T Stos <T> ::firstElement() const
- {
- assert(first != nullptr);
- return first->value;
- }
- template <class T> bool Stos <T> ::isEmpty() const
- {
- return first == nullptr;
- }
- template <class T> bool Stos <T> ::includes(T value) const
- {
- for (elementStosu <T> *p = first; p; p = p->next)
- if (value == p->value)return true;
- return false;
- }
- template <class T> void Stos <T> ::removeFirst() {
- assert(first != nullptr);
- elementStosu <T> *ptr = first; // save pointer to the first item
- first = ptr->next; // reassign the first node
- delete ptr;
- }
- template <class T> void Stos <T> ::deleteAll() {
- elementStosu <T> *next;
- for (elementStosu <T> *p = first; p; p = next) {
- next = p->next;
- delete p;
- }
- first = nullptr;
- }
- template <class T>
- ostream &operator << (ostream &wyjscie, Stos <T> &s)
- {
- Stos <T> pom;
- while(!s.isEmpty())
- {
- pom.add(s.firstElement());
- s.removeFirst();
- }
- while(!pom.isEmpty())
- {
- s.add(pom.firstElement());
- wyjscie << pom.firstElement()<< " ";
- pom.removeFirst();
- }
- return wyjscie;
- }
- template <class T>
- class FIFO
- {
- private:
- Stos <T> stos1;
- Stos <T> stos2;
- public:
- void add(T wartosc);
- void remove(int wartosc);
- T firsElement();
- void disp();
- };
- template <class T> void FIFO <T> ::add(T wartosc)
- {
- stos1.add(wartosc);
- }
- template <class T> void FIFO <T> ::disp()
- {
- cout<<stos1 << stos2;
- }
- template <class T> void FIFO <T> ::remove(int w) //w-ilosc elementow do usuniecia
- {
- if(w>0)
- {
- if(stos2.isEmpty())
- {
- while(!stos1.isEmpty())
- {
- stos2.add(stos1.firstElement());
- stos1.removeFirst();
- }
- while(stos2.isEmpty()==false && w>0)
- {
- stos2.removeFirst();
- w--;
- }
- }
- else
- {
- while(stos2.isEmpty()==false && w>0)
- {
- stos2.removeFirst();
- w--;
- }
- if(w>0)
- {
- while(!stos1.isEmpty())
- {
- stos2.add(stos1.firstElement());
- stos1.removeFirst();
- }
- while(stos2.isEmpty()==false && w>0)
- {
- stos2.removeFirst();
- w--;
- }
- }
- }
- }
- }
- // ======================================================================
- // main
- // ======================================================================
- int main()
- {
- FIFO <int> Kolejka;
- int K;
- cin>>K;
- int wartosc;
- for(int i=0; i<K; i++)
- {
- cin>>wartosc;
- Kolejka.add(wartosc);
- }
- int L;
- cin>>L;
- Kolejka.remove(L);
- int M;
- cin>>M;
- int wartosc2;
- for(int i=0; i<M; i++)
- {
- cin>>wartosc2;
- Kolejka.add(wartosc2);
- }
- int N;
- cin>>N;
- Kolejka.remove(N);
- Kolejka.disp();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement