/* * slist.cpp * * Created on: 27.11.2015 г. * Author: trifon */ #include using namespace std; #include "linked_list.cpp" class Element { public: // извеждане virtual void print(ostream&) const = 0; // събиране на атоми virtual void collectInts(LinkedList&) const = 0; virtual ~Element() {} }; class IntElement : public Element { private: int x; public: IntElement(int _x = 0) : x(_x) {} void print(ostream& os) const { os << x; } void collectInts(LinkedList& l) const { l.insertEnd(x); } }; class ListElement : public Element, public LinkedList { public: void print(ostream& os) const { os << '('; for(LinkedListIterator it = this->begin(); it; ++it) { if (it != this->begin()) os << ' '; (*it)->print(os); } os << ')'; } void collectInts(LinkedList& l) const { for(LinkedListIterator it = this->begin(); it; ++it) (*it)->collectInts(l); } ~ListElement() { for(LinkedListIterator it = this->begin(); it; ++it) { /* cout << "Ще изтрием: "; (*it)->print(cout); cout << endl; */ delete *it; } } }; using SList = ListElement; /* ostream& operator<<(ostream& os, LinkedListconst& sl) { for(LinkedListIterator it = sl.begin(); it; ++it) (*it)->print(os); return os; } */