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:
- void setnext() = 0;
- ElementListy* next() = 0;
- T getvalue() = 0;
- };
- template <class T>
- class MyElement: public ElementListy
- {
- protected:
- const T value;
- MyElement* next;
- public:
- MyElement(T value)
- {
- this->value = value;
- }
- void setnext(MyElement el)
- {
- next = ⪙
- }
- MyElement* next()
- {
- return next;
- }
- T getvalue()
- {
- return value;
- }
- };
- template <class X>
- class Lista
- {
- protected:
- MyElement* first;
- MyElement* last;
- MyElement* current;
- public:
- Lista(MyElement mel)
- {
- first = &mel;
- }
- void addElement(MyElement 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;
- lista = Lista(MyElement("Ala ma kota"));
- lista.addElement(MyElement("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