#pragma once #include using namespace std; class LineListException : public exception { public: const char* what() const noexcept override { return "LineList Exception"; } }; template class LineListElem { T data; LineListElem* next; public: LineListElem(const T& adata, LineListElem* anext); const T& getData() const; LineListElem* getNext(); template friend class LineList; }; template class LineList { LineListElem* start; public: LineList(); ~LineList(); LineListElem* getStart(); void deleteFirst(); void deleteAfter(LineListElem* ptr); void insertFirst(const T& data); void insertAfter(LineListElem* ptr, const T& data); template friend ostream& operator <<(ostream& out, LineList& list); }; template LineListElem::LineListElem(const T& adata, LineListElem* anext) { data = adata; next = anext; } template LineListElem* LineList::getStart() { return start; } template LineListElem* LineListElem::getNext() { return next; } template const T& LineListElem::getData() const { return data; } template LineList::LineList() { start = 0; } template LineList::~LineList() { while (start) deleteFirst(); } template void LineList::insertFirst(const T& data) { LineListElem* second = start; start = new LineListElem(data, second); } template void LineList::deleteAfter(LineListElem* ptr) { if (ptr && ptr->next) { LineListElem* temp = ptr->next; ptr->next = ptr->next->next; delete temp; } } template void LineList::deleteFirst() { if (start) { LineListElem* temp = start->next; delete start; start = temp; } else throw LineListException(); } template void LineList::insertAfter(LineListElem* ptr, const T& data) { if (ptr) { LineListElem* temp = ptr->next; ptr->next = new LineListElem(data, temp); } } template ostream& operator <<(ostream& out, LineList& list) { LineListElem* ptr = list.start; if (!ptr) out << "EMPTY "; else while (ptr) { out << ptr->getData() << ' '; ptr = ptr->getNext(); } return out; }