Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <exception>
- using namespace std;
- // to tylko do testow
- class EndOfList: public exception
- {
- public:
- virtual const char* what()
- {
- return "EndOfList exception happened!";
- }
- };
- template <class T>
- class ElementListy
- {
- public:
- virtual void setnext(ElementListy<T>& el) = 0;
- virtual ElementListy* getnext() = 0;
- virtual T getvalue() = 0;
- };
- template <class T>
- class MyElement: public ElementListy<T>
- {
- protected:
- const T value;
- MyElement* next;
- public:
- MyElement(T value)
- {
- this->value = value;
- }
- void setnext(MyElement<T>& el)
- {
- next = ⪙
- }
- MyElement* getnext()
- {
- return next;
- }
- T getvalue()
- {
- return value;
- }
- };
- template <class X>
- class Lista
- {
- protected:
- MyElement<X>& first;
- MyElement<X>& last;
- MyElement<X>*& current;
- public:
- Lista(MyElement<X>& mel)
- {
- first = &mel;
- }
- void addElement(MyElement<X>& newelem)
- {
- last = &newelem;
- }
- X getFirstValue()
- {
- return first->getvalue();
- }
- X getNextValue()
- {
- if(current == last)
- {
- EndOfList eol;
- throw eol;
- }
- return current->next->getvalue();
- }
- };
- int main()
- {
- try
- {
- Lista<string> lista(MyElement<string>("Ala ma kota");
- lista.addElement(MyElement<string>("Jarek ma psa"));
- ElementListy odczyt = lista.getFirstValue();
- }
- catch(EndOfList &e)
- {
- cout << "Blad! Poza zakresem!" << endl;
- }
- cin.sync();
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement